getByRole
getByRole is a user-interface testing query that locates an element by its accessible role and optionally its accessible name.
React in Depth
getByRole is a React Testing Library query function that finds an element by its semantic role and optionally its accessible name.
- How it is used
- Uses semantic roles to locate rendered React elements during component tests.
- How deep it goes
- Explains ARIA roles and demonstrates scoped and named queries, but defers most WAI-ARIA specification detail.
- Examples
- Tests a MenuItem link nested inside a list item and verifies its href, title, and text.
- What you can do after
- Apply it via React Testing Library to locate accessible elements and assert their rendered properties.
Build Financial Software with Generative AI (From Scratch)
getByRole is a user-interface testing query method that retrieves an element according to its accessible role.
- How it is used
- Selects interface elements by accessible role in unit and end-to-end tests.
- How deep it goes
- Demonstrates queries for headings, textboxes, and buttons, but does not develop the full ARIA standard.
- Examples
- Uses an upload-page heading and login textboxes, sign-in button, and error heading as query examples.
- What you can do after
- Apply it via React Testing Library or Playwright to locate accessible headings, fields, and buttons in UI tests.
Read how Build Financial Software with Generative AI (From Scratch) explains getByRole →
Related concepts
- accessible nameuses · An accessible name is the programmatically determined text label by which an interface element is identified to assistive technologies.
- aria semanticsuses · ARIA semantics are WAI-ARIA roles, states, and properties that communicate interface elements’ meaning and behavior to browsers and assistive technologies.
- WAI-ARIAuses · WAI-ARIA is a W3C accessibility specification that defines roles, states, and properties for communicating web interface semantics to assistive technologies.
Questions about getByRole
What is getByRole?
getByRole is a user-interface testing query that locates an element by its accessible role and optionally its accessible name.
Which Manning books explain getByRole?
React in Depth and Build Financial Software with Generative AI (From Scratch).
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

