Method: Playwright::BrowserContext#add_init_script

Defined in:
lib/playwright_api/browser_context.rb

#add_init_script(path: nil, script: nil) ⇒ Object

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`.

Usage

An example of overriding ‘Math.random` before the page loads:

“‘python sync # in your playwright script, assuming the preload.js file is in same directory. browser_context.add_init_script(path=“preload.js”) “`

NOTE: The order of evaluation of multiple scripts installed via [‘method: BrowserContext.addInitScript`] and

‘method: Page.addInitScript`

is not defined.



70
71
72
# File 'lib/playwright_api/browser_context.rb', line 70

def add_init_script(path: nil, script: nil)
  wrap_impl(@impl.add_init_script(path: unwrap_impl(path), script: unwrap_impl(script)))
end