MarketSquare / MarketSquare/robotframework-browser
Add `Add Init Script` keyword to run JavaScript before page scripts load
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 655
- Forks
- 147
- Avg merge
- 5h 27m
- Merged PRs (30d)
- 59
Description
## Use case
Playwright users routinely inject a small script that runs **before any of the page's own JavaScript** — on every navigation and in every new frame. Typical needs:
- Freeze or seed `Date` / `Math.random` so the app renders deterministic content.
- Stub browser APIs (`navigator.geolocation`, `Notification`, `matchMedia`) before the app reads them.
- Pre-set feature flags or `window` globals that the app checks during startup.
The Browser library has no way to do this today. `Evaluate JavaScript` runs only after the page has loaded, which is too late for anything the application reads during its own initialization — so there is currently **no workaround at all** for this standard Playwright technique.
## Proposed keyword / arguments
`Add Init Script script=None *, path=None scope=Context`
- `script` — JavaScript source to evaluate before page scripts (raw expression or function).
- `path` — alternative to `script`: path to a `.js` file whose content is injected. Exactly one of `script`/`path` must be given.
- `scope` — `Context` (default) registers the script on the current browser context so it applies to all current and future pages (including popups); `Page` limits it to the current page.
```robotframework
New Context
Add Init Script Date.now = () => 1748736000000; Math.random = () => 0.5;
New Page ${URL}
Get Text css=.generated-id == id-500
# Stub a browser API from a file, for one page only
Add Init Script path=${CURDIR}/stubs/geolocation.js scope=Page
Reload
```
## Playwright API
- [`browserContext.addInitScript`](https://playwright.dev/docs/api/class-browsercontext#browser-context-add-init-script)
- [`page.addInitScript`](https://playwright.dev/docs/api/class-page#page-add-init-script)
## Implementation notes
- New RPC in `protobuf/playwright.proto` carrying the script source plus a page/context flag.
- `node/playwright-wrapper`: resolve the current page or context from the catalog and call `addInitScript` (file content is read Python-side and sent as source, keeping the node process filesystem-agnostic).
- New keyword in a `Browser/keywords` module (e.g. `evaluation.py`), docs, and atest cases verifying the script ran before page scripts (e.g. a page whose inline script reads the seeded value at load time) and that `scope=Context` covers pages opened afterwards.
## Backwards compatibility
Purely additive: a new keyword with named-only options; no existing keyword or argument changes.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with protobuf/playwright.proto and the node/playwright-wrapper, then inspect the Browser/keywords module, such as evaluation.py. Add the keyword and documentation, read file content Python-side when path is used, and cover both page and context scope. Run atest cases verifying execution before inline page scripts and coverage of pages opened afterward.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, python
- Domain
- backend-api-design, testing-qa
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100