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 elements with the specified alt text. This method is useful for locating images and other elements that have an `alt` attribute, making your tests more accessible and user-focused.
|[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)| A locator object that can be used to interact with elements matching the specified alt text. |
|[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 alt text. |
25
-
26
-
### Examples
27
-
28
-
#### Basic alt text matching
24
+
### Basic alt text matching
29
25
30
26
Find and click an image by its alt text:
31
27
@@ -64,7 +60,7 @@ export default async function () {
64
60
}
65
61
```
66
62
67
-
####Exact alt text matching
63
+
### Exact alt text matching
68
64
69
65
Use exact matching for precise alt text:
70
66
@@ -103,7 +99,7 @@ export default async function () {
103
99
}
104
100
```
105
101
106
-
####Using regular expressions
102
+
### Using regular expressions
107
103
108
104
Find images using pattern matching:
109
105
@@ -142,38 +138,29 @@ export default async function () {
142
138
}
143
139
```
144
140
145
-
### Common use cases
146
-
147
-
**Image testing:**
148
-
149
-
- Testing image alt text for accessibility compliance
150
-
- Interacting with clickable images/banners
151
-
152
-
**Accessibility testing:**
153
-
154
-
- Ensuring all images have meaningful alt text
155
-
- Testing screen reader compatibility
156
-
- Validating WCAG compliance
141
+
## Common use cases
157
142
158
-
**UI interaction:**
143
+
-**Image testing:**
144
+
- Testing image alt text for accessibility compliance
145
+
- Interacting with clickable images/banners
146
+
-**Accessibility testing:**
147
+
- Ensuring all images have meaningful alt text
148
+
- Testing screen reader compatibility
149
+
- Validating WCAG compliance
150
+
-**UI interaction:**
151
+
- Clicking on logo images to navigate home
152
+
- Selecting images in galleries or carousels
153
+
- Interacting with image-based buttons
159
154
160
-
- Clicking on logo images to navigate home
161
-
- Selecting images in galleries or carousels
162
-
- Interacting with image-based buttons
163
-
164
-
### Best practices
155
+
## Best practices
165
156
166
157
1.**Use descriptive alt text**: When creating tests, ensure your application uses meaningful alt text that describes the image content or function.
158
+
1.**Prefer exact matches**: Use `exact: true` when you need precise matching to avoid false positives.
159
+
1.**Accessibility-first**: Using `getByAltText()` encourages better accessibility practices by ensuring images have proper alt attributes.
160
+
1.**Regular expressions for patterns**: Use RegExp for flexible matching when dealing with dynamic or numbered content.
161
+
1.**Combine with assertions**: Always verify that the located elements behave as expected using assertions.
167
162
168
-
2.**Prefer exact matches**: Use `exact: true` when you need precise matching to avoid false positives.
169
-
170
-
3.**Accessibility-first**: Using `getByAltText()` encourages better accessibility practices by ensuring images have proper alt attributes.
171
-
172
-
4.**Regular expressions for patterns**: Use RegExp for flexible matching when dealing with dynamic or numbered content.
173
-
174
-
5.**Combine with assertions**: Always verify that the located elements behave as expected using assertions.
175
-
176
-
### Related
163
+
## Related
177
164
178
165
-[page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role
179
166
-[page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels
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.
|[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)| A locator object that can be used to interact with the form control associated with the specified label. |
25
21
26
-
###Examples
22
+
## Examples
27
23
28
-
####Basic form interaction
24
+
### Basic form interaction
29
25
30
26
Fill form fields using their labels:
31
27
@@ -69,7 +65,7 @@ export default async function () {
69
65
}
70
66
```
71
67
72
-
####Working with different input types
68
+
### Working with different input types
73
69
74
70
Handle various form control types in various label association patterns:
75
71
@@ -136,52 +132,49 @@ export default async function () {
136
132
}
137
133
```
138
134
139
-
###Label association patterns
135
+
## Label association patterns
140
136
141
137
The `getByLabel()` method works with several HTML patterns for associating labels with form controls:
-**Settings pages**: User preferences, account settings, configuration forms
172
168
-**Accessibility testing**: Ensuring proper label association and screen reader compatibility
173
169
174
-
###Best practices
170
+
## Best practices
175
171
176
172
1.**Accessibility-first approach**: Using `getByLabel()` ensures your tests work the same way users with assistive technology interact with forms.
173
+
1.**Meaningful labels**: Encourage developers to use descriptive, unique label text that clearly identifies the form control's purpose.
174
+
1.**Required field indicators**: When testing required fields, include any visual indicators (like asterisks) in your label text matching.
175
+
1.**Form validation testing**: Use labels to test form validation scenarios, as they provide a stable way to identify fields regardless of styling changes.
177
176
178
-
2.**Meaningful labels**: Encourage developers to use descriptive, unique label text that clearly identifies the form control's purpose.
179
-
180
-
3.**Required field indicators**: When testing required fields, include any visual indicators (like asterisks) in your label text matching.
181
-
182
-
4.**Form validation testing**: Use labels to test form validation scenarios, as they provide a stable way to identify fields regardless of styling changes.
183
-
184
-
### Related
177
+
## Related
185
178
186
179
-[page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role
187
180
-[page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text
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. |
| placeholder | string\|RegExp | - | Required. The placeholder text to search for. Can be a string for exact match or a RegExp for pattern matching. |
15
-
| options | object |`null`||
16
-
| 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. |
|[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 element(s) matching the specified placeholder text. |
25
-
26
-
### Examples
22
+
## Examples
27
23
28
24
Find and fill inputs by their placeholder text:
29
25
@@ -69,40 +65,32 @@ export default async function () {
69
65
}
70
66
```
71
67
72
-
### Common use cases
73
-
74
-
**Form field identification:**
75
-
76
-
- Login and registration forms without explicit labels
77
-
- Quick search boxes
78
-
- Filter and input controls
79
-
- Comment and feedback forms
80
-
81
-
**E-commerce:**
82
-
83
-
- Product search bars
84
-
- Quantity input fields
85
-
- Promo code entry
86
-
- Address and payment information
87
-
88
-
**Interactive applications:**
89
-
90
-
- Chat input fields
91
-
- Command input interfaces
92
-
- Settings and configuration forms
93
-
- Data entry applications
94
-
95
-
### Best practices
68
+
## Common use cases
69
+
70
+
-**Form field identification:**
71
+
- Login and registration forms without explicit labels
72
+
- Quick search boxes
73
+
- Filter and input controls
74
+
- Comment and feedback forms
75
+
-**E-commerce:**
76
+
- Product search bars
77
+
- Quantity input fields
78
+
- Promo code entry
79
+
- Address and payment information
80
+
-**Interactive applications:**
81
+
- Chat input fields
82
+
- Command input interfaces
83
+
- Settings and configuration forms
84
+
- Data entry applications
85
+
86
+
## Best practices
96
87
97
88
1.**Complement, don't replace labels**: Placeholder text should supplement, not replace proper form labels for accessibility.
89
+
1.**Use descriptive placeholders**: Ensure placeholder text clearly indicates the expected input format or content.
90
+
1.**Consider internationalization**: When testing multi-language applications, be aware that placeholder text may change.
91
+
1.**Accessibility considerations**: Remember that placeholder text alone may not be sufficient for users with disabilities.
98
92
99
-
2.**Use descriptive placeholders**: Ensure placeholder text clearly indicates the expected input format or content.
100
-
101
-
3.**Consider internationalization**: When testing multi-language applications, be aware that placeholder text may change.
102
-
103
-
4.**Accessibility considerations**: Remember that placeholder text alone may not be sufficient for users with disabilities.
104
-
105
-
### Related
93
+
## Related
106
94
107
95
-[page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role
108
96
-[page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text
0 commit comments