Skip to content

Commit 8a5ca50

Browse files
committed
Add page.getByAltText doc
1 parent fd1bf0b commit 8a5ca50

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
title: 'getByAltText(altText[, options])'
3+
description: 'Browser module: page.getByAltText(altText[, options]) method'
4+
---
5+
6+
# getByAltText(altText[, options])
7+
8+
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.
9+
10+
<TableWithNestedRows>
11+
12+
| Parameter | Type | Default | Description |
13+
| ------------- | -------------- | ------- | ---------------------------------------------------------------------------------------------------------- |
14+
| altText | string\|RegExp | - | Required. The alt 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 alt text exactly with case sensitivity. When `true`, performs a case-sensitive match. |
17+
18+
</TableWithNestedRows>
19+
20+
### Returns
21+
22+
| Type | Description |
23+
| -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
24+
| [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
29+
30+
Find and click an image by its alt text:
31+
32+
{{< code >}}
33+
34+
```javascript
35+
import { browser } from 'k6/browser';
36+
37+
export const options = {
38+
scenarios: {
39+
ui: {
40+
executor: 'shared-iterations',
41+
options: {
42+
browser: {
43+
type: 'chromium',
44+
},
45+
},
46+
},
47+
},
48+
};
49+
50+
export default async function () {
51+
const page = await browser.newPage();
52+
53+
try {
54+
await page.goto('https://quickpizza.grafana.com');
55+
56+
const logo = page.getByAltText('LOGO');
57+
await logo.waitFor();
58+
59+
await logo.click();
60+
await page.waitForLoadState();
61+
} finally {
62+
await page.close();
63+
}
64+
}
65+
```
66+
67+
{{< /code >}}
68+
69+
#### Exact alt text matching
70+
71+
Use exact matching for precise alt text:
72+
73+
{{< code >}}
74+
75+
```javascript
76+
import { browser } from 'k6/browser';
77+
78+
export const options = {
79+
scenarios: {
80+
ui: {
81+
executor: 'shared-iterations',
82+
options: {
83+
browser: {
84+
type: 'chromium',
85+
},
86+
},
87+
},
88+
},
89+
};
90+
91+
export default async function () {
92+
const page = await browser.newPage();
93+
94+
try {
95+
await page.goto('https://quickpizza.grafana.com');
96+
97+
const logo = page.getByAltText('logo', { exact: true });
98+
await logo.waitFor();
99+
100+
await logo.click();
101+
await page.waitForLoadState();
102+
} finally {
103+
await page.close();
104+
}
105+
}
106+
```
107+
108+
{{< /code >}}
109+
110+
#### Using regular expressions
111+
112+
Find images using pattern matching:
113+
114+
{{< code >}}
115+
116+
```javascript
117+
import { browser } from 'k6/browser';
118+
119+
export const options = {
120+
scenarios: {
121+
ui: {
122+
executor: 'shared-iterations',
123+
options: {
124+
browser: {
125+
type: 'chromium',
126+
},
127+
},
128+
},
129+
},
130+
};
131+
132+
export default async function () {
133+
const page = await browser.newPage();
134+
135+
try {
136+
await page.goto('https://quickpizza.grafana.com');
137+
138+
const logo = page.getByAltText(/logo/s);
139+
await logo.waitFor();
140+
141+
await logo.click();
142+
await page.waitForLoadState();
143+
} finally {
144+
await page.close();
145+
}
146+
}
147+
```
148+
149+
{{< /code >}}
150+
151+
### Common use cases
152+
153+
**Image testing:**
154+
155+
- Testing image alt text for accessibility compliance
156+
- Interacting with clickable images/banners
157+
158+
**Accessibility testing:**
159+
160+
- Ensuring all images have meaningful alt text
161+
- Testing screen reader compatibility
162+
- Validating WCAG compliance
163+
164+
**UI interaction:**
165+
166+
- Clicking on logo images to navigate home
167+
- Selecting images in galleries or carousels
168+
- Interacting with image-based buttons
169+
170+
### Best practices
171+
172+
1. **Use descriptive alt text**: When creating tests, ensure your application uses meaningful alt text that describes the image content or function.
173+
174+
2. **Prefer exact matches**: Use `exact: true` when you need precise matching to avoid false positives.
175+
176+
3. **Accessibility-first**: Using `getByAltText()` encourages better accessibility practices by ensuring images have proper alt attributes.
177+
178+
4. **Regular expressions for patterns**: Use RegExp for flexible matching when dealing with dynamic or numbered content.
179+
180+
5. **Combine with assertions**: Always verify that the located elements behave as expected using assertions.
181+
182+
### Related
183+
184+
- [page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role
185+
- [page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels
186+
- [page.getByPlaceholder()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text
187+
- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID
188+
- [page.getByTitle()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute
189+
- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by visible text

0 commit comments

Comments
 (0)