Skip to content

Commit 30bade7

Browse files
committed
Add locator.locator doc for k6 browser
1 parent b79c90f commit 30bade7

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Locator can be created with the [page.locator(selector[, options])](https://graf
4040
| [isHidden()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/ishidden) | Checks if the element is `hidden`. |
4141
| [isVisible()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/isvisible) | Checks if the element is `visible`. |
4242
| [last()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/last) | Returns a `locator` to the last matching element. |
43+
| [locator(selector)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/locator) | Creates a nested locator relative to the current locator, allowing for more precise element targeting by chaining selectors. |
4344
| [nth()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/nth) | Returns a `locator` to the n-th matching element. |
4445
| [press(key, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/press) | Press a single key on the keyboard or a combination of keys. |
4546
| [selectOption(values, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/selectoption) {{< docs/bwipt id="470" >}} | Select one or more options which match the values. |
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: 'locator(selector)'
3+
description: 'Browser module: locator.locator method'
4+
---
5+
6+
# locator(selector)
7+
8+
The method finds all elements matching the selector and creates a new [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) that matches all of them. This method can be used to further refine the locator by chaining additional selectors.
9+
10+
This allows you to define locators relative to a parent locator, enabling more precise element targeting by creating nested locators.
11+
12+
### Returns
13+
14+
| Type | Description |
15+
| -------------------------------------------------------------------------------------- | -------------------------------------------------------- |
16+
| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | The new `Locator` associated with the selector. |
17+
18+
### Example
19+
20+
{{< code >}}
21+
22+
```javascript
23+
import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js';
24+
import { browser } from 'k6/browser';
25+
26+
export const options = {
27+
scenarios: {
28+
ui: {
29+
executor: 'shared-iterations',
30+
options: {
31+
browser: {
32+
type: 'chromium',
33+
},
34+
},
35+
},
36+
},
37+
};
38+
39+
export default async function () {
40+
const page = await browser.newPage();
41+
42+
try {
43+
await page.setContent(`
44+
<div>
45+
<div data-testid="products">
46+
<div data-category="fruits">
47+
<div data-product="apple">
48+
<h3>Apple</h3>
49+
<button>Add to Cart</button>
50+
<button>View Details</button>
51+
</div>
52+
</div>
53+
<div data-category="fruits">
54+
<h3>Orange</h3>
55+
<button>Add to Cart</button>
56+
<button>View Details</button>
57+
</div>
58+
<div data-category="vegetables">
59+
<h3>Carrot</h3>
60+
<button>Add to Cart</button>
61+
<button>View Details</button>
62+
</div>
63+
</div>
64+
</div>
65+
`);
66+
67+
// Use locator.locator to find specific products within the list
68+
const productList = page.locator('div[data-testid="products"]');
69+
const fruitProducts = productList.locator('div[data-category="fruits"]');
70+
const appleProduct = fruitProducts.locator('div[data-product="apple"]');
71+
const addToCartButton = appleProduct.locator('//button[text()="Add to Cart"]');
72+
73+
// Interact with the nested locators
74+
await addToCartButton.click();
75+
} finally {
76+
await page.close();
77+
}
78+
}
79+
```
80+
81+
{{< /code >}}

0 commit comments

Comments
 (0)