Skip to content

Commit cd1309f

Browse files
authored
Merge pull request #1986 from grafana/browser/locator-all
k6 browser: add locator.all
2 parents 93ca95c + 0e393d8 commit cd1309f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

docs/sources/k6/next/javascript-api/k6-browser/locator/_index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ The Locator API makes it easier to work with dynamically changing elements. Some
1212
- Helps with writing robust tests by finding an element even if the underlying frame navigates.
1313
- Makes it easier to work with dynamic web pages and SPAs built with Svelte, React, Vue, etc.
1414
- Enables the use of test abstractions like the Page Object Model (POM) pattern to simplify and organize tests.
15-
- `strict` mode is enabled for all `locator` methods, which means that if more than one element matches the given selector it will throw an error.
15+
- `strict` mode is enabled for all `locator` methods that are expected to target a single DOM element, meaning that if more than one element matches the given selector, an error will be thrown.
1616

1717
Locator can be created with the [page.locator(selector[, options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/locator) method.
1818

1919
| Method | Description |
2020
| ------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
21+
| [all()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/all) | When multiple elements match the selector, returns an array of `locator`. |
2122
| [check([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/check) {{< docs/bwipt id="471" >}} | Select the input checkbox. |
2223
| [clear([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/clear) | Clears text boxes and input fields of any existing values. |
2324
| [click([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/click) {{< docs/bwipt id="471" >}} | Mouse click on the chosen element. |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: 'all()'
3+
description: 'Browser module: locator.all method'
4+
---
5+
6+
# all()
7+
8+
When multiple elements match the selector, returns an array of [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/), each pointing to their respective element.
9+
10+
### Returns
11+
12+
| Type | Description |
13+
| -------------------------------------------------------------------------------------- | -------------------------------------------------------- |
14+
| Array<[Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/)> | An array of `Locator`. |
15+
16+
### Example
17+
18+
{{< code >}}
19+
20+
```javascript
21+
import { browser } from 'k6/browser';
22+
23+
export const options = {
24+
scenarios: {
25+
ui: {
26+
executor: 'shared-iterations',
27+
options: {
28+
browser: {
29+
type: 'chromium',
30+
},
31+
},
32+
},
33+
},
34+
};
35+
36+
export default async function () {
37+
const page = await browser.newPage();
38+
await page.goto('https://quickpizza.grafana.com/browser.php');
39+
40+
const options = await page.locator('#numbers-options > option').all();
41+
console.log(`Found ${options.length} options.`);
42+
43+
for (const option of options) {
44+
console.log('Inner text: ', await option.innerText());
45+
await option.click();
46+
}
47+
48+
await page.close();
49+
}
50+
```
51+
52+
{{< /code >}}

0 commit comments

Comments
 (0)