feat(electron): per-test launch, keyboard, and window evaluate

Merged
OK
2d ago
+917-152

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) and reload() on a window, and the engine option electron({ cwd }).
  • Fixed: a button with an icon-font glyph is named by its text, getByText answers 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

  1. Launch checks. checkLaunchOptions validates what electron.launch() is given with the same checks the config gets (checkEnv, checkCwd, ownedArgument), as INVALID_ARGUMENT instead of INVALID_CONFIG (packages/electron/src/options.ts:146). A secret anywhere in the options is POLICY_DENIED (packages/electron/src/electron.ts).
  2. Launch. launchWith stores the checked extras inside the serialized launch queue and restarts (packages/electron/src/surface.ts:186). AppProcess.start appends the arguments after the target's, lays the variables over the engine's env (launchEnv replaces a name in any case on Windows, where path and PATH are one variable), and resolves cwd from the project root. startAttempt clears them, so the next test starts without them. Another launch() replaces them.
  3. userDataDir. The attempt's directory already exists from startAttempt; the getter exposes it before the launch.
  4. Keyboard and mouse. Every call goes through one surface.input, which runs it on the same act path as a locator action: in turn after earlier gestures, bounded by the action timeout (surface.ts:404). keyboard.type shares typeKeys with locator typing (packages/electron/src/actions.ts) and gets the action timeout plus its delays.
  5. Held modifiers. withModifiers skips a modifier the keyboard already holds, so keyboard.down('Shift') survives press('Shift+Tab') and a tap with modifiers (actions.ts:435).
  6. Evaluate. electron.evaluate and window.evaluate share callForPlainData: the function as source text, the argument as a protocol value, the result through plainData (packages/electron/src/evaluate.ts:28). A thrown error is EVALUATE_FAILED in both.
  7. 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).
  8. 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).
  9. 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 answers getByText with 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.
  10. Closed window. locate reads the next shown window when the one it read closes before the query. Both it and readShown decide that through closedUnder (surface.ts:259). Each retry discards a window, so the loop ends.
  11. Focus. The in-page focus op answers held when the active element is inside the target (packages/electron/src/in-page/node-ops.ts).

Decisions a reviewer may ask about:

  • launch options 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.
  • launch takes 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:

FixTestFix removedThis PR
Icon glyph in a namenames a button by its text, not its icon font's glyphfailspasses
Tap on folded texttaps text where it is, not the middle of the containerfailspasses
Focus handed to a childfocuses a composite that hands focus to a part of itselffailspasses

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):

Playwrighte2e
Tests passed110 of 113100 of 102 run (3 marked fixme)
Time for the 100 tests that pass in both327.5 s288.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 runner port covers 7 of its 15, and one sidebar-delete-shortcut test 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.