docs: add example w/ params for locator.evaluate (#35645)

This commit is contained in:
Pavel Feldman 2025-04-16 19:34:56 -07:00 committed by GitHub
parent 039e87f5cb
commit 0109882a66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 0 deletions

View File

@ -871,6 +871,37 @@ If [`param: expression`] throws or rejects, this method throws.
**Usage** **Usage**
Passing argument to [`param: expression`]:
```js
const result = await page.getByTestId('myId').evaluate((element, [x, y]) => {
return element.textContent + ' ' + x * y;
}, [7, 8]);
console.log(result); // prints "myId text 56"
```
```java
Object result = page.getByTestId("myId").evaluate("(element, [x, y]) => {\n" +
" return element.textContent + ' ' + x * y;\n" +
"}", Arrays.asList(7, 8));
System.out.println(result); // prints "myId text 56"
```
```python async
result = await page.get_by_testid("myId").evaluate("(element, [x, y]) => element.textContent + ' ' + x * y", [7, 8])
print(result) # prints "myId text 56"
```
```python sync
result = page.get_by_testid("myId").evaluate("(element, [x, y]) => element.textContent + ' ' + x * y", [7, 8])
print(result) # prints "myId text 56"
```
```csharp
var result = await page.GetByTestId("myId").EvaluateAsync<string>("(element, [x, y]) => element.textContent + ' ' + x * y)", new[] { 7, 8 });
Console.WriteLine(result); // prints "myId text 56"
```
### param: Locator.evaluate.expression = %%-evaluate-expression-%% ### param: Locator.evaluate.expression = %%-evaluate-expression-%%
* since: v1.14 * since: v1.14

View File

@ -12197,6 +12197,17 @@ export interface Locator {
* rejects, this method throws. * rejects, this method throws.
* *
* **Usage** * **Usage**
*
* Passing argument to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression):
*
* ```js
* const result = await page.getByTestId('myId').evaluate((element, [x, y]) => {
* return element.textContent + ' ' + x * y;
* }, [7, 8]);
* console.log(result); // prints "myId text 56"
* ```
*
* @param pageFunction Function to be evaluated in the page context. * @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to * @param arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression). * [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).
@ -12222,6 +12233,17 @@ export interface Locator {
* rejects, this method throws. * rejects, this method throws.
* *
* **Usage** * **Usage**
*
* Passing argument to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression):
*
* ```js
* const result = await page.getByTestId('myId').evaluate((element, [x, y]) => {
* return element.textContent + ' ' + x * y;
* }, [7, 8]);
* console.log(result); // prints "myId text 56"
* ```
*
* @param pageFunction Function to be evaluated in the page context. * @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to * @param arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression). * [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).

View File

@ -12197,6 +12197,17 @@ export interface Locator {
* rejects, this method throws. * rejects, this method throws.
* *
* **Usage** * **Usage**
*
* Passing argument to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression):
*
* ```js
* const result = await page.getByTestId('myId').evaluate((element, [x, y]) => {
* return element.textContent + ' ' + x * y;
* }, [7, 8]);
* console.log(result); // prints "myId text 56"
* ```
*
* @param pageFunction Function to be evaluated in the page context. * @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to * @param arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression). * [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).
@ -12222,6 +12233,17 @@ export interface Locator {
* rejects, this method throws. * rejects, this method throws.
* *
* **Usage** * **Usage**
*
* Passing argument to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression):
*
* ```js
* const result = await page.getByTestId('myId').evaluate((element, [x, y]) => {
* return element.textContent + ' ' + x * y;
* }, [7, 8]);
* console.log(result); // prints "myId text 56"
* ```
*
* @param pageFunction Function to be evaluated in the page context. * @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to * @param arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression). * [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).