Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Pavel Feldman0109882a66ce
diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.md
index 25462a0ed9..4397d144db 100644
--- a/docs/src/api/class-locator.md
+++ b/docs/src/api/class-locator.md
@@ -871,6 +871,37 @@ If [`param: expression`] throws or rejects, this method throws.
**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-%%
* since: v1.14
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index aff26e8942..de16729658 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -12197,6 +12197,17 @@ export interface Locator {
* rejects, this method throws.
*
* **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 arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).
@@ -12222,6 +12233,17 @@ export interface Locator {
* rejects, this method throws.
*
* **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 arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index aff26e8942..de16729658 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -12197,6 +12197,17 @@ export interface Locator {
* rejects, this method throws.
*
* **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 arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).
@@ -12222,6 +12233,17 @@ export interface Locator {
* rejects, this method throws.
*
* **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 arg Optional argument to pass to
* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).