sincecutoff.com
← all changes

addInitScript() accepts functions as arguments

Introduced 2026-07-24 · score 0.93

HOW TO USE IT

When an init script needs a function in its argument, pass a function as the script and set `exposeFunctions: true`. The function is callable from the init script and returns a Promise.

await page.addInitScript(({ callback }) => { window.result = callback(); }, { callback: async () => 'value' }, { exposeFunctions: true });
await browserContext.addInitScript(({ callback }) => { window.result = callback(); }, { callback: async () => 'value' }, { exposeFunctions: true });

WHY THIS HELPS

Pass a function as an init-script argument when configuring page or context setup in a test.

REQUIREMENTS AND LIMITATIONS

  • Version: v1.62.0
  • script (required): Function, string, or object describing the init script; `arg` is supported only with a function.
  • arg: Optional argument passed to the script function; with `exposeFunctions: true`, functions in it are exposed to the page.
  • options.exposeFunctions: Boolean; set to `true` to expose functions passed inside `arg`. Calls return Promises. Defaults to `false`, when functions are silently dropped. No mutually exclusive sub-keys are documented.
  • Prerequisite: Use Playwright v1.62.0 or later for `exposeFunctions`; it was added in v1.62.

FIRST-PARTY EVIDENCE

Check the installed version and the source before using it. Entry JSON

Report a problem with this card