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
-`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.
-`pageFunction` <[function]|[string]> Function to be evaluated in browser context
970
+
-`pageFunction` <[function]|[string]> Function to be evaluated in the page
948
971
-`...args` <...[Serializable]> Arguments to pass to `pageFunction`
949
972
- returns: <[Promise]>
950
973
@@ -954,23 +977,19 @@ Adds a function which would be invoked in one of the following scenarios:
954
977
955
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`.
956
979
957
-
An example of overriding the navigator.languages property before the page loads:
980
+
An example of overriding `Math.random` before the page loads:
958
981
959
982
```js
960
983
// preload.js
984
+
Math.random= () =>42;
961
985
962
-
// overwrite the `languages` property to use a custom getter
963
-
Object.defineProperty(navigator, "languages", {
964
-
get:function() {
965
-
return ["en-US", "en", "bn"];
966
-
}
967
-
});
968
-
969
-
// In your playwright script, assuming the preload.js file is in same folder of our script
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