feat(web): init scripts run before the page's own, from web({ initScripts }) or browser.addInitScript
Merged
OK
2mo ago+468+468+012345678901234567890123456789-15-15-01234567890123456789
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), seedMath.random, or set a flag the app reads at boot. Until now the migration table listed this asmissing. 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 wayevaluatedoes it, with the same__namehelper, now shared asKEEP_NAMES_HELPER. Its arg must be JSON, and a secret there isPOLICY_DENIED. - Scripts are registered on the context, and the attempt's list is applied in the
configurehook. So they're added again whenever the context is replaced (app.clearState(), session restore) and after a CDP reconnect (reconnectEndpoint, Kernelscope: 'attempt'), with no duplicates (packages/web/src/surface.ts,startAttempt). - Config files are read in
prepare, so a bad path stops the run withINVALID_CONFIGbefore any test. Each worker reads them again ininit, in parallel with the browser launch; a failed read is thrown only after the launch settles, sodisposeowns that browser (ConfiguredInitScriptsinpackages/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 aDisposable; ours returnsvoid, 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 seedsMath.randomthroughbrowser.addInitScriptand reloads, and/browsershows 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 afterapp.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.jsonstep:"api": "browser.addInitScript", "label": "function". The script's code (selectedAddress) appears nowhere in the report.- Integration tests cover order, frames, a
window.opentab,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/**/*.mdxpage for the behavior, andskills/e2e/if the skill describes it. - Contract change: emitted
.d.tsreviewed,tests/types/sdk-types.tsupdated; wire change edits the schema and both fixtures. - Engine contract change (
e2e/engine): changesets fore2e,@e2e-dev/web, and@e2e-dev/mobile.