You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns a locator for form controls associated with the specified label text. This method is ideal for interacting with form elements in an accessible and user-focused way, as it mirrors how users typically identify form fields.
-**Settings pages**: User preferences, account settings, configuration forms
179
-
-**Accessibility testing**: Ensuring proper label association and screen reader compatibility
180
-
181
-
## Best practices
182
-
183
-
1.**Accessibility-first approach**: Using `getByLabel()` ensures your tests work the same way users with assistive technology interact with forms.
184
-
1.**Meaningful labels**: Encourage developers to use descriptive, unique label text that clearly identifies the form control's purpose.
185
-
1.**Required field indicators**: When testing required fields, include any visual indicators (like asterisks) in your label text matching.
186
-
1.**Form validation testing**: Use labels to test form validation scenarios, as they provide a stable way to identify fields regardless of styling changes.
Returns a locator for input elements with the specified `placeholder` attribute. This method is useful for locating form fields that use `placeholder` attribute to provide hints or examples to users about the expected input format.
|`placeholder`| string \| RegExp | - | Required. The placeholder text to search for. Can be a string for exact match or a RegExp for pattern matching. |
13
-
|`options`| object |`null`||
14
-
|`options.exact`| boolean |`false`| Whether to match the placeholder text exactly with case sensitivity. When `true`, performs a case-sensitive match. |
|[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)| A locator object that can be used to interact with the input elements matching the specified placeholder text. |
Returns a locator for elements with the specified ARIA role. This is the preferred way to locate elements as it most closely resembles how users and assistive technology perceive the page.
|`role`| string | - | Required. The ARIA role to search for. For example, `'button'`, `'link'`, `'heading'`, or `'textbox'`. |
13
-
| options | object |`null`||
14
-
|`options.checked`| boolean |`null`| Filter elements by their checked state. Only applicable for roles like `checkbox`, `radio`, `menuitemcheckbox`. |
15
-
|`options.disabled`| boolean |`null`| Filter elements by their disabled state. |
16
-
|`options.exact`| boolean |`false`| Whether to match the accessible name exactly with case sensitivity. When `true`, performs a case-sensitive match. |
17
-
|`options.expanded`| boolean |`null`| Filter elements by their expanded state. Only applicable for roles that support the `aria-expanded` attribute. |
18
-
|`options.includeHidden`| boolean |`false`| Whether to include elements that are normally excluded from the accessibility tree in the search. |
19
-
|`options.level`| number |`null`| Filter headings by their level 1 to 6. Only applicable for `heading` role. |
20
-
|`options.name`| string\|RegExp |`null`| Filter elements by their accessible name. The accessible name is typically the text content, label text, or aria-label attribute. |
21
-
|`options.pressed`| boolean |`null`| Filter elements by their pressed state. Only applicable for roles like `button` with toggle behavior. |
22
-
|`options.selected`| boolean |`null`| Filter elements by their selected state. Only applicable for roles like `option`, `tab`. |
|[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)| A locator object that can be used to interact with the elements matching the specified role and options. |
1.**Prefer role-based selection**: `getByRole()` is the preferred method for locating elements as it closely mirrors how users and assistive technology interact with your page.
214
-
1.**Use accessible names**: Always try to use the `name` option to make your tests more specific and reliable.
215
-
1.**Consider accessibility**: Using `getByRole()` encourages better accessibility practices in your application by ensuring elements have proper ARIA roles.
Returns a locator for elements with the specified test ID attribute. This method is designed for robust test automation by locating elements using dedicated test identifiers that are independent of the visual appearance or content changes. Currently it can only work with the `data-testid` attribute.
|`testId`| string \| RegExp | - | Required. The test ID value to search for. Searches for the `data-testid` attribute. Can be a string for exact match or a RegExp for pattern matching. |
|[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)| A locator object that can be used to interact with the elements matching the specified test ID. |
0 commit comments