Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(evaluate): add "world" option to evaluate methods (#42635)
Dmitry Gozmanaeebee1d29d3
diff --git a/docs/src/api/class-elementhandle.md b/docs/src/api/class-elementhandle.mdindex 7f3a147ccd..2f32463204 100644--- a/docs/src/api/class-elementhandle.md+++ b/docs/src/api/class-elementhandle.md@@ -470,6 +470,9 @@ Assert.AreEqual("10", await tweetHandle.EvalOnSelectorAsync(".retweets", "node =Optional argument to pass to [`param: expression`].+### option: ElementHandle.evalOnSelector.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: ElementHandle.evalOnSelectorAll* since: v1.9* discouraged: In most cases, [`method: Locator.evaluateAll`],@@ -538,6 +541,9 @@ Assert.AreEqual(new [] { "Hello!", "Hi!" }, await feedHandle.EvalOnSelectorAllAsOptional argument to pass to [`param: expression`].+### option: ElementHandle.evalOnSelectorAll.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: ElementHandle.fill* since: v1.8* discouraged: Use locator-based [`method: Locator.fill`] instead. Read more about [locators](../locators.md).diff --git a/docs/src/api/class-frame.md b/docs/src/api/class-frame.mdindex 2d846912c6..e5ec33f0c1 100644--- a/docs/src/api/class-frame.md+++ b/docs/src/api/class-frame.md@@ -572,6 +572,9 @@ var html = await frame.EvalOnSelectorAsync(".main-container", "(e, suffix) => e.Optional argument to pass to [`param: expression`].+### option: Frame.evalOnSelector.world = %%-js-evaluate-world-%%+* since: v1.64+### option: Frame.evalOnSelector.strict = %%-input-strict-%%* since: v1.14@@ -629,6 +632,9 @@ var divsCount = await frame.EvalOnSelectorAllAsync<bool>("div", "(divs, min) =>Optional argument to pass to [`param: expression`].+### option: Frame.evalOnSelectorAll.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: Frame.evaluate* since: v1.8- returns: <[Serializable]>@@ -748,6 +754,9 @@ Optional argument to pass to [`param: expression`].### option: Frame.evaluate.exposeFunctions = %%-js-evaluate-expose-functions-%%* since: v1.62+### option: Frame.evaluate.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: Frame.evaluateHandle* since: v1.8- returns: <[JSHandle]>diff --git a/docs/src/api/class-locator.md b/docs/src/api/class-locator.mdindex d7ee7012f0..945cc8884c 100644--- a/docs/src/api/class-locator.md+++ b/docs/src/api/class-locator.md@@ -1181,6 +1181,9 @@ Optional argument to pass to [`param: expression`].### option: Locator.evaluate.exposeFunctions = %%-js-evaluate-expose-functions-%%* since: v1.62+### option: Locator.evaluate.world = %%-js-evaluate-world-%%+* since: v1.64+### option: Locator.evaluate.timeout* since: v1.14* langs: python, java, csharp@@ -1250,6 +1253,9 @@ var moreThanTen = await locator.EvaluateAllAsync<bool>("(divs, min) => divs.lengOptional argument to pass to [`param: expression`].+### option: Locator.evaluateAll.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: Locator.evaluateHandle* since: v1.14- returns: <[JSHandle]>diff --git a/docs/src/api/class-page.md b/docs/src/api/class-page.mdindex f11c21ff36..5466537a8c 100644--- a/docs/src/api/class-page.md+++ b/docs/src/api/class-page.md@@ -1421,6 +1421,9 @@ var html = await page.EvalOnSelectorAsync(".main-container", "(e, suffix) => e.oOptional argument to pass to [`param: expression`].+### option: Page.evalOnSelector.world = %%-js-evaluate-world-%%+* since: v1.64+### option: Page.evalOnSelector.strict = %%-input-strict-%%* since: v1.14@@ -1476,6 +1479,9 @@ var divsCount = await page.EvalOnSelectorAllAsync<bool>("div", "(divs, min) => dOptional argument to pass to [`param: expression`].+### option: Page.evalOnSelectorAll.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: Page.evaluate* since: v1.8- returns: <[Serializable]>@@ -1599,6 +1605,9 @@ Optional argument to pass to [`param: expression`].### option: Page.evaluate.exposeFunctions = %%-js-evaluate-expose-functions-%%* since: v1.62+### option: Page.evaluate.world = %%-js-evaluate-world-%%+* since: v1.64+## async method: Page.evaluateHandle* since: v1.8- returns: <[JSHandle]>diff --git a/docs/src/api/params.md b/docs/src/api/params.mdindex 5b6be94663..2f93cecd67 100644--- a/docs/src/api/params.md+++ b/docs/src/api/params.md@@ -596,6 +596,12 @@ When set to `true`, functions passed inside [`param: arg`] are exposed in the paWhen set to `true`, functions passed inside [`param: arg`] are exposed in the page and can be called from the init script. Calling one returns a [Promise] of its result. Under the hood, each function is exposed via [`method: Page.exposeFunction`], so it is technically accessible from all frames and worlds of the page. Unlike functions passed to [`method: Page.evaluate`], functions passed to an init script are exposed in every new document, so they survive navigations. Defaults to `false`, in which case functions are not serializable and are silently dropped.+## js-evaluate-world+* langs: js+- `world` <[EvaluationWorld]<"main"|"utility">>++The JavaScript world to evaluate the function in. `"main"` is the world where the page's own scripts run. `"utility"` is an isolated world that shares the DOM with the page, but has a separate JavaScript environment that the page's scripts cannot observe or tamper with. Defaults to `"main"`.+## js-evalonselector-pagefunction* langs: js- `pageFunction` <[function]\([Element]\)|[string]>diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.tsindex 8c57503963..3a1663b0fd 100644--- a/packages/playwright-client/types/types.d.ts+++ b/packages/playwright-client/types/types.d.ts@@ -134,7 +134,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression).* @param options*/- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the value of the* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression) invocation.@@ -187,7 +187,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression).* @param options*/- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the value of the@@ -400,7 +400,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -433,7 +433,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -466,7 +466,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -499,11 +499,11 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -516,7 +516,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -529,11 +529,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -546,7 +547,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -559,11 +560,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -576,7 +578,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -589,11 +591,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -606,7 +609,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -619,8 +622,9 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when the@@ -5959,7 +5963,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).* @param options*/- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the return value of* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).@@ -6008,7 +6012,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).* @param options*/- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the return value of@@ -6201,7 +6205,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6234,7 +6238,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6267,7 +6271,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6300,11 +6304,11 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6317,7 +6321,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6330,11 +6334,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6347,7 +6352,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6360,11 +6365,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6377,7 +6383,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6390,11 +6396,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6407,7 +6414,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6420,8 +6427,9 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when the@@ -12419,7 +12427,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12434,8 +12442,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12453,7 +12462,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12468,8 +12477,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12487,7 +12497,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12502,8 +12512,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12521,7 +12532,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12536,12 +12547,13 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12555,7 +12567,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12578,11 +12590,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12596,7 +12609,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12619,11 +12632,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12637,7 +12651,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12660,11 +12674,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12678,7 +12693,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12701,8 +12716,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** Use web assertions that assert visibility or a locator-based@@ -14277,7 +14293,7 @@ export interface Locator {* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).* @param options*/- evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Execute JavaScript code in the page, taking the matching element as an argument, and return a* [JSHandle](https://playwright.dev/docs/api/class-jshandle) with the result.@@ -14338,8 +14354,9 @@ export interface Locator {* @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-all-option-expression).+ * @param options*/- evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg): Promise<R>;+ evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-wait-for-function-option-expression) returnsdiff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.tsindex 8d747e38a9..a6c0c92c8f 100644--- a/packages/playwright-core/src/client/browserContext.ts+++ b/packages/playwright-core/src/client/browserContext.ts@@ -47,7 +47,7 @@ import { Worker } from './worker';import { TimeoutSettings, kNoTimeout } from './timeoutSettings';import { mkdirIfNeeded } from './fileUtils';-import type { EvaluateOptions } from './jsHandle';+import type { ExposeFunctionsOptions } from './jsHandle';import type { BrowserContextOptions, Headers, SetStorageState, StorageState, WaitForEventOptions } from './types';import type { HttpCredentials } from '@protocol/structs';import type * as structs from '../../types/structs';@@ -369,7 +369,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>await this._channel.setHTTPCredentials({ httpCredentials: toHttpCredentialsProtocol(httpCredentials || undefined) }, kNoTimeout);}- async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any, options?: EvaluateOptions) {+ async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any, options?: ExposeFunctionsOptions) {assertEvaluateOptions(options);if (options?.exposeFunctions)return await addInitScriptWithExposedFunctions(this, script, arg);diff --git a/packages/playwright-core/src/client/channels.d.ts b/packages/playwright-core/src/client/channels.d.tsindex 3d0223a2dd..301b508a57 100644--- a/packages/playwright-core/src/client/channels.d.ts+++ b/packages/playwright-core/src/client/channels.d.ts@@ -2244,10 +2244,12 @@ export type FrameEvalOnSelectorParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorOptions = {strict?: boolean,isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorResult = {value: SerializedValue,@@ -2257,9 +2259,11 @@ export type FrameEvalOnSelectorAllParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorAllOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorAllResult = {value: SerializedValue,@@ -2466,9 +2470,11 @@ export type FrameEvaluateExpressionParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvaluateExpressionOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvaluateExpressionResult = {value: SerializedValue,@@ -2923,9 +2929,11 @@ export type JSHandleEvaluateExpressionParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type JSHandleEvaluateExpressionOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type JSHandleEvaluateExpressionResult = {value: SerializedValue,@@ -3017,10 +3025,12 @@ export type ElementHandleEvalOnSelectorParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorOptions = {strict?: boolean,isFunction?: boolean,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorResult = {value: SerializedValue,@@ -3030,9 +3040,11 @@ export type ElementHandleEvalOnSelectorAllParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorAllOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorAllResult = {value: SerializedValue,diff --git a/packages/playwright-core/src/client/elementHandle.ts b/packages/playwright-core/src/client/elementHandle.tsindex 4cee41bcf5..7cb0aa25ff 100644--- a/packages/playwright-core/src/client/elementHandle.ts+++ b/packages/playwright-core/src/client/elementHandle.ts@@ -22,13 +22,14 @@ import { assert } from '@isomorphic/assert';import { isString } from '@isomorphic/rtti';import { getMimeTypeForPath } from '@isomorphic/mimeType';import { Frame } from './frame';-import { JSHandle, parseResult, serializeArgument } from './jsHandle';+import { JSHandle, assertEvaluateOptions, parseResult, serializeArgument } from './jsHandle';import { fileUploadSizeLimit, mkdirIfNeeded } from './fileUtils';import { WritableStream } from './writableStream';import { kNoTimeout } from './timeoutSettings';import type { BrowserContext } from './browserContext';import type { ChannelOwner } from './channelOwner';+import type { WorldOptions } from './jsHandle';import type { Locator } from './locator';import type { FilePayload, Rect, SelectOption, SelectOptionOptions, TimeoutOptions } from './types';import type * as structs from '../../types/structs';@@ -216,13 +217,15 @@ export class ElementHandle<T extends Node = Node> extends JSHandle<T> implementsreturn result.elements.map(h => ElementHandle.from(h) as ElementHandle<SVGElement | HTMLElement>);}- async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg): Promise<R> {- const result = await this._elementChannel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }, kNoTimeout);+ async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertEvaluateOptions(options);+ const result = await this._elementChannel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg), world: options?.world }, kNoTimeout);return parseResult(result.value);}- async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg): Promise<R> {- const result = await this._elementChannel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }, kNoTimeout);+ async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertEvaluateOptions(options);+ const result = await this._elementChannel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg), world: options?.world }, kNoTimeout);return parseResult(result.value);}diff --git a/packages/playwright-core/src/client/frame.ts b/packages/playwright-core/src/client/frame.tsindex a1f3ede4ef..b0b6f1d998 100644--- a/packages/playwright-core/src/client/frame.ts+++ b/packages/playwright-core/src/client/frame.ts@@ -35,7 +35,7 @@ import { kLifecycleEvents } from './types';import { Waiter } from './waiter';import { TimeoutSettings, kNoTimeout } from './timeoutSettings';-import type { EvaluateOptions } from './jsHandle';+import type { EvaluateOptions, ExposeFunctionsOptions, WorldOptions } from './jsHandle';import type { LocatorOptions } from './locator';import type { Page } from './page';import type { DropPayload, FilePayload, LifecycleEvent, SelectOption, SelectOptionOptions, StrictOptions, TimeoutOptions, WaitForFunctionOptions } from './types';@@ -207,7 +207,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Frreturn ElementHandle.from((await this._channel.frameElement({}, kNoTimeout)).element);}- async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg, options?: EvaluateOptions): Promise<structs.SmartHandle<R>> {+ async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg, options?: ExposeFunctionsOptions): Promise<structs.SmartHandle<R>> {assertMaxArguments(arguments.length, 3);assertEvaluateOptions(options);const serializedArg = options?.exposeFunctions ? await serializeArgumentWithCallbacks(this, this._page, arg) : serializeArgument(arg);@@ -219,7 +219,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.FrassertMaxArguments(arguments.length, 3);assertEvaluateOptions(options);const serializedArg = options?.exposeFunctions ? await serializeArgumentWithCallbacks(this, this._page, arg) : serializeArgument(arg);- const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializedArg }, kNoTimeout);+ const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializedArg, world: options?.world }, kNoTimeout);return parseResult(result.value);}@@ -249,15 +249,17 @@ export class Frame extends ChannelOwner<channels.FrameChannel> implements api.Frawait this._channel.dispatchEvent({ selector, type, eventInit: serializeArgument(eventInit), ...options }, this._timeout(options));}- async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg): Promise<R> {- assertMaxArguments(arguments.length, 3);- const result = await this._channel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }, kNoTimeout);+ async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertMaxArguments(arguments.length, 4);+ assertEvaluateOptions(options);+ const result = await this._channel.evalOnSelector({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg), world: options?.world }, kNoTimeout);return parseResult(result.value);}- async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg): Promise<R> {- assertMaxArguments(arguments.length, 3);- const result = await this._channel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }, kNoTimeout);+ async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertMaxArguments(arguments.length, 4);+ assertEvaluateOptions(options);+ const result = await this._channel.evalOnSelectorAll({ selector, expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg), world: options?.world }, kNoTimeout);return parseResult(result.value);}diff --git a/packages/playwright-core/src/client/jsHandle.ts b/packages/playwright-core/src/client/jsHandle.tsindex c0f20cc979..2415cfefab 100644--- a/packages/playwright-core/src/client/jsHandle.ts+++ b/packages/playwright-core/src/client/jsHandle.ts@@ -40,14 +40,19 @@ export class JSHandle<T = any> extends ChannelOwner<channels.JSHandleChannel> imthis._channel.on('previewUpdated', ({ preview }) => this._preview = preview);}- async evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg, options?: EvaluateOptions): Promise<R> {+ async evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg, options?: ExposeFunctionsOptions): Promise<R> {+ assertEvaluateOptions(options);+ return await this._evaluate(pageFunction, arg, { exposeFunctions: options?.exposeFunctions });+ }++ async _evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg, options?: EvaluateOptions): Promise<R> {assertEvaluateOptions(options);const serializedArg = options?.exposeFunctions ? await serializeArgumentWithCallbacks(this, this._parentOfType('Page') as Page | undefined, arg) : serializeArgument(arg);- const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializedArg }, kNoTimeout);+ const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializedArg, world: options?.world }, kNoTimeout);return parseResult(result.value);}- async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg, options?: EvaluateOptions): Promise<structs.SmartHandle<R>> {+ async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<T, Arg, R>, arg?: Arg, options?: ExposeFunctionsOptions): Promise<structs.SmartHandle<R>> {assertEvaluateOptions(options);const serializedArg = options?.exposeFunctions ? await serializeArgumentWithCallbacks(this, this._parentOfType('Page') as Page | undefined, arg) : serializeArgument(arg);const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializedArg }, kNoTimeout);@@ -111,7 +116,9 @@ export function serializeArgument(arg: any, registerCallback?: (callback: Functireturn { value, handles };}-export type EvaluateOptions = { exposeFunctions?: boolean };+export type WorldOptions = { world?: 'main' | 'utility' };+export type ExposeFunctionsOptions = { exposeFunctions?: boolean };+export type EvaluateOptions = ExposeFunctionsOptions & WorldOptions;export async function serializeArgumentWithCallbacks(owner: ChannelOwner<any>, page: Page | undefined, arg: any): Promise<channels.SerializedArgument> {return await owner._wrapApiCall(async () => {diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.tsindex fa3e1fdefc..d9c4bb2119 100644--- a/packages/playwright-core/src/client/locator.ts+++ b/packages/playwright-core/src/client/locator.ts@@ -28,7 +28,7 @@ import { DisposableStub } from './disposable';import { kNoTimeout } from './timeoutSettings';import type { ExpectResult, Frame } from './frame';-import type { EvaluateOptions } from './jsHandle';+import type { EvaluateOptions, ExposeFunctionsOptions, WorldOptions } from './jsHandle';import type { DropPayload, FilePayload, FrameExpectParams, Rect, SelectOption, SelectOptionOptions, TimeoutOptions } from './types';import type * as structs from '../../types/structs';import type * as api from '../../types/types';@@ -138,14 +138,14 @@ export class Locator implements api.Locator {}async evaluate<R, Arg>(pageFunction: structs.PageFunctionOn<SVGElement | HTMLElement, Arg, R>, arg?: Arg, options?: TimeoutOptions & EvaluateOptions): Promise<R> {- return await this._withElement(h => h.evaluate(pageFunction, arg, options), { title: 'Evaluate', timeout: options?.timeout, signal: options?.signal });+ return await this._withElement(h => h._evaluate(pageFunction, arg, options), { title: 'Evaluate', timeout: options?.timeout, signal: options?.signal });}- async evaluateAll<R, Arg>(pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg): Promise<R> {- return await this._frame.$$eval(this._selector, pageFunction, arg);+ async evaluateAll<R, Arg>(pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ return await this._frame.$$eval(this._selector, pageFunction, arg, options);}- async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<any, Arg, R>, arg?: Arg, options?: TimeoutOptions & EvaluateOptions): Promise<structs.SmartHandle<R>> {+ async evaluateHandle<R, Arg>(pageFunction: structs.PageFunctionOn<any, Arg, R>, arg?: Arg, options?: TimeoutOptions & ExposeFunctionsOptions): Promise<structs.SmartHandle<R>> {return await this._withElement(h => h.evaluateHandle(pageFunction, arg, options), { title: 'Evaluate', timeout: options?.timeout, signal: options?.signal });}diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.tsindex f516f789ce..381b833de3 100644--- a/packages/playwright-core/src/client/page.ts+++ b/packages/playwright-core/src/client/page.ts@@ -50,7 +50,7 @@ import { TimeoutSettings, kNoTimeout } from './timeoutSettings';import { mkdirIfNeeded } from './fileUtils';import { ConsoleMessage } from './consoleMessage';import type { BrowserContext } from './browserContext';-import type { EvaluateOptions } from './jsHandle';+import type { EvaluateOptions, ExposeFunctionsOptions, WorldOptions } from './jsHandle';import type { Clock } from './clock';import type { APIRequestContext } from './fetch';import type { WaitForNavigationOptions } from './frame';@@ -339,19 +339,19 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Pagereturn await this._mainFrame.dispatchEvent(selector, type, eventInit, options);}- async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg, options?: EvaluateOptions): Promise<structs.SmartHandle<R>> {+ async evaluateHandle<R, Arg>(pageFunction: structs.PageFunction<Arg, R>, arg?: Arg, options?: ExposeFunctionsOptions): Promise<structs.SmartHandle<R>> {assertMaxArguments(arguments.length, 3);return await this._mainFrame.evaluateHandle(pageFunction, arg, options);}- async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg): Promise<R> {- assertMaxArguments(arguments.length, 3);- return await this._mainFrame.$eval(selector, pageFunction, arg);+ async $eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element, Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertMaxArguments(arguments.length, 4);+ return await this._mainFrame.$eval(selector, pageFunction, arg, options);}- async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg): Promise<R> {- assertMaxArguments(arguments.length, 3);- return await this._mainFrame.$$eval(selector, pageFunction, arg);+ async $$eval<R, Arg>(selector: string, pageFunction: structs.PageFunctionOn<Element[], Arg, R>, arg?: Arg, options?: WorldOptions): Promise<R> {+ assertMaxArguments(arguments.length, 4);+ return await this._mainFrame.$$eval(selector, pageFunction, arg, options);}async $$(selector: string): Promise<ElementHandle<SVGElement | HTMLElement>[]> {@@ -557,7 +557,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Pagereturn await this._mainFrame.evaluate(pageFunction, arg, options);}- async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any, options?: EvaluateOptions) {+ async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any, options?: ExposeFunctionsOptions) {assertEvaluateOptions(options);if (options?.exposeFunctions)return await addInitScriptWithExposedFunctions(this, script, arg);diff --git a/packages/playwright-core/src/server/channels.d.ts b/packages/playwright-core/src/server/channels.d.tsindex c1a1d870c5..5b58e66f84 100644--- a/packages/playwright-core/src/server/channels.d.ts+++ b/packages/playwright-core/src/server/channels.d.ts@@ -2245,10 +2245,12 @@ export type FrameEvalOnSelectorParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorOptions = {strict?: boolean,isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorResult = {value: SerializedValue,@@ -2258,9 +2260,11 @@ export type FrameEvalOnSelectorAllParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorAllOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvalOnSelectorAllResult = {value: SerializedValue,@@ -2467,9 +2471,11 @@ export type FrameEvaluateExpressionParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type FrameEvaluateExpressionOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type FrameEvaluateExpressionResult = {value: SerializedValue,@@ -2924,9 +2930,11 @@ export type JSHandleEvaluateExpressionParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type JSHandleEvaluateExpressionOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type JSHandleEvaluateExpressionResult = {value: SerializedValue,@@ -3018,10 +3026,12 @@ export type ElementHandleEvalOnSelectorParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorOptions = {strict?: boolean,isFunction?: boolean,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorResult = {value: SerializedValue,@@ -3031,9 +3041,11 @@ export type ElementHandleEvalOnSelectorAllParams = {expression: string,isFunction?: boolean,arg: SerializedArgument,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorAllOptions = {isFunction?: boolean,+ world?: 'main' | 'utility',};export type ElementHandleEvalOnSelectorAllResult = {value: SerializedValue,diff --git a/packages/playwright-core/src/server/dispatchers/elementHandlerDispatcher.ts b/packages/playwright-core/src/server/dispatchers/elementHandlerDispatcher.tsindex 639040930c..1522e049a4 100644--- a/packages/playwright-core/src/server/dispatchers/elementHandlerDispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/elementHandlerDispatcher.ts@@ -55,6 +55,11 @@ export class ElementHandleDispatcher extends JSHandleDispatcher<FrameDispatcher>this._elementHandle = elementHandle;}+ override async evaluateExpression(params: channels.JSHandleEvaluateExpressionParams, progress: Progress): Promise<channels.JSHandleEvaluateExpressionResult> {+ const value = await this._elementHandle.evaluateExpression(progress, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg));+ return { value: serializeResult(value) };+ }+async ownerFrame(params: channels.ElementHandleOwnerFrameParams, progress: Progress): Promise<channels.ElementHandleOwnerFrameResult> {const frame = await this._elementHandle.ownerFrame(progress);return { frame: frame ? FrameDispatcher.from(this._browserContextDispatcher(), frame) : undefined };@@ -197,11 +202,11 @@ export class ElementHandleDispatcher extends JSHandleDispatcher<FrameDispatcher>}async evalOnSelector(params: channels.ElementHandleEvalOnSelectorParams, progress: Progress): Promise<channels.ElementHandleEvalOnSelectorResult> {- return { value: serializeResult(await this._elementHandle.evalOnSelector(progress, params.selector, !!params.strict, params.expression, params.isFunction, parseArgument(params.arg))) };+ return { value: serializeResult(await this._elementHandle.evalOnSelector(progress, params.selector, !!params.strict, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg))) };}async evalOnSelectorAll(params: channels.ElementHandleEvalOnSelectorAllParams, progress: Progress): Promise<channels.ElementHandleEvalOnSelectorAllResult> {- return { value: serializeResult(await this._elementHandle.evalOnSelectorAll(progress, params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };+ return { value: serializeResult(await this._elementHandle.evalOnSelectorAll(progress, params.selector, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg))) };}async waitForElementState(params: channels.ElementHandleWaitForElementStateParams, progress: Progress): Promise<void> {diff --git a/packages/playwright-core/src/server/dispatchers/frameDispatcher.ts b/packages/playwright-core/src/server/dispatchers/frameDispatcher.tsindex 5a4cd31359..412d23c39f 100644--- a/packages/playwright-core/src/server/dispatchers/frameDispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/frameDispatcher.ts@@ -82,7 +82,7 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameChannel, Br}async evaluateExpression(params: channels.FrameEvaluateExpressionParams, progress: Progress): Promise<channels.FrameEvaluateExpressionResult> {- return { value: serializeResult(await this._frame.evaluateExpression(progress, params.expression, { isFunction: params.isFunction }, parseArgument(params.arg))) };+ return { value: serializeResult(await this._frame.evaluateExpression(progress, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg))) };}async evaluateExpressionHandle(params: channels.FrameEvaluateExpressionHandleParams, progress: Progress): Promise<channels.FrameEvaluateExpressionHandleResult> {@@ -98,11 +98,11 @@ export class FrameDispatcher extends Dispatcher<Frame, channels.FrameChannel, Br}async evalOnSelector(params: channels.FrameEvalOnSelectorParams, progress: Progress): Promise<channels.FrameEvalOnSelectorResult> {- return { value: serializeResult(await this._frame.evalOnSelector(progress, params.selector, !!params.strict, params.expression, params.isFunction, parseArgument(params.arg))) };+ return { value: serializeResult(await this._frame.evalOnSelector(progress, params.selector, !!params.strict, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg))) };}async evalOnSelectorAll(params: channels.FrameEvalOnSelectorAllParams, progress: Progress): Promise<channels.FrameEvalOnSelectorAllResult> {- return { value: serializeResult(await this._frame.evalOnSelectorAll(progress, params.selector, params.expression, params.isFunction, parseArgument(params.arg))) };+ return { value: serializeResult(await this._frame.evalOnSelectorAll(progress, params.selector, params.expression, { isFunction: params.isFunction, world: params.world }, parseArgument(params.arg))) };}async querySelector(params: channels.FrameQuerySelectorParams, progress: Progress): Promise<channels.FrameQuerySelectorResult> {diff --git a/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts b/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.tsindex d3ecc88c1b..c2aa99a892 100644--- a/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts@@ -43,6 +43,8 @@ export class JSHandleDispatcher<ParentScope extends JSHandleDispatcherParentScop}async evaluateExpression(params: channels.JSHandleEvaluateExpressionParams, progress: Progress): Promise<channels.JSHandleEvaluateExpressionResult> {+ if (params.world)+ throw new Error(`Only element handles can be evaluated in the "${params.world}" world`);const jsHandle = await this._object.evaluateExpression(progress, params.expression, { isFunction: params.isFunction }, parseArgument(params.arg));return { value: serializeResult(jsHandle) };}diff --git a/packages/playwright-core/src/server/dom.ts b/packages/playwright-core/src/server/dom.tsindex 472ca1b2a7..c1048c1dd4 100644--- a/packages/playwright-core/src/server/dom.ts+++ b/packages/playwright-core/src/server/dom.ts@@ -869,12 +869,21 @@ export class ElementHandle<T extends Node = Node> extends js.JSHandle<T> {return this._frame.selectors.queryAll(selector, this);}- async evalOnSelector(progress: Progress, selector: string, strict: boolean, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {- return this._frame.evalOnSelector(progress, selector, strict, expression, isFunction, arg, this);+ override async evaluateExpression(progress: Progress, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any): Promise<any> {+ return await progress.race(this.internalEvaluateExpression(expression, options, arg));}- async evalOnSelectorAll(progress: Progress, selector: string, expression: string, isFunction: boolean | undefined, arg: any): Promise<any> {- return this._frame.evalOnSelectorAll(progress, selector, expression, isFunction, arg, this);+ override async internalEvaluateExpression(expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any): Promise<any> {+ const context = options.world ? await this._frame.context(options.world) : this._context;+ return await js.evaluateExpression(context, expression, { isFunction: options.isFunction, returnByValue: true }, this, arg);+ }++ async evalOnSelector(progress: Progress, selector: string, strict: boolean, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any): Promise<any> {+ return this._frame.evalOnSelector(progress, selector, strict, expression, options, arg, this);+ }++ async evalOnSelectorAll(progress: Progress, selector: string, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any): Promise<any> {+ return this._frame.evalOnSelectorAll(progress, selector, expression, options, arg, this);}async isVisible(progress: Progress): Promise<boolean> {diff --git a/packages/playwright-core/src/server/frameSelectors.ts b/packages/playwright-core/src/server/frameSelectors.tsindex 373846e537..7d25600ff5 100644--- a/packages/playwright-core/src/server/frameSelectors.ts+++ b/packages/playwright-core/src/server/frameSelectors.ts@@ -67,13 +67,25 @@ export class FrameSelectors {return adoptIfNeeded(elementHandle, await elementHandle._frame.mainContext());}- async queryArrayInMainWorld(selector: string, scope?: ElementHandle): Promise<JSHandle<Element[]>> {- const resolved = await this.callOnSelectorHandle(selector, { mainWorld: true, strict: false, scope }, ({ elements }) => elements, {});+ async queryArrayInWorld(selector: string, world: types.World, scope?: ElementHandle): Promise<JSHandle<Element[]>> {+ const resolved = await this.callOnSelectorHandle(selector, { mainWorld: world === 'main', strict: false, scope }, ({ elements }) => elements, {});if (!resolved) {- const context = await this.frame.context('main');+ const context = await this.frame.context(world);return await context.evaluateHandle(() => []);}- return resolved.result;+ // Note that the selector may have been resolved in the main world regardless of the requested+ // one, e.g. when a custom selector engine requires it. Move the elements over in that case.+ const context = await resolved.frame.context(world);+ if (context === resolved.result._context)+ return resolved.result;+ const properties = await resolved.result.internalGetProperties();+ resolved.result.dispose();+ const elements = [...properties.values()];+ try {+ return await context.evaluateExpressionHandle('elements => elements', { isFunction: true }, elements);+ } finally {+ elements.map(element => element.dispose());+ }}async queryCount(selector: string): Promise<number> {@@ -296,7 +308,7 @@ export class FrameSelectors {options: types.StrictOptions & { mainWorld?: boolean, scope?: ElementHandle, markTargets?: 'all' | 'first' | 'none' },pageFunction: MatchedElementsCallback<Arg, R>,arg: Arg,- ): Promise<{ result: SmartHandle<R> } | null> {+ ): Promise<{ frame: Frame, info: SelectorInfo, result: SmartHandle<R> } | null> {const result = await this._callOnSelectorInternal(selector, { ...options, callWithoutMatches: false }, pageFunction, arg, false /* returnByValue */);return result as { frame: Frame, info: SelectorInfo, result: SmartHandle<R> } | null;}diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.tsindex 8d87d25b0e..2c20749b71 100644--- a/packages/playwright-core/src/server/frames.ts+++ b/packages/playwright-core/src/server/frames.ts@@ -901,26 +901,26 @@ export class Frame extends SdkObject<FrameEventMap> {}, { type, eventInit }, { mainWorld: true, ...options }, scope);}- async evalOnSelector(progress: Progress, selector: string, strict: boolean, expression: string, isFunction: boolean | undefined, arg: any, scope?: dom.ElementHandle): Promise<any> {- return progress.race(this._evalOnSelector(selector, strict, expression, isFunction, arg, scope));+ async evalOnSelector(progress: Progress, selector: string, strict: boolean, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any, scope?: dom.ElementHandle): Promise<any> {+ return progress.race(this._evalOnSelector(selector, strict, expression, options, arg, scope));}- private async _evalOnSelector(selector: string, strict: boolean, expression: string, isFunction: boolean | undefined, arg: any, scope?: dom.ElementHandle): Promise<any> {+ private async _evalOnSelector(selector: string, strict: boolean, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any, scope?: dom.ElementHandle): Promise<any> {const handle = await this.selectors.query(selector, { strict }, scope);if (!handle)throw new Error(`Failed to find element matching selector "${selector}"`);- const result = await handle.internalEvaluateExpression(expression, { isFunction }, arg);+ const result = await handle.internalEvaluateExpression(expression, options, arg);handle.dispose();return result;}- async evalOnSelectorAll(progress: Progress, selector: string, expression: string, isFunction: boolean | undefined, arg: any, scope?: dom.ElementHandle): Promise<any> {- return progress.race(this._evalOnSelectorAll(selector, expression, isFunction, arg, scope));+ async evalOnSelectorAll(progress: Progress, selector: string, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any, scope?: dom.ElementHandle): Promise<any> {+ return progress.race(this._evalOnSelectorAll(selector, expression, options, arg, scope));}- private async _evalOnSelectorAll(selector: string, expression: string, isFunction: boolean | undefined, arg: any, scope?: dom.ElementHandle): Promise<any> {- const arrayHandle = await this.selectors.queryArrayInMainWorld(selector, scope);- const result = await arrayHandle.internalEvaluateExpression(expression, { isFunction }, arg);+ private async _evalOnSelectorAll(selector: string, expression: string, options: { isFunction?: boolean, world?: types.World }, arg: any, scope?: dom.ElementHandle): Promise<any> {+ const arrayHandle = await this.selectors.queryArrayInWorld(selector, options.world ?? 'main', scope);+ const result = await arrayHandle.internalEvaluateExpression(expression, { isFunction: options.isFunction }, arg);arrayHandle.dispose();return result;}diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.tsindex 8c57503963..3a1663b0fd 100644--- a/packages/playwright-core/types/types.d.ts+++ b/packages/playwright-core/types/types.d.ts@@ -134,7 +134,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression).* @param options*/- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the value of the* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression) invocation.@@ -187,7 +187,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-evaluate-option-expression).* @param options*/- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the value of the@@ -400,7 +400,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -433,7 +433,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -466,7 +466,7 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -499,11 +499,11 @@ export interface Page {* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-option-expression).* @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -516,7 +516,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -529,11 +529,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -546,7 +547,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -559,11 +560,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -576,7 +578,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -589,11 +591,12 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -606,7 +609,7 @@ export interface Page {** If [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression) returns* a [Promise], then- * [page.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)+ * [page.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -619,8 +622,9 @@ export interface Page {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-page#page-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when the@@ -5959,7 +5963,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).* @param options*/- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the return value of* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).@@ -6008,7 +6012,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-evaluate-option-expression).* @param options*/- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Returns the return value of@@ -6201,7 +6205,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6234,7 +6238,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6267,7 +6271,7 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass the actionability checks and therefore can lead to the flaky* tests. Use@@ -6300,11 +6304,11 @@ export interface Frame {* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-option-expression).* @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6317,7 +6321,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6330,11 +6334,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6347,7 +6352,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6360,11 +6365,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6377,7 +6383,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6390,11 +6396,12 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -6407,7 +6414,7 @@ export interface Frame {** If [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression)* returns a [Promise], then- * [frame.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)+ * [frame.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -6420,8 +6427,9 @@ export interface Frame {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-frame#frame-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when the@@ -12419,7 +12427,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12434,8 +12442,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12453,7 +12462,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12468,8 +12477,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12487,7 +12497,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12502,8 +12512,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests.* Use@@ -12521,7 +12532,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression)* returns a [Promise], then- * [elementHandle.$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)+ * [elementHandle.$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector)* would wait for the promise to resolve and return its value.** **Usage**@@ -12536,12 +12547,13 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-option-expression).+ * @param options*/- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12555,7 +12567,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12578,11 +12590,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12596,7 +12609,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12619,11 +12632,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12637,7 +12651,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12660,11 +12674,12 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** In most cases,- * [locator.evaluateAll(pageFunction[, arg])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),+ * [locator.evaluateAll(pageFunction[, arg, options])](https://playwright.dev/docs/api/class-locator#locator-evaluate-all),* other [Locator](https://playwright.dev/docs/api/class-locator) helper methods and web-first assertions do a better* job.*@@ -12678,7 +12693,7 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* If* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression)* returns a [Promise], then- * [elementHandle.$$eval(selector, pageFunction[, arg])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)+ * [elementHandle.$$eval(selector, pageFunction[, arg, options])](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all)* would wait for the promise to resolve and return its value.** **Usage**@@ -12701,8 +12716,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {* @param pageFunction Function to be evaluated in the page context.* @param arg Optional argument to pass to* [`pageFunction`](https://playwright.dev/docs/api/class-elementhandle#element-handle-eval-on-selector-all-option-expression).+ * @param options*/- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;/*** **NOTE** Use web assertions that assert visibility or a locator-based@@ -14277,7 +14293,7 @@ export interface Locator {* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-evaluate-option-expression).* @param options*/- evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;/*** Execute JavaScript code in the page, taking the matching element as an argument, and return a* [JSHandle](https://playwright.dev/docs/api/class-jshandle) with the result.@@ -14338,8 +14354,9 @@ export interface Locator {* @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-all-option-expression).+ * @param options*/- evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg): Promise<R>;+ evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;/*** Returns when* [`pageFunction`](https://playwright.dev/docs/api/class-locator#locator-wait-for-function-option-expression) returnsdiff --git a/packages/protocol/spec/frame.yml b/packages/protocol/spec/frame.ymlindex d0c28ffc15..439ebfd99a 100644--- a/packages/protocol/spec/frame.yml+++ b/packages/protocol/spec/frame.yml@@ -34,6 +34,11 @@ Frame:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:@@ -48,6 +53,11 @@ Frame:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:@@ -327,6 +337,11 @@ Frame:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:diff --git a/packages/protocol/spec/handles.yml b/packages/protocol/spec/handles.ymlindex 9a358546f7..00e581fe65 100644--- a/packages/protocol/spec/handles.yml+++ b/packages/protocol/spec/handles.yml@@ -29,6 +29,11 @@ JSHandle:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:@@ -95,6 +100,11 @@ ElementHandle:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:@@ -109,6 +119,11 @@ ElementHandle:expression: stringisFunction: boolean?arg: SerializedArgument+ world:+ type: enum?+ literals:+ - main+ - utilityreturns:value: SerializedValueflags:diff --git a/packages/protocol/src/validator.ts b/packages/protocol/src/validator.tsindex bd456ec0d5..6e6185a6cd 100644--- a/packages/protocol/src/validator.ts+++ b/packages/protocol/src/validator.ts@@ -1210,6 +1210,7 @@ scheme.FrameEvalOnSelectorParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.FrameEvalOnSelectorResult = tObject({value: tType('SerializedValue'),@@ -1219,6 +1220,7 @@ scheme.FrameEvalOnSelectorAllParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.FrameEvalOnSelectorAllResult = tObject({value: tType('SerializedValue'),@@ -1342,6 +1344,7 @@ scheme.FrameEvaluateExpressionParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.FrameEvaluateExpressionResult = tObject({value: tType('SerializedValue'),@@ -1631,6 +1634,7 @@ scheme.JSHandleEvaluateExpressionParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.ElementHandleEvaluateExpressionParams = tType('JSHandleEvaluateExpressionParams');scheme.JSHandleEvaluateExpressionResult = tObject({@@ -1679,6 +1683,7 @@ scheme.ElementHandleEvalOnSelectorParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.ElementHandleEvalOnSelectorResult = tObject({value: tType('SerializedValue'),@@ -1688,6 +1693,7 @@ scheme.ElementHandleEvalOnSelectorAllParams = tObject({expression: tString,isFunction: tOptional(tBoolean),arg: tType('SerializedArgument'),+ world: tOptional(tEnum(['main', 'utility'])),});scheme.ElementHandleEvalOnSelectorAllResult = tObject({value: tType('SerializedValue'),diff --git a/tests/library/selectors-register.spec.ts b/tests/library/selectors-register.spec.tsindex b739376186..4a68753ac3 100644--- a/tests/library/selectors-register.spec.ts+++ b/tests/library/selectors-register.spec.ts@@ -52,6 +52,27 @@ it('should work', async ({ playwright, browser }) => {await context.close();});+it('should support the world option with a main world selector engine', async ({ playwright, browser }) => {+ // Custom engines without "contentScript" resolve in the main world, but the+ // evaluated function should still run in the requested world.+ await playwright.selectors.register('world-tag', `(${createTagSelector.toString()})()`);++ const context = await browser.newContext();+ const page = await context.newPage();+ await page.setContent('<div id=one>one</div><div id=two>two</div>');+ await page.evaluate(() => (window as any).marker = 'main');++ expect(await page.$eval('world-tag=DIV', e => (window as any).marker)).toBe('main');+ expect(await page.$eval('world-tag=DIV', e => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ expect(await page.$eval('world-tag=DIV', e => e.id, undefined, { world: 'utility' })).toBe('one');++ expect(await page.$$eval('world-tag=DIV', ee => (window as any).marker)).toBe('main');+ expect(await page.$$eval('world-tag=DIV', ee => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ expect(await page.$$eval('world-tag=DIV', ee => ee.map(e => e.id), undefined, { world: 'utility' })).toEqual(['one', 'two']);++ await context.close();+});+it('should work when registered on global', async ({ browser, mode }) => {it.skip(mode === 'driver', 'We expect registering selectors on the right Playwright instance.');diff --git a/tests/page/page-evaluate-world.spec.ts b/tests/page/page-evaluate-world.spec.tsnew file mode 100644index 0000000000..4665b326b8--- /dev/null+++ b/tests/page/page-evaluate-world.spec.ts@@ -0,0 +1,144 @@+/**+ * Copyright (c) Microsoft Corporation.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { attachFrame } from '../config/utils';+import { test as it, expect } from './pageTest';++it('should evaluate in different worlds', async ({ page }) => {+ await page.evaluate(() => (window as any).marker = 'main');+ await page.evaluate(() => (window as any).marker = 'utility', undefined, { world: 'utility' });+ expect(await page.evaluate(() => (window as any).marker)).toBe('main');+ expect(await page.evaluate(() => (window as any).marker, undefined, { world: 'main' })).toBe('main');+ expect(await page.evaluate(() => (window as any).marker, undefined, { world: 'utility' })).toBe('utility');+});++it('should share the dom between worlds', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => document.querySelector('.child')!.setAttribute('data-from-main', 'yes'));+ await page.evaluate(() => document.querySelector('.child')!.setAttribute('data-from-utility', 'yes'), undefined, { world: 'utility' });+ for (const world of ['main', 'utility'] as const) {+ const attributes = await page.evaluate(() => {+ const child = document.querySelector('.child')!;+ return {+ value: child.getAttribute('data-value'),+ fromMain: child.getAttribute('data-from-main'),+ fromUtility: child.getAttribute('data-from-utility'),+ text: child.textContent,+ };+ }, undefined, { world });+ expect(attributes).toEqual({ value: '1', fromMain: 'yes', fromUtility: 'yes', text: 'one' });+ }+});++it('should evaluate in different worlds in a frame', async ({ page, server }) => {+ await page.goto(server.EMPTY_PAGE);+ const frame = await attachFrame(page, 'frame1', server.EMPTY_PAGE + '?frame');+ await frame.evaluate(() => (window as any).marker = 'main');+ expect(await frame.evaluate(() => (window as any).marker, undefined, { world: 'main' })).toBe('main');+ expect(await frame.evaluate(() => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ // Worlds are per-frame, so the main frame is not affected.+ expect(await page.evaluate(() => (window as any).marker)).toBe(undefined);+ expect(await frame.evaluate(() => location.search, undefined, { world: 'utility' })).toBe('?frame');+});++it('should evaluate locator in different worlds', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => (window as any).marker = 'main');+ const locator = page.locator('.child').first();+ expect(await locator.evaluate(e => (window as any).marker)).toBe('main');+ expect(await locator.evaluate(e => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ // The element is the same in both worlds.+ for (const world of ['main', 'utility'] as const)+ expect(await locator.evaluate(e => e.getAttribute('data-value') + '/' + e.textContent, undefined, { world })).toBe('1/one');+});++it('should evaluate all in different worlds', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => (window as any).marker = 'main');+ const locator = page.locator('.child');+ expect(await locator.evaluateAll(ee => (window as any).marker)).toBe('main');+ expect(await locator.evaluateAll(ee => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ // The elements are the same in both worlds.+ for (const world of ['main', 'utility'] as const)+ expect(await locator.evaluateAll(ee => ee.map(e => e.getAttribute('data-value') + '/' + e.textContent), undefined, { world })).toEqual(['1/one', '2/two']);+});++it('should eval on selector in different worlds', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => (window as any).marker = 'main');+ expect(await page.$eval('.child', e => (window as any).marker)).toBe('main');+ expect(await page.$eval('.child', e => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ expect(await page.$$eval('.child', ee => (window as any).marker)).toBe('main');+ expect(await page.$$eval('.child', ee => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ for (const world of ['main', 'utility'] as const) {+ expect(await page.$eval('.child', e => e.getAttribute('data-value'), undefined, { world })).toBe('1');+ expect(await page.$$eval('.child', ee => ee.map(e => e.textContent), undefined, { world })).toEqual(['one', 'two']);+ }+});++it('should eval on selector inside an element handle in different worlds', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => (window as any).marker = 'main');+ const parent = (await page.$('#parent'))!;+ expect(await parent.$eval('.child', e => (window as any).marker)).toBe('main');+ expect(await parent.$eval('.child', e => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ expect(await parent.$$eval('.child', ee => (window as any).marker)).toBe('main');+ expect(await parent.$$eval('.child', ee => (window as any).marker, undefined, { world: 'utility' })).toBe(undefined);+ for (const world of ['main', 'utility'] as const) {+ expect(await parent.$eval('.child', e => e.getAttribute('data-value'), undefined, { world })).toBe('1');+ expect(await parent.$$eval('.child', ee => ee.map(e => e.textContent), undefined, { world })).toEqual(['one', 'two']);+ }+});++it('should isolate the utility world from main world tampering', async ({ page }) => {+ await page.setContent(`+ <div id=parent>+ <div class=child data-value=1>one</div>+ <div class=child data-value=2>two</div>+ </div>+ `);+ await page.evaluate(() => {+ Element.prototype.getAttribute = () => 'tampered';+ });+ const locator = page.locator('.child').first();+ expect(await locator.evaluate(e => e.getAttribute('data-value'))).toBe('tampered');+ expect(await locator.evaluate(e => e.getAttribute('data-value'), undefined, { world: 'utility' })).toBe('1');+});diff --git a/utils/generate_types/overrides.d.ts b/utils/generate_types/overrides.d.tsindex 62a2231567..001d1785a4 100644--- a/utils/generate_types/overrides.d.ts+++ b/utils/generate_types/overrides.d.ts@@ -38,8 +38,8 @@ type ZodSchema = ZodTypeAny | z3.ZodTypeAny;type InferZodSchema<T extends ZodSchema> = T extends z3.ZodTypeAny ? z3.infer<T> : T extends ZodTypeAny ? z.infer<T> : never;export interface Page {- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<SmartHandle<R>>;evaluateHandle<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<SmartHandle<R>>;@@ -52,15 +52,15 @@ export interface Page {$$<K extends keyof HTMLElementTagNameMap>(selector: K): Promise<ElementHandleForTag<K>[]>;$$(selector: string): Promise<ElementHandle<SVGElement | HTMLElement>[]>;- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;waitForFunction<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;waitForFunction<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;@@ -85,8 +85,8 @@ export interface Page {}export interface Frame {- evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<R>;- evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;+ evaluate<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: { exposeFunctions?: boolean }): Promise<SmartHandle<R>>;evaluateHandle<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: { exposeFunctions?: boolean }): Promise<SmartHandle<R>>;@@ -97,15 +97,15 @@ export interface Frame {$$<K extends keyof HTMLElementTagNameMap>(selector: K): Promise<ElementHandleForTag<K>[]>;$$(selector: string): Promise<ElementHandle<SVGElement | HTMLElement>[]>;- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;waitForFunction<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;waitForFunction<R>(pageFunction: PageFunction<void, R>, arg?: any, options?: PageWaitForFunctionOptions): Promise<SmartHandle<R>>;@@ -177,15 +177,15 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {$$<K extends keyof HTMLElementTagNameMap>(selector: K): Promise<ElementHandleForTag<K>[]>;$$(selector: string): Promise<ElementHandle<SVGElement | HTMLElement>[]>;- $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg): Promise<R>;- $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg): Promise<R>;- $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any): Promise<R>;- $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E, void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg): Promise<R>;- $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg): Promise<R>;- $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any): Promise<R>;- $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R, Arg>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], Arg, R>, arg: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<K extends keyof HTMLElementTagNameMap, R>(selector: K, pageFunction: PageFunctionOn<HTMLElementTagNameMap[K][], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;+ $$eval<R, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(selector: string, pageFunction: PageFunctionOn<E[], void, R>, arg?: any, options?: { world?: 'main'|'utility' }): Promise<R>;waitForSelector<K extends keyof HTMLElementTagNameMap>(selector: K, options?: ElementHandleWaitForSelectorOptionsNotHidden): Promise<ElementHandleForTag<K>>;waitForSelector(selector: string, options?: ElementHandleWaitForSelectorOptionsNotHidden): Promise<ElementHandle<SVGElement | HTMLElement>>;@@ -194,9 +194,9 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {}export interface Locator {- evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean }): Promise<R>;+ evaluate<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean, world?: 'main'|'utility' }): Promise<R>;evaluateHandle<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, R>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal, exposeFunctions?: boolean }): Promise<SmartHandle<R>>;- evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg): Promise<R>;+ evaluateAll<R, Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E[], Arg, R>, arg?: Arg, options?: { world?: 'main'|'utility' }): Promise<R>;waitForFunction<Arg, E extends SVGElement | HTMLElement = SVGElement | HTMLElement>(pageFunction: PageFunctionOn<E, Arg, any>, arg?: Arg, options?: { timeout?: number, signal?: AbortSignal }): Promise<void>;elementHandle(options?: { timeout?: number, signal?: AbortSignal }): Promise<ElementHandle<SVGElement | HTMLElement>>;highlight(options?: { style?: string | { [key: string]: string | number } }): Promise<Disposable>;