Skip to content

Commit 2b175d7

Browse files
committed
Add locator.locator() method documentation
1 parent 4ca180e commit 2b175d7

File tree

1 file changed

+26
-10
lines changed
  • docs/sources/k6/next/javascript-api/k6-browser/locator

1 file changed

+26
-10
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
---
2-
title: 'locator(selector)'
3-
description: 'Browser module: locator.locator method'
2+
title: 'locator(selector[, options])'
3+
description: 'Browser module: locator.locator(selector[, options]) method'
44
---
55

6-
# locator(selector)
6+
# locator(selector[, options])
77

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.
8+
Creates and returns a new locator chained/relative to the current locator. Locators resolve to the element when the action takes place, which means locators can span over navigations where the underlying dom changes.
99

1010
This allows you to define locators relative to a parent locator, enabling more precise element targeting by creating nested locators.
1111

12+
<TableWithNestedRows>
13+
14+
| Parameter | Type | Default | Description |
15+
| ------------------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
16+
| selector | string | `''` | A selector to use when resolving DOM element. |
17+
| options | object | `null` | |
18+
| options.hasText | string or RegExp | `null` | Matches only elements that contain the specified text. String or regular expression. Optional. |
19+
| options.hasNotText | string or RegExp | `null` | Matches only elements that do not contain the specified text. String or regular expression. Optional. |
20+
21+
</TableWithNestedRows>
22+
1223
### Returns
1324

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. |
25+
| Type | Description |
26+
| -------------------------------------------------------------------------------------- | --------------------------------------------------------- |
27+
| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | A new chained `Locator` that can be used for further actions. |
1728

1829
### Example
1930

@@ -38,7 +49,7 @@ export const options = {
3849

3950
export default async function () {
4051
const page = await browser.newPage();
41-
52+
4253
try {
4354
await page.setContent(`
4455
<div>
@@ -63,13 +74,18 @@ export default async function () {
6374
</div>
6475
</div>
6576
`);
66-
77+
6778
// Use locator.locator to find specific products within the list
6879
const appleProduct = page.locator('div[data-product="apple"]');
6980
const addToCartButton = appleProduct.locator('//button[text()="Add to Cart"]');
70-
81+
82+
// Use locator.locator with options to find specific items
83+
const fruitsSection = page.locator('div[data-category="fruits"]');
84+
const orangeButton = fruitsSection.locator('button', { hasText: 'Add to Cart' });
85+
7186
// Interact with the nested locators
7287
await addToCartButton.click();
88+
await orangeButton.click();
7389
} finally {
7490
await page.close();
7591
}

0 commit comments

Comments
 (0)