feat(electron): per-test launch, keyboard, and window evaluate
What
The electron fixture gets what a real app's suite needs beside screen, and five engine bugs are fixed.
test('opens on saved settings', async ({ electron, screen }) => {
writeFileSync(path.join(electron.userDataDir, 'settings.json'), '{"theme":"dark"}');
await electron.launch({ args: ['--skip-onboarding'], env: { APP_DATA_PATH: electron.userDataDir }, cwd: 'app' });
await electron.keyboard.press('ControlOrMeta+P');
const theme = await electron.activeWindow().evaluate((key) => localStorage.getItem(key), 'theme');
});
- New:
electron.launch({ args, env, cwd, timeout }),electron.userDataDir,electron.activeWindow(),electron.keyboard,electron.mouse,evaluate(fn, arg)andreload()on a window, and the engine optionelectron({ cwd }). - Fixed: a button with an icon-font glyph is named by its text,
getByTextanswers with the text and not the container it sits in, a step survives the active window closing under it,focus()accepts a composite that hands focus to its own child, and a key chord no longer releases a modifier the test holds.
Why
I ported 31 of Kong/insomnia's Playwright Electron specs (105 tests) to @e2e-dev/electron to see what a real app needs. The suite could not start without per-test launch environment: Insomnia writes its data beside --user-data-dir unless a variable tells it where, so tests shared one data directory. It also uses page.keyboard 75 times and page.evaluate 53 times, and neither had a counterpart. The bugs each failed or silently mis-ran a ported test, except the held modifier, which a review of this PR found in the new keyboard.down.
How it works
- Launch checks.
checkLaunchOptionsvalidates whatelectron.launch()is given with the same checks the config gets (checkEnv,checkCwd,ownedArgument), asINVALID_ARGUMENTinstead ofINVALID_CONFIG(packages/electron/src/options.ts:146). A secret anywhere in the options isPOLICY_DENIED(packages/electron/src/electron.ts). - Launch.
launchWithstores the checked extras inside the serialized launch queue and restarts (packages/electron/src/surface.ts:186).AppProcess.startappends the arguments after the target's, lays the variables over the engine'senv(launchEnvreplaces a name in any case on Windows, wherepathandPATHare one variable), and resolvescwdfrom the project root.startAttemptclears them, so the next test starts without them. Anotherlaunch()replaces them. userDataDir. The attempt's directory already exists fromstartAttempt; the getter exposes it before the launch.- Keyboard and mouse. Every call goes through one
surface.input, which runs it on the sameactpath as a locator action: in turn after earlier gestures, bounded by the action timeout (surface.ts:404).keyboard.typesharestypeKeyswithlocatortyping (packages/electron/src/actions.ts) and gets the action timeout plus its delays. - Held modifiers.
withModifiersskips a modifier the keyboard already holds, sokeyboard.down('Shift')survivespress('Shift+Tab')and a tap with modifiers (actions.ts:435). - Evaluate.
electron.evaluateandwindow.evaluatesharecallForPlainData: the function as source text, the argument as a protocol value, the result throughplainData(packages/electron/src/evaluate.ts:28). A thrown error isEVALUATE_FAILEDin both. window.reload. Waits for a new main-frame document and then its load, so a load the old document was still finishing does not count, and rejects if the window closes or crashes (packages/electron/src/page.ts:220).- Names. A name's private-use code points become spaces before collapsing, as
web()does; a name of glyphs alone is kept (packages/electron/src/tree.ts,spokenName). - Text runs. A locator keeps a lone text run as its own node when it sits inside elements the tree leaves out, unless the node is a control that is its text (button, link, heading, tab, option, and the like) (
tree.ts:359). Core's innermost-match rule then answersgetByTextwith the run, which has the text's own box. The rule reads the document's structure only, never layout. Observations for the agent are unchanged; that is a follow-up. - Closed window.
locatereads the next shown window when the one it read closes before the query. Both it andreadShowndecide that throughclosedUnder(surface.ts:259). Each retry discards a window, so the loop ends. - Focus. The in-page
focusop answersheldwhen the active element is inside the target (packages/electron/src/in-page/node-ops.ts).
Decisions a reviewer may ask about:
launchoptions stay for the rest of the test instead of applying to one launch, because a relaunch on other arguments than the first launch is the rarer need.launchtakes the spawn-level options only. Playwright's context-level ones (locale,timezoneId,colorScheme,extraHTTPHeaders,offline) need emulation on every window and are a follow-up.
Issue
None.
Verified
Ran it locally: yes
Surface: the built CLI on apps/electron-testbed, and on Insomnia built from adc07b8.
packages/electron unit Tests 103 passed (103)
packages/electron integration Tests 46 passed (46)
apps/electron-testbed Tests 87 passed (87)
pnpm check exit 0
Three of the fixes were checked by removing the fix, rebuilding, and running tests/controls.e2e.ts. The text-run row was checked on the previous head, before the rule became structural:
| Fix | Test | Fix removed | This PR |
|---|---|---|---|
| Icon glyph in a name | names a button by its text, not its icon font's glyph | fails | passes |
| Tap on folded text | taps text where it is, not the middle of the container | fails | passes |
| Focus handed to a child | focuses a composite that hands focus to a part of itself | fails | passes |
The held modifier has a unit test in tests/unit/modifiers.test.ts and a testbed case. The closed-window retry has no test of its own: I could not make the window close between the read and the query on purpose. Before the fix the ported oauth spec failed 1 run in 4 with APP_NOT_OPEN; after it, 3 of 3 pass with the workaround removed.
Insomnia, the same 31 specs, serial, no retries, one run each on one Mac mini (Playwright 1.59.1 with tracing off, Electron 43.2.0):
| Playwright | e2e | |
|---|---|---|
| Tests passed | 110 of 113 | 100 of 102 run (3 marked fixme) |
| Time for the 100 tests that pass in both | 327.5 s | 288.0 s |
- Playwright's three failures: the two "AI URL settings" tests time out because this build has no
@kong/insomnia-plugin-ai(fixme in the port), and "manage environment" is an app race the original documents. - e2e's two failures: "manage environment" again (1 of 3 on repeat), and one import dialog that did not show its button once and then passed 9 of 9.
- Playwright has 9 more tests: the
runnerport covers 7 of its 15, and onesidebar-delete-shortcuttest is fixme in the port. - e2e is 1.14x faster on the matched tests. App launch and the app's own waits dominate both, and one run each is too few to read small per-spec differences.
pnpm bench:ab: not run, it has no Electron benchmark. No video: the changes are asserted by the testbed tests above and add no on-screen behavior beyond them.
Summary by CodeRabbit
- New Features
- Configure Electron launch arguments, environment variables, working directory, and user-data directory per test.
- Use keyboard and mouse input, access the active window, evaluate page code, and reload windows.
- Bug Fixes
- Improved accessible button names and text targeting, including disabled buttons.
- Fixed focus handling for composite controls and recovery when a window closes during an action.
- Documentation
- Added Electron API guidance and Playwright migration examples for the new options and controls.