For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/testing-library/queries.md.

Element queries

Find rendered elements the way a screen reader does: by role, by accessibility label or by text. import { screen, within } from '@blotch/angular-lynx-testing-library';

Usage

const { getByRole } = await render(Counter);

getByRole('button', { name: 'Add one' }); // bound to render()'s container
screen.getByText('Taps: 0'); // bound to document.body
  • render()'s result has every query, bound to its container.
  • screen has them bound to document.body.
  • within() binds them to one element.

*ByRole

getByRole(role: ByRoleMatcher, options?: ByRoleOptions): HTMLElement;
// getAllBy…, queryBy…, queryAllBy…, findBy… and findAllBy… too

Testing Library's *ByRole, reading Lynx's accessibility attributes as a screen reader does. It also finds what Testing Library finds, such as an <input> as a textbox.

accessibility-traitsRole
buttonbutton
linklink
imageimg
headerheading
searchsearchbox
adjustableslider
tabbartablist
  • <image> is an img unless it sets accessibility-traits. Other traits, such as text, give no role.
  • accessibility-heading is a heading, as Android reads it. This adds to the roles from the traits.
  • accessibility-role-description of button, switch, checkbox, image or progressbar is that role, as Android reads it. image is img. This adds to the roles from the traits too.

Which elements match

  • Only reachable elements match. A screen reader reaches <text>, <image>, and other elements with accessibility-element="true".
  • accessibility-elements-hidden hides an element and everything in it.
  • A tabbar is read from the container around the tabs, so it needs no accessibility-element.
  • Pass hidden: true to include the rest.

Options

  • name is the accessibility-label.
    • A <text> without one is named by its text.
    • A <view> isn't named by the text inside it, since iOS doesn't read it.
    • An accessibility-status comes first, as iOS and Android read it: { name: 'Unread, Inbox' }.
  • selected is the selected trait: accessibility-traits="button,selected".
    • An active routerLinkActive element gets it, and [lynxAccessibilityTraits]="{ selected: true }" adds it.
    • iOS reads a comma-separated list, but Android reads only one trait.
  • value: { text } is the accessibility-value.
  • Other ARIA states, such as checked or expanded, have no Lynx attribute. A switch or checkbox from a role description never has them.

ARIA in a template

The renderer turns these ARIA attributes into Lynx's, so the queries find them as VoiceOver and TalkBack do:

In the templateBecomes
roletraits and accessibility-element
aria-labelaccessibility-label
aria-hidden="true"accessibility-elements-hidden
aria-valuetextaccessibility-value
aria-selectedthe selected trait
aria-disabledthe disabled trait

aria-checked, aria-expanded, aria-pressed and the rest of ARIA do nothing on a device. Don't test for them: a screen reader won't read them. Use ARIA attributes has the details.

*ByAccessibilityLabel

getByAccessibilityLabel(label: Matcher, options?: MatcherOptions): HTMLElement;
// getAllBy…, queryBy…, queryAllBy…, findBy… and findAllBy… too

Finds elements by their accessibility-label, the text a screen reader reads for them. Testing Library's *ByLabelText reads <label> and aria-* attributes, not Lynx's accessibility-label.

  • label: a string, a RegExp or a function, as for getByText.
  • options: exact: false matches part of the label in any case. normalizer changes how whitespace is trimmed.
  • Variants: the same six as Testing Library's queries. findBy… waits for the element, 1 second by default.

screen

const screen: LynxScreen;

Every query, bound to document.body, with Testing Library's debug() and logTestingPlaygroundURL().

within(element, queries?)

function within(element: HTMLElement): BoundLynxQueries;
function within<Q extends Queries>(
  element: HTMLElement,
  queries: Q,
): BoundFunctions<Q>;

Binds every query to element, to search one part of the tree.

  • Pass queries to bind only those.
  • getQueriesForElement is the same function.

queries

const queries: LynxQueries;

Every query, unbound, each taking the container first. These are Testing Library's, with *ByRole and *ByAccessibilityLabel from this library.

  • Each is also exported on its own, such as getByAccessibilityLabel(container, 'Close').
  • The *ByRole exports are getByRole, getAllByRole, queryByRole, queryAllByRole, findByRole and findAllByRole.
  • The *ByAccessibilityLabel exports follow the same six names.

Text written straight in a view

getByText finds the element whose own text matches, as in a browser. For text written straight in a <view> or <div>, that's the plain <text> the runtime draws it in (see HTML tags). Its parentElement is the <view>.

Types

TypeDescription
LynxQueriesThe type of queries
BoundLynxQueriesEvery query bound to a container, as within() returns them
BoundAccessibilityLabelQueriesThe *ByAccessibilityLabel queries bound to a container
LynxScreenThe type of screen