Skip to content

Commit d01c406

Browse files
committed
browser: add locator.evaluate and evaluateHandle
1 parent 53a357f commit d01c406

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Locator can be created with the [page.locator(selector[, options])](https://graf
2626
| [count()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/count) | Returns the number of elements matching the selector. |
2727
| [dblclick([options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/dblclick) {{< docs/bwipt id="471" >}} | Mouse double click on the chosen element. |
2828
| [dispatchEvent(type, eventInit, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/dispatchevent) | Dispatches HTML DOM event types e.g. `'click'`. |
29+
| [evaluate(pageFunction[, arg])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/evaluate) | Returns the value of the `pageFunction` invocation, called with the matching element as first argument. |
30+
| [evaluateHandle(pageFunction[, arg])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/evaluatehandle) | Returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle). |
2931
| [fill(value, [options])](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/fill) | Fill an `input`, `textarea` or `contenteditable` element with the provided value. |
3032
| [filter(options)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/filter) | Returns a new `locator` that matches only elements containing or excluding specified text. |
3133
| [first()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/first) | Returns a `locator` to the first matching element. |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: 'evaluate(pageFunction[, arg])'
3+
description: 'Browser module: locator.evaluate(pageFunction[, arg]) method'
4+
---
5+
6+
# evaluate(pageFunction[, arg])
7+
8+
Returns the value of the `pageFunction` invocation. It passes the matching element of the locator as the first argument to the `pageFunction` and arg as a second argument.
9+
10+
<TableWithNestedRows>
11+
12+
| Parameter | Type | Defaults | Description |
13+
| ------------ | ------------------ | -------- | -------------------------------------------- |
14+
| pageFunction | function or string | | Function to be evaluated. |
15+
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
16+
17+
</TableWithNestedRows>
18+
19+
### Returns
20+
21+
| Type | Description |
22+
| ------------ | ----------------------------------- |
23+
| Promise<any> | The return value of `pageFunction`. |
24+
25+
### Example
26+
27+
{{< code >}}
28+
29+
<!-- eslint-skip -->
30+
31+
```javascript
32+
import { browser } from 'k6/browser';
33+
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
34+
35+
export const options = {
36+
scenarios: {
37+
browser: {
38+
executor: 'shared-iterations',
39+
options: {
40+
browser: {
41+
type: 'chromium',
42+
},
43+
},
44+
},
45+
},
46+
};
47+
48+
export default async function () {
49+
const page = await browser.newPage();
50+
51+
try {
52+
await page.goto("https://quickpizza.grafana.com", { waitUntil: "load" });
53+
54+
await page.getByText('Pizza, Please!').click();
55+
56+
await check(page, {
57+
'pizza name': async p => {
58+
const n = await p.locator('#pizza-name').evaluate((pizzaName, extra) => pizzaName.textContent + extra, ' Super pizza!');
59+
return n == 'Our recommendation: Super pizza!';
60+
}
61+
});
62+
} finally {
63+
await page.close();
64+
}
65+
}
66+
```
67+
68+
{{< /code >}}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 'evaluateHandle(pageFunction[, arg])'
3+
description: 'Browser module: locator.evaluateHandle(pageFunction[, arg]) method'
4+
---
5+
6+
# evaluateHandle(pageFunction[, arg])
7+
8+
Returns the value of the `pageFunction` invocation as a `JSHandle`. It passes the matching element of the locator as the first argument to the `pageFunction` and arg as a second argument.
9+
10+
The only difference between [evaluate](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/evaluate/) and `evaluateHandle` is that `evaluateHandle` returns [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
11+
12+
<TableWithNestedRows>
13+
14+
| Parameter | Type | Defaults | Description |
15+
| ------------ | ------------------ | -------- | -------------------------------------------- |
16+
| pageFunction | function or string | | Function to be evaluated. |
17+
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
18+
19+
</TableWithNestedRows>
20+
21+
### Returns
22+
23+
| Type | Description |
24+
| ----------------- | --------------------------------------------------- |
25+
| Promise<JSHandle> | A `JSHandle` of the return value of `pageFunction`. |
26+
27+
### Example
28+
29+
{{< code >}}
30+
31+
<!-- eslint-skip -->
32+
33+
```javascript
34+
import { browser } from 'k6/browser';
35+
36+
export const options = {
37+
scenarios: {
38+
browser: {
39+
executor: 'shared-iterations',
40+
options: {
41+
browser: {
42+
type: 'chromium',
43+
},
44+
},
45+
},
46+
},
47+
};
48+
49+
export default async function () {
50+
const page = await browser.newPage();
51+
52+
try {
53+
await page.goto("https://quickpizza.grafana.com", { waitUntil: "load" });
54+
55+
await page.getByText('Pizza, Please!').click();
56+
57+
const jsHandle = await page.locator('#pizza-name').evaluateHandle((pizzaName) => pizzaName);
58+
59+
const obj = await jsHandle.evaluateHandle((handle) => {
60+
return { innerText: handle.innerText };
61+
});
62+
console.log(await obj.jsonValue()); // {"innerText":"Our recommendation:"}
63+
} finally {
64+
await page.close();
65+
}
66+
}
67+
```
68+
69+
{{< /code >}}

0 commit comments

Comments
 (0)