You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`script` <[function]|[string]|[Object]> Script to be evaluated in all pages in the browser context.
291
+
-`path` <[string]> Path to the JavaScript file. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd).
292
+
-`content` <[string]> Raw script content.
293
+
-`...args` <...[Serializable]> Arguments to pass to `script` (only supported when passing a function).
294
+
- returns: <[Promise]>
295
+
296
+
Adds a script which would be evaluated in one of the following scenarios:
297
+
- Whenever a page is created in the browser context or is navigated.
298
+
- 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.
299
+
300
+
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`.
301
+
302
+
An example of overriding `Math.random` before the page loads:
303
+
304
+
```js
305
+
// preload.js
306
+
Math.random= () =>42;
307
+
308
+
// In your playwright script, assuming the preload.js file is in same folder
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args) and [page.addInitScript(script[, ...args])](#pageaddinitscriptscript-args) is not defined.
314
+
289
315
#### browserContext.clearCookies()
290
316
- returns: <[Promise]>
291
317
@@ -328,30 +354,6 @@ will be closed.
328
354
If no URLs are specified, this method returns all cookies.
329
355
If URLs are specified, only cookies that affect those URLs are returned.
-`pageFunction` <[function]|[string]> Function to be evaluated in all pages in the browser context
333
-
-`...args` <...[Serializable]> Arguments to pass to `pageFunction`
334
-
- returns: <[Promise]>
335
-
336
-
Adds a function which would be invoked in one of the following scenarios:
337
-
- Whenever a page is created in the browser context or is navigated.
338
-
- Whenever a child frame is attached or navigated in any page in the browser context. In this case, the function is invoked in the context of the newly attached frame.
339
-
340
-
The function is invoked 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`.
341
-
342
-
An example of overriding `Math.random` before the page loads:
343
-
344
-
```js
345
-
// preload.js
346
-
Math.random= () =>42;
347
-
348
-
// In your playwright script, assuming the preload.js file is in same folder
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.evaluateOnNewDocument(pageFunction[, ...args])](#browsercontextevaluateonnewdocumentpagefunction-args) and [page.evaluateOnNewDocument(pageFunction[, ...args])](#pageevaluateonnewdocumentpagefunction-args) is not defined.
@@ -752,6 +754,32 @@ This is a shortcut to [page.waitForSelector(selector[, options])](#pagewaitforse
752
754
#### page.accessibility
753
755
- returns: <[Accessibility]>
754
756
757
+
#### page.addInitScript(script[, ...args])
758
+
-`script` <[function]|[string]|[Object]> Script to be evaluated in the page.
759
+
-`path` <[string]> Path to the JavaScript file. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd).
760
+
-`content` <[string]> Raw script content.
761
+
-`...args` <...[Serializable]> Arguments to pass to `script` (only supported when passing a function).
762
+
- returns: <[Promise]>
763
+
764
+
Adds a script which would be evaluated in one of the following scenarios:
765
+
- Whenever the page is navigated.
766
+
- Whenever the child frame is attached or navigated. In this case, the scritp is evaluated in the context of the newly attached frame.
767
+
768
+
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`.
769
+
770
+
An example of overriding `Math.random` before the page loads:
771
+
772
+
```js
773
+
// preload.js
774
+
Math.random= () =>42;
775
+
776
+
// In your playwright script, assuming the preload.js file is in same folder
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args) and [page.addInitScript(script[, ...args])](#pageaddinitscriptscript-args) is not defined.
-`pageFunction` <[function]|[string]> Function to be evaluated in the page
971
-
-`...args` <...[Serializable]> Arguments to pass to `pageFunction`
972
-
- returns: <[Promise]>
973
-
974
-
Adds a function which would be invoked in one of the following scenarios:
975
-
- whenever the page is navigated
976
-
- whenever the child frame is attached or navigated. In this case, the function is invoked in the context of the newly attached frame
977
-
978
-
The function is invoked 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`.
979
-
980
-
An example of overriding `Math.random` before the page loads:
981
-
982
-
```js
983
-
// preload.js
984
-
Math.random= () =>42;
985
-
986
-
// In your playwright script, assuming the preload.js file is in same folder
> **NOTE** The order of evaluation of multiple scripts installed via [browserContext.evaluateOnNewDocument(pageFunction[, ...args])](#browsercontextevaluateonnewdocumentpagefunction-args) and [page.evaluateOnNewDocument(pageFunction[, ...args])](#pageevaluateonnewdocumentpagefunction-args) is not defined.
0 commit comments