Skip to content

Commit 3ae0bcd

Browse files
committed
move every getBy methods to shared folder
1 parent a9b1165 commit 3ae0bcd

24 files changed

+464
-750
lines changed

docs/sources/k6/next/javascript-api/k6-browser/framelocator/getbylabel.md

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ title: 'getByLabel(text[, options])'
33
description: 'Browser module: frameLocator.getByLabel(text[, options]) method'
44
---
55

6-
# getByLabel(text[, options])
7-
8-
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.
9-
10-
| Parameter | Type | Default | Description |
11-
| --------------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------ |
12-
| `text` | string \| RegExp | - | Required. The label 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 label text exactly with case sensitivity. When `true`, performs a case-sensitive match. |
6+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbylabel-spec.md" version="<K6_VERSION>" >}}
157

168
## Returns
179

@@ -143,47 +135,7 @@ export default async function () {
143135
}
144136
```
145137

146-
## Label association patterns
147-
148-
The `getByLabel()` method works with several HTML patterns for associating labels with form controls:
149-
150-
1. Explicit association with `for` attribute:
151-
152-
<!-- eslint-skip -->
153-
154-
```html
155-
<label for="username">Username</label> <input type="text" id="username" name="username" />
156-
```
157-
158-
1. ARIA labeling:
159-
160-
<!-- eslint-skip -->
161-
162-
```html
163-
<span id="username-label">Username</span> <input type="text" aria-labelledby="username-label" />
164-
```
165-
166-
1. ARIA label attribute:
167-
168-
<!-- eslint-skip -->
169-
170-
```html
171-
<input type="text" aria-label="Username" />
172-
```
173-
174-
## Common use cases
175-
176-
- **Form testing**: Login forms, registration forms, contact forms
177-
- **E-commerce**: Checkout forms, shipping information, payment details
178-
- **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.
138+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbylabel-tips.md" version="<K6_VERSION>" >}}
187139

188140
## Related
189141

docs/sources/k6/next/javascript-api/k6-browser/framelocator/getbyplaceholder.md

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,7 @@ title: 'getByPlaceholder(placeholder[, options])'
33
description: 'Browser module: frameLocator.getByPlaceholder(placeholder[, options]) method'
44
---
55

6-
# getByPlaceholder(placeholder[, options])
7-
8-
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.
9-
10-
| Parameter | Type | Default | Description |
11-
| --------------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------ |
12-
| `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. |
15-
16-
## Returns
17-
18-
| Type | Description |
19-
| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
20-
| [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. |
6+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbyplaceholder-spec.md" version="<K6_VERSION>" >}}
217

228
## Example
239

@@ -70,30 +56,7 @@ export default async function () {
7056
}
7157
```
7258

73-
## Common use cases
74-
75-
- **Form field identification:**
76-
- Login and registration forms without explicit labels
77-
- Quick search boxes
78-
- Filter and input controls
79-
- Comment and feedback forms
80-
- **E-commerce:**
81-
- Product search bars
82-
- Quantity input fields
83-
- Promo code entry
84-
- Address and payment information
85-
- **Interactive applications:**
86-
- Chat input fields
87-
- Command input interfaces
88-
- Settings and configuration forms
89-
- Data entry applications
90-
91-
## Best practices
92-
93-
1. **Complement, don't replace labels**: Placeholder text should supplement, not replace proper form labels for accessibility.
94-
1. **Use descriptive placeholders**: Ensure placeholder text clearly indicates the expected input format or content.
95-
1. **Consider internationalization**: When testing multi-language applications, be aware that placeholder text may change.
96-
1. **Accessibility considerations**: Remember that placeholder text alone may not be sufficient for users with disabilities.
59+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbyplaceholder-tips.md" version="<K6_VERSION>" >}}
9760

9861
## Related
9962

docs/sources/k6/next/javascript-api/k6-browser/framelocator/getbyrole.md

Lines changed: 2 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,7 @@ title: 'getByRole(role[, options])'
33
description: 'Browser module: frameLocator.getByRole(role[, options]) method'
44
---
55

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-
| Parameter | Type | Default | Description |
11-
| ----------------------- | -------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------- |
12-
| `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`. |
23-
24-
## Returns
25-
26-
| Type | Description |
27-
| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
28-
| [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. |
6+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbyrole-spec.md" version="<K6_VERSION>" >}}
297

308
## Examples
319

@@ -67,152 +45,7 @@ export default async function () {
6745
}
6846
```
6947

70-
## Complete ARIA roles reference
71-
72-
The following roles are supported and can be used with `getByRole()`, organized by category:
73-
74-
### Widgets & Inputs
75-
76-
- `button` - Buttons and clickable elements
77-
- `checkbox` - Checkable boxes that can be on/off
78-
- `combobox` - Editable drop-down menu combining input and list box
79-
- `listbox` - Container for selectable list options
80-
- `menu` - Menu of actions or navigation
81-
- `menubar` - Container for top-level menus
82-
- `menuitem` - Option within a menu
83-
- `menuitemcheckbox` - Checkable menu item
84-
- `menuitemradio` - Mutually exclusive menu item
85-
- `meter` - Scalar measurement within a known range
86-
- `option` - Selectable option in a list box or combo box
87-
- `progressbar` - Progress indicator of an operation
88-
- `radio` - Single-select option in a group
89-
- `radiogroup` - Grouping of related radio buttons
90-
- `scrollbar` - Control for scrolling content
91-
- `searchbox` - Text field for search input
92-
- `separator` - Divider that separates content or controls
93-
- `slider` - Adjustable value control within a range
94-
- `spinbutton` - Numeric input with increment/decrement controls
95-
- `status` - Advisory status information (non-alert)
96-
- `switch` - On/off toggle control
97-
- `tab` - A tab in a tabbed interface
98-
- `tablist` - Container for a set of tabs
99-
- `tabpanel` - Content panel associated with a tab
100-
- `textbox` - Input field for free-form text
101-
- `timer` - Running time display that updates
102-
- `toolbar` - Group of interactive controls
103-
- `tooltip` - Contextual help popup
104-
- `tree` - Hierarchical list of items
105-
- `treeitem` - Item in a tree
106-
107-
### Tables & Grids
108-
109-
- `table` - Tabular data with rows and columns.
110-
- `rowgroup` - Section that groups rows. For example, `thead`, `tbody`, `tfoot`.
111-
- `row` - A row of cells within a table or grid.
112-
- `rowheader` - Header cell for a row.
113-
- `columnheader` - Header cell for a column.
114-
- `cell` - Data cell in a table.
115-
- `grid` - Interactive, tabular widget, such as a spreadsheet.
116-
- `gridcell` - Cell within a grid.
117-
- `treegrid` - Tree-structured grid with expandable rows.
118-
119-
### Document Structure & Semantics
120-
121-
- `article` - Self-contained composition (article)
122-
- `blockquote` - Quotation from another source
123-
- `caption` - Caption for a figure, table, or media
124-
- `code` - Fragment of computer code
125-
- `definition` - Definition of a term
126-
- `deletion` - Content removed from a document
127-
- `directory` - List of references
128-
- `document` - Standalone document content
129-
- `emphasis` - Content with emphatic stress
130-
- `feed` - Stream of articles or entries
131-
- `figure` - Figure with optional caption
132-
- `generic` - Generic container with no specific semantics
133-
- `img` - Image treated as a single graphic
134-
- `insertion` - Content added to a document
135-
- `link` - Hyperlink to another location
136-
- `list` - List of items
137-
- `listitem` - A single item within a list
138-
- `mark` - Highlighted or marked content
139-
- `marquee` - Scrolling region of text
140-
- `math` - Mathematical expression
141-
- `note` - An aside or annotation
142-
- `none` - No semantic role; remove semantics
143-
- `paragraph` - Paragraph of text
144-
- `presentation` - Presentational only; ignore semantics
145-
- `strong` - Content of strong importance
146-
- `subscript` - Subscripted text
147-
- `superscript` - Superscripted text
148-
- `term` - A term being defined
149-
- `time` - A time or date
150-
151-
### Landmarks & Regions
152-
153-
- `banner` - Site header landmark
154-
- `complementary` - Complementary content (sidebar)
155-
- `contentinfo` - Page footer information
156-
- `form` - Region containing a form
157-
- `main` - Main content landmark
158-
- `navigation` - Navigation region of links
159-
- `region` - Generic region of the page
160-
- `search` - Search region
161-
- `application` - Application container widget
162-
163-
### Usage Examples by Category
164-
165-
- **Form Testing:**
166-
167-
<!-- md-k6:skip -->
168-
<!-- eslint-skip -->
169-
170-
```javascript
171-
// Text inputs
172-
await frameLocator.getByRole('textbox', { name: 'Email' }).fill('[email protected]');
173-
await frameLocator.getByRole('searchbox', { name: 'Search products' }).fill('laptop');
174-
175-
// Selections
176-
await frameLocator.getByRole('checkbox', { name: 'Agree to terms' }).check();
177-
await frameLocator.getByRole('radio', { name: 'Standard shipping' }).check();
178-
await frameLocator.getByRole('combobox', { name: 'Country' }).selectOption('US');
179-
180-
// Interactive controls
181-
await frameLocator.getByRole('slider', { name: 'Volume' }).fill('75');
182-
await frameLocator.getByRole('switch', { name: 'Enable notifications' }).click();
183-
```
184-
185-
- **Navigation Testing:**
186-
187-
<!-- md-k6:skip -->
188-
<!-- eslint-skip -->
189-
190-
```javascript
191-
// Tabs
192-
await frameLocator.getByRole('tab', { name: 'Overview' }).click();
193-
await expect(frameLocator.getByRole('tabpanel', { name: 'Overview' })).toBeVisible();
194-
```
195-
196-
- **Content Verification:**
197-
198-
<!-- md-k6:skip -->
199-
<!-- eslint-skip -->
200-
201-
```javascript
202-
// Structure
203-
await expect(frameLocator.getByRole('article')).toHaveCount(5);
204-
await expect(frameLocator.getByRole('heading', { level: 1 })).toHaveText('Welcome');
205-
206-
// Status and feedback
207-
await expect(frameLocator.getByRole('alert')).toHaveText('Form submitted successfully');
208-
await expect(frameLocator.getByRole('status')).toHaveText('3 items selected');
209-
```
210-
211-
## Best practices
212-
213-
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.
48+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbyrole-tips.md" version="<K6_VERSION>" >}}
21649

21750
## Related
21851

docs/sources/k6/next/javascript-api/k6-browser/framelocator/getbytestid.md

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@ title: 'getByTestId(testId)'
33
description: 'Browser module: frameLocator.getByTestId(testId) method'
44
---
55

6-
# getByTestId(testId)
7-
8-
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.
9-
10-
| Parameter | Type | Default | Description |
11-
| --------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
12-
| `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. |
13-
14-
## Returns
15-
16-
| Type | Description |
17-
| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
18-
| [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. |
6+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbytestid-spec.md" version="<K6_VERSION>" >}}
197

208
## Examples
219

@@ -65,13 +53,7 @@ export default async function () {
6553
}
6654
```
6755

68-
## Best practices
69-
70-
1. **Stable identifiers**: Use meaningful, stable test IDs that won't change with refactoring or content updates.
71-
1. **Hierarchical naming**: Use consistent naming conventions like `user-profile-edit-btn`.
72-
1. **Avoid duplicates**: Ensure test IDs are unique within the frame to prevent ambiguity.
73-
1. **Strategic placement**: Add test IDs to key interactive elements and components that are frequently tested.
74-
1. **Team coordination**: Establish test ID conventions with your development team to ensure consistency.
56+
{{< docs/shared source="k6" lookup="browser/getby-apis/getbytestid-tips.md" version="<K6_VERSION>" >}}
7557

7658
## Related
7759

0 commit comments

Comments
 (0)