Skip to content

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

feat(core): type addInitScript function arg (#17651)

Stephen Li0b1b718afc13
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 3a24c1d91c..676e2fd333 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -249,6 +249,35 @@ export interface Page {
*/
evaluateHandle<R>(pageFunction: PageFunction<void, R>, arg?: any): Promise<SmartHandle<R>>;
+ /**
+ * Adds a script which would be evaluated in one of the following scenarios:
+ * - Whenever the page is navigated.
+ * - Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly
+ * attached frame.
+ *
+ * The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
+ * the JavaScript environment, e.g. to seed `Math.random`.
+ *
+ * An example of overriding `Math.random` before the page loads:
+ *
+ * ```js
+ * // preload.js
+ * Math.random = () => 42;
+ * ```
+ *
+ * ```js
+ * // In your playwright script, assuming the preload.js file is in same directory
+ * await page.addInitScript({ path: './preload.js' });
+ * ```
+ *
+ * > NOTE: The order of evaluation of multiple scripts installed via
+ * [browserContext.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script)
+ * and [page.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-page#page-add-init-script) is not defined.
+ * @param script Script to be evaluated in the page.
+ * @param arg Optional argument to pass to `script` (only supported when passing a function).
+ */
+ addInitScript<Arg>(script: PageFunction<Arg, any> | { path?: string, content?: string }, arg?: Arg): Promise<void>;
+
/**
* > NOTE: The use of [ElementHandle] is discouraged, use [Locator] objects and web-first assertions instead.
*
@@ -1731,46 +1760,6 @@ export interface Page {
*/
accessibility: Accessibility;
- /**
- * Adds a script which would be evaluated in one of the following scenarios:
- * - Whenever the page is navigated.
- * - Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly
- * attached frame.
- *
- * The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
- * the JavaScript environment, e.g. to seed `Math.random`.
- *
- * An example of overriding `Math.random` before the page loads:
- *
- * ```js
- * // preload.js
- * Math.random = () => 42;
- * ```
- *
- * ```js
- * // In your playwright script, assuming the preload.js file is in same directory
- * await page.addInitScript({ path: './preload.js' });
- * ```
- *
- * > NOTE: The order of evaluation of multiple scripts installed via
- * [browserContext.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script)
- * and [page.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-page#page-add-init-script) is not defined.
- * @param script Script to be evaluated in the page.
- * @param arg Optional argument to pass to `script` (only supported when passing a function).
- */
- addInitScript(script: Function|string|{
- /**
- * Path to the JavaScript file. If `path` is a relative path, then it is resolved relative to the current working
- * directory. Optional.
- */
- path?: string;
-
- /**
- * Raw script content. Optional.
- */
- content?: string;
- }, arg?: Serializable): Promise<void>;
-
/**
* Adds a `<script>` tag into the page with the desired url or content. Returns the added tag when the script's onload
* fires or when the script content was injected into frame.
@@ -6802,6 +6791,37 @@ export interface BrowserContext {
* @param options
*/
exposeBinding(name: string, playwrightBinding: (source: BindingSource, ...args: any[]) => any, options?: { handle?: boolean }): Promise<void>;
+
+ /**
+ * Adds a script which would be evaluated in one of the following scenarios:
+ * - Whenever a page is created in the browser context or is navigated.
+ * - Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is
+ * evaluated in the context of the newly attached frame.
+ *
+ * The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
+ * the JavaScript environment, e.g. to seed `Math.random`.
+ *
+ * An example of overriding `Math.random` before the page loads:
+ *
+ * ```js
+ * // preload.js
+ * Math.random = () => 42;
+ * ```
+ *
+ * ```js
+ * // In your playwright script, assuming the preload.js file is in same directory.
+ * await browserContext.addInitScript({
+ * path: 'preload.js'
+ * });
+ * ```
+ *
+ * > NOTE: The order of evaluation of multiple scripts installed via
+ * [browserContext.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script)
+ * and [page.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-page#page-add-init-script) is not defined.
+ * @param script Script to be evaluated in all pages in the browser context.
+ * @param arg Optional argument to pass to `script` (only supported when passing a function).
+ */
+ addInitScript<Arg>(script: PageFunction<Arg, any> | { path?: string, content?: string }, arg?: Arg): Promise<void>;
/**
* > NOTE: Only works with Chromium browser's persistent context.
*
@@ -7235,48 +7255,6 @@ export interface BrowserContext {
sameSite?: "Strict"|"Lax"|"None";
}>): Promise<void>;
- /**
- * Adds a script which would be evaluated in one of the following scenarios:
- * - Whenever a page is created in the browser context or is navigated.
- * - Whenever a child frame is attached or navigated in any page in the browser context. In this case, the script is
- * evaluated in the context of the newly attached frame.
- *
- * The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend
- * the JavaScript environment, e.g. to seed `Math.random`.
- *
- * An example of overriding `Math.random` before the page loads:
- *
- * ```js
- * // preload.js
- * Math.random = () => 42;
- * ```
- *
- * ```js
- * // In your playwright script, assuming the preload.js file is in same directory.
- * await browserContext.addInitScript({
- * path: 'preload.js'
- * });
- * ```
- *
- * > NOTE: The order of evaluation of multiple scripts installed via
- * [browserContext.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script)
- * and [page.addInitScript(script[, arg])](https://playwright.dev/docs/api/class-page#page-add-init-script) is not defined.
- * @param script Script to be evaluated in all pages in the browser context.
- * @param arg Optional argument to pass to `script` (only supported when passing a function).
- */
- addInitScript(script: Function|string|{
- /**
- * Path to the JavaScript file. If `path` is a relative path, then it is resolved relative to the current working
- * directory. Optional.
- */
- path?: string;
-
- /**
- * Raw script content. Optional.
- */
- content?: string;
- }, arg?: Serializable): Promise<void>;
-
/**
* > NOTE: Background pages are only supported on Chromium-based browsers.
*
diff --git a/utils/generate_types/overrides.d.ts b/utils/generate_types/overrides.d.ts
index 760954e0c8..c85a05234c 100644
--- a/utils/generate_types/overrides.d.ts
+++ b/utils/generate_types/overrides.d.ts
@@ -34,6 +34,8 @@ export interface Page {
evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg: Arg): Promise<SmartHandle<R>>;
evaluateHandle<R>(pageFunction: PageFunction<void, R>, arg?: any): Promise<SmartHandle<R>>;
+ addInitScript<Arg>(script: PageFunction<Arg, any> | { path?: string, content?: string }, arg?: Arg): Promise<void>;
+
$<K extends keyof HTMLElementTagNameMap>(selector: K, options?: { strict: boolean }): Promise<ElementHandleForTag<K> | null>;
$(selector: string, options?: { strict: boolean }): Promise<ElementHandle<SVGElement | HTMLElement> | null>;
@@ -97,6 +99,8 @@ export interface Frame {
export interface BrowserContext {
exposeBinding(name: string, playwrightBinding: (source: BindingSource, arg: JSHandle) => any, options: { handle: true }): Promise<void>;
exposeBinding(name: string, playwrightBinding: (source: BindingSource, ...args: any[]) => any, options?: { handle?: boolean }): Promise<void>;
+
+ addInitScript<Arg>(script: PageFunction<Arg, any> | { path?: string, content?: string }, arg?: Arg): Promise<void>;
}
export interface Worker {
diff --git a/utils/generate_types/test/test.ts b/utils/generate_types/test/test.ts
index 00be3d3e06..ac608c1165 100644
--- a/utils/generate_types/test/test.ts
+++ b/utils/generate_types/test/test.ts
@@ -576,6 +576,17 @@ playwright.chromium.launch().then(async browser => {
});
}
+ {
+ await page.addInitScript((args) => {
+ args.foo === args.hello.world
+ }, {
+ foo: 'bar',
+ hello: {
+ world: 'bar'
+ }
+ });
+ }
+
await browser.close();
})();