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

Allows autocomplete on the arg passed to the init script

```ts
await page.addInitScript((arg) => {
    window.localStorage.setItem('init_user_state', JSON.stringify(arg.initUserState)) // arg was previously 'any'
}, {
    initUserState: { foo: 'bar' },
})
```
This commit is contained in:
Stephen Li 2022-10-05 12:08:30 -04:00 коммит произвёл GitHub
Родитель 2bcd9ce9ae
Коммит 0b1b718afc
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 75 добавлений и 82 удалений

142
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.
*

4
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 {

Просмотреть файл

@ -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();
})();