Skip to content

feat: Add Init Script runs JavaScript before the page's own scripts - #5516

Open
aaltat wants to merge 2 commits into
MarketSquare:mainfrom
aaltat:add-init-script
Open

aaltat wants to merge 2 commits into
MarketSquare:mainfrom
aaltat:add-init-script

Conversation

@aaltat

@aaltat aaltat commented Nov 10, 2026

Copy link
Copy Markdown
Member

Fixes #5099

Summary

Add Init Script script=None * path=None arg=None scope=Test registers an init script on the active context: it runs before the page's own scripts in every page, frame and popup of that context. It is always context level; there is no page-level variant (ADR 0015).

Add Init Script                         (Python, Evaluation)
  path given → read the file in Python, send its source
  scope_owner(scope)                    Test fails outside a test, hint: scope=Suite
  rpc AddInitScript(content, arg)
    initScriptSource(content, arg)      (Node, init-script.ts)
      no arg → source as it is
      arg    → must compile as one expression → "(content)(arg)"
    context.addInitScript({content})    Disposable kept on the context
  InitScripts.added(context, script, scope)

end of test / suite (_end_test, _end_suite, before automatic closing)
  for each script of that scope
    rpc RemoveInitScript(context, script)  → disposable.dispose(), no-op if the context closed

Corrections to the issue body, found while implementing:

  • With arg, the check is that the source is one expression, not one function expression. An expression that is not a function fails in the page, like any other error in the script.
  • Errors in a script show up with Get Page Errors (Playwright pageerror), not in the console log.

Also: the glossary gets Init script, ADR 0015 records why it is context level with a scope, and the outdated comment in recorder.ts that said an init script cannot be removed is reworded (Recorder behaviour unchanged).

Evidence

  • Before: no way to run JavaScript before the page's own scripts without a JS extension.
    After:
    • atest/test/05_JS_Tests/init_script.robot, 9 tests: the script runs before an inline page script, arg, path, popups, several scripts, a broken script does not fail the keyword and shows in Get Page Errors, error when arg is used with statements, error when both or neither of script/path are given, a Suite script reaches every test, and scope=Test fails in Suite Setup.
    • utest/test_init_script_scope.py runs its own two-test suite: a Test-scoped script does not reach the next test. With the removal at scope end disabled, this test fails.
    • utest/test_python_usage.py: on the Python path the script stays until its context closes; no open context fails.
    • Jest init-script.test.ts: arg wrapping, the one-expression check, a file ending in a line comment.
    • Locally after rebasing on main: inv utest 1765 passed, inv utest-node 406 passed, inv lint clean, full inv atest 1106 passed, 4 failed. The 4 failures are local to the maintainer's Mac and unrelated: three Firefox launch failures, and Wait For Response With OPTIONS Request, which needs Playwright 1.65 while the local wrapper install still has 1.64.1. The new tests also ran on 1.64.1 locally; CI runs them on 1.65.

Merge Danger

Door: two-way until released, one-way after: a released keyword is removed only through deprecation.

Blast Radius: small

Purely additive keyword. The only shared path touched is the end of each test and suite, which now also removes the init scripts of that scope; it does nothing when none were added.

🤖 Generated with Claude Code

aaltat and others added 2 commits November 10, 2026 22:36
The script is registered on the active context, so it reaches every
page, frame and popup of it, and it is removed when its scope (Test by
default, Suite or Global) ends or its context closes. A script from a
file is read by Python; with arg, the script must be one function
expression, which Node calls with the value.

Fixes MarketSquare#5099

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The arg check promises one expression, which is what it checks; a
non-function fails in the page like other script errors. The test that
a Test-scoped init script does not reach the next test runs its own
two-test suite, so it no longer depends on test order. Also drops an
unused type export, trims the docstring and comments the new rpcs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Add Init Script keyword to run JavaScript before page scripts load

1 participant