> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# 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

```typescript
const { getByRole } = await render(Counter);

getByRole('button', { name: 'Add one' }); // bound to render()'s container
screen.getByText('Taps: 0'); // bound to document.body
```

- **[`render()`](/api/testing-library/render.md#renderresult)'s result** has every query, bound to its `container`.
- **[`screen`](#screen)** has them bound to `document.body`.
- **[`within()`](#withinelement-queries)** binds them to one element.

## `*ByRole`

```typescript
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-traits` | Role        |
| ---------------------- | ----------- |
| `button`               | `button`    |
| `link`                 | `link`      |
| `image`                | `img`       |
| `header`               | `heading`   |
| `search`               | `searchbox` |
| `adjustable`           | `slider`    |
| `tabbar`               | `tablist`   |

- **`<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 template      | Becomes                            |
| -------------------- | ---------------------------------- |
| `role`               | traits and `accessibility-element` |
| `aria-label`         | `accessibility-label`              |
| `aria-hidden="true"` | `accessibility-elements-hidden`    |
| `aria-valuetext`     | `accessibility-value`              |
| `aria-selected`      | the `selected` trait               |
| `aria-disabled`      | the `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](/guide/accessibility.md#use-aria-attributes) has the details.

## `*ByAccessibilityLabel`

```typescript
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`

```typescript
const screen: LynxScreen;
```

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

## `within(element, queries?)`

```typescript
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`

```typescript
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](/guide/elements/index.md#html-tags)). Its `parentElement` is the `<view>`.

## Types

| Type                             | Description                                                  |
| -------------------------------- | ------------------------------------------------------------ |
| `LynxQueries`                    | The type of [`queries`](#queries)                            |
| `BoundLynxQueries`               | Every query bound to a container, as `within()` returns them |
| `BoundAccessibilityLabelQueries` | The `*ByAccessibilityLabel` queries bound to a container     |
| `LynxScreen`                     | The type of [`screen`](#screen)                              |

## Related

- [Testing](/guide/testing/index.md)
- [Accessibility](/guide/accessibility.md)
- [`render()`](/api/testing-library/render.md)
- [`fireEvent`](/api/testing-library/fire-event.md)
