feat(web): init scripts run before the page's own, from web({ initScripts }) or browser.addInitScript

Merged
OK
2mo ago
+468-15

What and why

  • Adds init scripts, the same thing as Playwright's addInitScript. A script runs in every document before the page's own scripts, in every tab and frame. They're needed to mock a browser API (a wallet provider, say), seed Math.random, or set a flag the app reads at boot. Until now the migration table listed this as missing.
  • web({ initScripts }) applies to every attempt. browser.addInitScript(script, arg?) adds a script for the rest of one attempt, after the configured ones.
  • A script is JavaScript source, a { path } relative to the project root, or a function. A function is serialized the same way evaluate does it, with the same __name helper, now shared as KEEP_NAMES_HELPER. Its arg must be JSON, and a secret there is POLICY_DENIED.
  • Scripts are registered on the context, and the attempt's list is applied in the configure hook. So they're added again whenever the context is replaced (app.clearState(), session restore) and after a CDP reconnect (reconnectEndpoint, Kernel scope: 'attempt'), with no duplicates (packages/web/src/surface.ts, startAttempt).
  • Config files are read in prepare, so a bad path stops the run with INVALID_CONFIG before any test. Each worker reads them again in init, in parallel with the browser launch; a failed read is thrown only after the launch settles, so dispose owns that browser (ConfiguredInitScripts in packages/web/src/init-scripts.ts).
  • The step label is the file path, or source / function, never the script's code.
  • Not implemented: Playwright's { content } (use a plain string), and a remover. Playwright returns a Disposable; ours returns void, so a remover can come later without breaking anyone.
// e2e.config.ts
web({ initScripts: [{ path: 'tests/init.js' }] })

// a test
await browser.addInitScript((value) => { Math.random = () => value; }, 0.5);
await app.open('/raffle');
flowchart LR
  P[prepare: read config files<br/>bad path = INVALID_CONFIG] --> I[init: read again per worker,<br/>parallel with browser launch]
  I --> S[startAttempt: list = configured]
  T[browser.addInitScript] -->|append + add to live context| S
  S --> C[configure: closed-shadow, then list<br/>new context, clearState, restore, CDP reconnect]

Verified

Ran it locally: yes

  • Testbed, e2e run tests/browser.e2e.ts: the new test seeds Math.random through browser.addInitScript and reloads, and /browser shows that its own boot script read the seeded value. Full testbed suite: 71 passed, 1 skipped.
  • Scratch config with initScripts: [{ path: shim.js }]: the page sees the shim on first load and again after app.clearState().
  • The same config pointing at a missing file:
 ERROR  configuration error INVALID_CONFIG (launch)
web({ initScripts })[0] path cannot be read: .../apps/testbed/missing.js (ENOENT)

 Test Files  no test files
exit 2
  • .e2e/report.json step: "api": "browser.addInitScript", "label": "function". The script's code (selectedAddress) appears nowhere in the report.
  • Integration tests cover order, frames, a window.open tab, clearState, carry-over within one attempt and no leak into the next, bad inputs, and CDP reconnect (cdp-recovery.test.ts). Breaking the line that re-applies scripts to a new context makes them fail.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.