Skip to content

Commit 11c022e

Browse files
authored
Merge pull request #2098 from grafana/browser/evaluate
browser: add locator.evaluate and evaluateHandle
2 parents 85c4c9f + b721c40 commit 11c022e

File tree

9 files changed

+146
-11
lines changed

9 files changed

+146
-11
lines changed

docs/sources/k6/next/javascript-api/k6-browser/frame/evaluate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: 'Browser module: frame.evaluate(pageFunction[, arg]) method'
55

66
# evaluate(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation.
8+
Executes JavaScript code in the page and returns the value of the `pageFunction` invocation.
99

1010

1111
| Parameter | Type | Defaults | Description |

docs/sources/k6/next/javascript-api/k6-browser/frame/evaluatehandle.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: 'Browser module: frame.evaluateHandle(pageFunction[, arg]) method'
55

66
# evaluateHandle(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
8+
Executes JavaScript code in the page and returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
99

1010
The only difference between `page.evaluate()` and `page.evaluateHandle()` is that `page.evaluateHandle()` returns [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
1111

@@ -20,4 +20,4 @@ The only difference between `page.evaluate()` and `page.evaluateHandle()` is tha
2020

2121
| Type | Description |
2222
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
23-
| `Promise<JSHandle>` | The [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/) ) instance associated with the frame. |
23+
| `Promise<JSHandle>` | The [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/) instance associated with the frame. |

docs/sources/k6/next/javascript-api/k6-browser/jshandle/evaluate.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ description: 'Browser module: JSHandle.evaluate(pageFunction[, arg]) method'
55

66
# evaluate(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation. It passes this handle as the first argument to the `pageFunction`.
8+
Executes JavaScript code in the page, passing this handle as the first argument to the `pageFunction` and `arg` as the following arguments. It returns the value of the `pageFunction` invocation.
99

1010

1111
| Parameter | Type | Defaults | Description |
1212
| ------------ | ------------------ | -------- | -------------------------------------------- |
13-
| pageFunction | function or string | | Function to be evaluated. |
13+
| pageFunction | function or string | | Function to be evaluated in the page context. |
1414
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
1515

1616

docs/sources/k6/next/javascript-api/k6-browser/jshandle/evaluatehandle.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ description: 'Browser module: JSHandle.evaluateHandle(pageFunction[, arg]) metho
55

66
# evaluateHandle(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation as a `JSHandle`. It passes this handle as the first argument to the `pageFunction`.
8+
Executes JavaScript code in the page, passing this handle as the first argument to the `pageFunction` and `arg` as the following arguments. It returns the value of the `pageFunction` invocation as a `JSHandle`.
99

