Skip to content

Commit

Permalink
Update queryAll example for v0.53
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 committed Oct 7, 2024
1 parent 883417e commit d48a4ea
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ description: 'Browser module: page.$$(selector) method'

{{< admonition type="warning" >}}

Use locator-based [`page.locator(selector)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/locator/) instead.
When possible, use locator-based [`page.locator(selector)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/locator/) instead.

However, working with `locator`s might not be possible when trying to select an element from a list or table if it's difficult to find a stable and unique way to identify the element.

{{< /admonition >}}

The method finds all elements matching the specified selector within the page. If no elements match the selector, the return value resolves to `[]`.
The method finds all elements matching the specified selector within the page. If no elements match the selector, the return value resolves to `[]`. This is particularly useful when you want to retrieve a list of elements, and iterate through them to find the one that you need for your test case.

### Returns

Expand Down Expand Up @@ -43,9 +45,21 @@ export const options = {
export default async function () {
const page = await browser.newPage();

await page.goto('https://test.k6.io/browser.php');
const text = await page.$$('#text1')[0];
await text.type('hello world');
await page.goto('https://test.k6.io/');

// Retrieve all the td elements.
const cells = await page.$$('td');
for (let i = 0; i < cells.length; i++) {
if ((await cells[i].innerText()) == '/pi.php?decimals=3') {
// When the element is found, click on it and
// wait for the navigation.
await Promise.all([page.waitForNavigation(), cells[i].click()]);
break;
}
}

// Wait for an important element to load.
await page.locator('//pre[text()="3.141"]').waitFor();
}
```

Expand Down

0 comments on commit d48a4ea

Please sign in to comment.