Skip to content

Commit 9c0157d

Browse files
committed
Add page.getByRole docs page
1 parent e58b708 commit 9c0157d

File tree

1 file changed

+235
-0
lines changed
  • docs/sources/k6/next/javascript-api/k6-browser/page

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
title: 'getByRole(role[, options])'
3+
description: 'Browser module: page.getByRole(role[, options]) method'
4+
---
5+
6+
# getByRole(role[, options])
7+
8+
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.
9+
10+
<TableWithNestedRows>
11+
12+
| Parameter | Type | Default | Description |
13+
| --------------------- | -------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------- |
14+
| role | string | - | Required. The ARIA role to search for (e.g. `'button'`, `'link'`, `'heading'`, `'textbox'`, etc.). |
15+
| options | object | `null` | |
16+
| options.checked | boolean | `null` | Filter elements by their checked state. Only applicable for roles like `checkbox`, `radio`, `menuitemcheckbox`. |
17+
| options.disabled | boolean | `null` | Filter elements by their disabled state. |
18+
| options.exact | boolean | `false` | Whether to match the accessible name exactly with case sensitivity. When `true`, performs a case-sensitive match. |
19+
| options.expanded | boolean | `null` | Filter elements by their expanded state. Only applicable for roles that support the `aria-expanded` attribute. |
20+
| options.includeHidden | boolean | `false` | Whether to include elements that are normally excluded from the accessibility tree in the search. |
21+
| options.level | number | `null` | Filter headings by their level 1 to 6. Only applicable for `heading` role. |
22+
| 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. |
23+
| options.pressed | boolean | `null` | Filter elements by their pressed state. Only applicable for roles like `button` with toggle behavior. |
24+
| options.selected | boolean | `null` | Filter elements by their selected state. Only applicable for roles like `option`, `tab`. |
25+
26+
</TableWithNestedRows>
27+
28+
### Returns
29+
30+
| Type | Description |
31+
| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
32+
| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | A locator object that can be used to interact with the element(s) matching the specified role and options. |
33+
34+
### Examples
35+
36+
#### Basic role selection
37+
38+
Find and click a button by its role:
39+
40+
{{< code >}}
41+
42+
```javascript
43+
import { browser } from 'k6/browser';
44+
45+
export const options = {
46+
scenarios: {
47+
browser: {
48+
executor: 'shared-iterations',
49+
options: {
50+
browser: {
51+
type: 'chromium',
52+
},
53+
},
54+
},
55+
},
56+
};
57+
58+
export default async function () {
59+
const page = await browser.newPage();
60+
61+
try {
62+
await page.goto('https://quickpizza.grafana.com/');
63+
64+
await page.getByRole('button', { name: 'Pizza, Please!' }).click();
65+
} finally {
66+
page.close();
67+
}
68+
}
69+
```
70+
71+
{{< /code >}}
72+
73+
### Complete ARIA roles reference
74+
75+
The following roles are supported and can be used with `getByRole()`, organized by category:
76+
77+
#### Widgets & Inputs
78+
79+
- `button` - Buttons and clickable elements
80+
- `checkbox` - Checkable boxes that can be on/off
81+
- `combobox` - Editable dropdown combining input and listbox
82+
- `listbox` - Container for selectable list options
83+
- `menu` - Menu of actions or navigation
84+
- `menubar` - Container for top-level menus
85+
- `menuitem` - Option within a menu
86+
- `menuitemcheckbox` - Checkable menu item
87+
- `menuitemradio` - Mutually exclusive menu item
88+
- `meter` - Scalar measurement within a known range
89+
- `option` - Selectable option in a listbox or combobox
90+
- `progressbar` - Progress indicator of an operation
91+
- `radio` - Single-select option in a group
92+
- `radiogroup` - Grouping of related radio buttons
93+
- `scrollbar` - Control for scrolling content
94+
- `searchbox` - Text field for search input
95+
- `separator` - Divider that separates content or controls
96+
- `slider` - Adjustable value control within a range
97+
- `spinbutton` - Numeric input with increment/decrement controls
98+
- `status` - Advisory status information (non-alert)
99+
- `switch` - On/off toggle control
100+
- `tab` - A tab in a tabbed interface
101+
- `tablist` - Container for a set of tabs
102+
- `tabpanel` - Content panel associated with a tab
103+
- `textbox` - Input field for free-form text
104+
- `timer` - Running time display that updates
105+
- `toolbar` - Group of interactive controls
106+
- `tooltip` - Contextual help popup
107+
- `tree` - Hierarchical list of items
108+
- `treeitem` - Item in a tree
109+
110+
#### Tables & Grids
111+
112+
- `table` - Tabular data with rows and columns
113+
- `rowgroup` - Section that groups rows (thead, tbody, tfoot)
114+
- `row` - A row of cells within a table or grid
115+
- `rowheader` - Header cell for a row
116+
- `columnheader` - Header cell for a column
117+
- `cell` - Data cell in a table
118+
- `grid` - Interactive, tabular widget (like a spreadsheet)
119+
- `gridcell` - Cell within a grid
120+
- `treegrid` - Tree-structured grid with expandable rows
121+
122+
#### Document Structure & Semantics
123+
124+
- `article` - Self-contained composition (article)
125+
- `blockquote` - Quotation from another source
126+
- `caption` - Caption for a figure, table, or media
127+
- `code` - Fragment of computer code
128+
- `definition` - Definition of a term
129+
- `deletion` - Content removed from a document
130+
- `directory` - List of references
131+
- `document` - Standalone document content
132+
- `emphasis` - Content with emphatic stress
133+
- `feed` - Stream of articles or entries
134+
- `figure` - Figure with optional caption
135+
- `generic` - Generic container with no specific semantics
136+
- `img` - Image treated as a single graphic
137+
- `insertion` - Content added to a document
138+
- `link` - Hyperlink to another location
139+
- `list` - List of items
140+
- `listitem` - A single item within a list
141+
- `mark` - Highlighted or marked content
142+
- `marquee` - Scrolling region of text
143+
- `math` - Mathematical expression
144+
- `note` - An aside or annotation
145+
- `none` - No semantic role; remove semantics
146+
- `paragraph` - Paragraph of text
147+
- `presentation` - Presentational only; ignore semantics
148+
- `strong` - Content of strong importance
149+
- `subscript` - Subscripted text
150+
- `superscript` - Superscripted text
151+
- `term` - A term being defined
152+
- `time` - A time or date
153+
154+
#### Landmarks & Regions
155+
156+
- `banner` - Site header landmark
157+
- `complementary` - Complementary content (sidebar)
158+
- `contentinfo` - Page footer information
159+
- `form` - Region containing a form
160+
- `main` - Main content landmark
161+
- `navigation` - Navigation region of links
162+
- `region` - Generic region of the page
163+
- `search` - Search region
164+
- `application` - Application container widget
165+
166+
#### Usage Examples by Category
167+
168+
**Form Testing:**
169+
{{< code >}}
170+
171+
<!-- eslint-skip -->
172+
173+
```javascript
174+
// Text inputs
175+
await page.getByRole('textbox', { name: 'Email' }).fill('[email protected]');
176+
await page.getByRole('searchbox', { name: 'Search products' }).fill('laptop');
177+
178+
// Selections
179+
await page.getByRole('checkbox', { name: 'Agree to terms' }).check();
180+
await page.getByRole('radio', { name: 'Standard shipping' }).check();
181+
await page.getByRole('combobox', { name: 'Country' }).selectOption('US');
182+
183+
// Interactive controls
184+
await page.getByRole('slider', { name: 'Volume' }).fill('75');
185+
await page.getByRole('switch', { name: 'Enable notifications' }).click();
186+
```
187+
188+
{{< /code >}}
189+
190+
**Navigation Testing:**
191+
{{< code >}}
192+
193+
<!-- eslint-skip -->
194+
195+
```javascript
196+
// Tabs
197+
await page.getByRole('tab', { name: 'Overview' }).click();
198+
await expect(page.getByRole('tabpanel', { name: 'Overview' })).toBeVisible();
199+
```
200+
201+
{{< /code >}}
202+
203+
**Content Verification:**
204+
{{< code >}}
205+
206+
<!-- eslint-skip -->
207+
208+
```javascript
209+
// Structure
210+
await expect(page.getByRole('article')).toHaveCount(5);
211+
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Welcome');
212+
213+
// Status and feedback
214+
await expect(page.getByRole('alert')).toHaveText('Form submitted successfully');
215+
await expect(page.getByRole('status')).toHaveText('3 items selected');
216+
```
217+
218+
{{< /code >}}
219+
220+
### Best practices
221+
222+
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.
223+
224+
2. **Use accessible names**: Always try to use the `name` option to make your tests more specific and reliable.
225+
226+
3. **Consider accessibility**: Using `getByRole()` encourages better accessibility practices in your application by ensuring elements have proper ARIA roles.
227+
228+
### Related
229+
230+
- [page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text
231+
- [page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels
232+
- [page.getByPlaceholder()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text
233+
- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID
234+
- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by text content
235+
- [page.getByTitle()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute

0 commit comments

Comments
 (0)