10-
The only difference between [evaluate](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/evaluate/) and `evaluateHandle` is that `evaluateHandle` returns [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
10+
The only difference between [`evaluate`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/evaluate/) and `evaluateHandle` is that `evaluateHandle` returns [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
1111

1212

1313
| Parameter | Type | Defaults | Description |
1414
| ------------ | ------------------ | -------- | -------------------------------------------- |
15-
| pageFunction | function or string | | Function to be evaluated. |
15+
| pageFunction | function or string | | Function to be evaluated in the page context. |
1616
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
1717

1818

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: 'evaluate(pageFunction[, arg])'
3+
description: 'Browser module: locator.evaluate(pageFunction[, arg]) method'
4+
---
5+
6+
# evaluate(pageFunction[, arg])
7+
8+
Executes JavaScript code in the page, passing the matching element of the locator as the first argument to the `pageFunction` and `arg` as the following arguments. It returns the value of the `pageFunction` invocation.
9+
10+
11+
| Parameter | Type | Defaults | Description |
12+
| ------------ | ------------------ | -------- | -------------------------------------------- |
13+
| pageFunction | function or string | | Function to be evaluated in the page context. |
14+
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
15+
16+
17+
### Returns
18+
19+
| Type | Description |
20+
| ------------ | ----------------------------------- |
21+
| Promise<any> | The return value of `pageFunction`. |
22+
23+
### Example
24+
25+
{{< code >}}
26+
27+
<!-- eslint-skip -->
28+
29+
```javascript
30+
import { browser } from 'k6/browser';
31+
import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js';
32+
33+
export const options = {
34+
scenarios: {
35+
browser: {
36+
executor: 'shared-iterations',
37+
options: {
38+
browser: {
39+
type: 'chromium',
40+
},
41+
},
42+
},
43+
},
44+
};
45+
46+
export default async function () {
47+
const page = await browser.newPage();
48+
49+
try {
50+
await page.goto("https://quickpizza.grafana.com", { waitUntil: "load" });
51+
52+
await page.getByText('Pizza, Please!').click();
53+
54+
await check(page, {
55+
'pizza name': async p => {
56+
const n = await p.locator('#pizza-name').evaluate((pizzaName, extra) => pizzaName.textContent + extra, ' Super pizza!');
57+
return n == 'Our recommendation: Super pizza!';
58+
}
59+
});
60+
} finally {
61+
await page.close();
62+
}
63+
}
64+
```
65+
66+
{{< /code >}}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: 'evaluateHandle(pageFunction[, arg])'
3+
description: 'Browser module: locator.evaluateHandle(pageFunction[, arg]) method'
4+
---
5+
6+
# evaluateHandle(pageFunction[, arg])
7+
8+
Executes JavaScript code in the page, passing the matching element of the locator as the first argument to the `pageFunction` and `arg` as the following arguments. It returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
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+
13+
| Parameter | Type | Defaults | Description |
14+
| ------------ | ------------------ | -------- | -------------------------------------------- |
15+
| pageFunction | function or string | | Function to be evaluated in the page context. |
16+
| arg | string | `''` | Optional argument to pass to `pageFunction`. |
17+
18+
19+
### Returns
20+
21+
| Type | Description |
22+
| ----------------- | --------------------------------------------------- |
23+
| Promise<JSHandle> | A [JSHandle]((https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/)) of 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+
34+
export const options = {
35+
scenarios: {
36+
browser: {
37+
executor: 'shared-iterations',
38+
options: {
39+
browser: {
40+
type: 'chromium',
41+
},
42+
},
43+
},
44+
},
45+
};
46+
47+
export default async function () {
48+
const page = await browser.newPage();
49+
50+
try {
51+
await page.goto("https://quickpizza.grafana.com", { waitUntil: "load" });
52+
53+
await page.getByText('Pizza, Please!').click();
54+
55+
const jsHandle = await page.locator('#pizza-name').evaluateHandle((pizzaName) => pizzaName);
56+
57+
const obj = await jsHandle.evaluateHandle((handle) => {
58+
return { innerText: handle.innerText };
59+
});
60+
console.log(await obj.jsonValue()); // {"innerText":"Our recommendation:"}
61+
} finally {
62+
await page.close();
63+
}
64+
}
65+
```
66+
67+
{{< /code >}}

docs/sources/k6/next/javascript-api/k6-browser/page/evaluate.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: 'Browser module: page.evaluate(pageFunction[, arg]) method'
55

66
# evaluate(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation.
8+
Executes JavaScript code in the page and returns the value of the `pageFunction` invocation.
99

1010

1111
| Parameter | Type | Defaults | Description |
@@ -28,7 +28,7 @@ Returns the value of the `pageFunction` invocation.
2828

2929
```javascript
3030
import { browser } from 'k6/browser';
31-
import { check } from 'k6/http';
31+
import { check } from 'k6';
3232

3333
export const options = {
3434
scenarios: {

docs/sources/k6/next/javascript-api/k6-browser/page/evaluatehandle.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: 'Browser module: page.evaluateHandle(pageFunction[, arg]) method'
55

66
# evaluateHandle(pageFunction[, arg])
77

8-
Returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
8+
Executes JavaScript code in the page and returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
99

1010
The only difference between `page.evaluate()` and `page.evaluateHandle()` is that `page.evaluateHandle()` returns [JSHandle](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/jshandle/).
1111

0 commit comments

Comments
 (0)