feat(electron): add @e2e-dev/electron, the reference third-party engine (ENG-892)
What
A new package, @e2e-dev/electron, tests Electron apps:
import { electron } from '@e2e-dev/electron';
export default {
targets: [{ name: 'electron', engine: electron(), app: { appPath: '.' } }],
} satisfies E2EConfig;
app.open() launches the app on an empty user-data directory. screen, expect, and the agent work on its windows as they do on a browser page. The electron fixture adds evaluate (in the main process, typed by the project's own electron package), windows, waitForWindow, window, waitForDownload, and locator. Windows are handles shaped like the web engine's tabs: title(), url(), isActive(), isClosed(), activate(), close().
It records video (--video, app.startRecording()): the shown window's DevTools screencast, encoded to one VP9 .webm by ffmpeg, following window switches and app.restart(). ffmpeg is E2E_FFMPEG_PATH, else one on the PATH with VP9, else the pinned ffmpeg-static b6.1.1 build downloaded into the e2e cache on the first recording, checked against its SHA-256. app.back(), toBeInViewport, computed-style reads, and in-memory setInputFiles work too.
The package uses only the public engine contract, so it is the reference for an engine built outside this repository. @e2e-dev/core/engine gets three exports from it, expressionSelectors, readPngSize, and sleep, and the textQuery option of resolveExpression. The web and mobile engines now read a screenshot's size with core's readPngSize, so both need @e2e-dev/core and e2e 0.25.1. docs/engines.mdx lists the contract's assumptions the engine broke.
Why
ENG-892. Users drive Electron apps today by attaching web() to the app's DevTools port, which breaks in several ways. Electron 22 has no Browser.setDownloadBehavior and no defaultBrowserContextId. The attach starts on about:blank, so goto refuses the app's file: page, and the preload's IPC bridge is gone. An engine that launches the app itself has none of these problems.
The ticket also asks for this engine to be the test of the engine contract: it may use nothing a third party could not, and what it needs from core becomes public contract.
How it works
flowchart LR
T[test / agent] --> R[runner]
R -- engine protocol --> E["electron() (defineEngine)"]
E -- "spawn" --> A[Electron app]
E -- "CDP over --remote-debugging-pipe" --> W[each window]
E -- "Node inspector (--inspect=0)" --> M[main process]
surface.ts holds the contract methods and the attempt's state; each part below is a module that owns one kind of state.
- Protocol (
packages/electron/src/cdp/connection.ts:139). The engine speaks the Chrome DevTools Protocol itself and has no runtime dependency beside@e2e-dev/core.Connectioncarries flat sessions over one transport: the NUL-separated pipe a browser opens with--remote-debugging-pipe(connection.ts:71), or a WebSocket for the Node inspector. Commands and events are typed bydevtools-protocol, a dev dependency the published types never reach.cdp/input.tssends keys and mouse events throughInput.*with a US key table. Dropped alternative: Playwright's_electron, which an earlier head used. It waits on every window and on the inspector, so an app with a blank window or theEnableNodeCliInspectArgumentsfuse off never launched. - Launch (
app-process.ts:121,launcher.ts:79).preparedownloads the Electron binary once per run, since Electron 45 has no install script (executable.ts). Each attempt gets its own--user-data-dir.app.open()has nosession.opento call, so the runner callssession.restart, which spawns the binary in a process group of its own with--inspect=0 --remote-debugging-pipe --force-color-profile=srgb, so a capture has the same pixels on every display. No DevTools port is opened. A page target becomes a window once it answersPage.enable, so a window that loaded nothing is not waited on (app-process.ts:214). The launch returns on the first open window with a parsed document (app-process.ts:414), which need not be the first one opened: a splash screen may still be loading. Launches and quits run one at a time. A quit has Electron ignorebeforeunloadguards, then kills the process group and waits for it before the user-data directory is deleted (app-process.ts:390). A cancelled or failed launch kills the group too. The app is started with an allowlist of the run's environment (INHERITED_ENV,app-process.ts) pluselectron({ env }), so provider keys and tokens stay with the runner. Downloads go to a directory inside the attempt's user-data directory. A launch that fails says why: the app's last stderr lines, the app quitting before its first window, a window that loaded nothing, or Electron never firingready.initanddoctorrefuse anappPathwith no main script, and the options refuse launch switches the engine owns (options.ts). - Main process (
main-process.ts:58).electron.evaluateruns over the Node inspector the app prints on stderr, reached with Node's built-inWebSocket. The argument goes as JSON and the result comes back as plain data (functions and cycles dropped). An app with no inspector still launches:screenand the agent work,electron.evaluatefails with a message naming the fuse, and downloads are placed withBrowser.setDownloadBehavior(default session only). - Windows (
windows.ts). The app's windows areEngineSnapshot.tabs,switchTab, andcloseTab;closeTabrefuses the last window. The fixture hands out one handle per window, shaped like the web engine's tabs:electron.waitForWindow(trigger)makes the window a step opens the active one,electron.window({ title })finds one by the document's or the OS window's title and waits for it, and a handle'sactivate()andclose()do what they say (windows.ts,electron.ts). When the shown window closes, reads go to the window that opened it, else the one shown before it, as a closed tab hands back to its opener on the web. A read that finds its window closed before the close event arrives drops it and reads again (windows.ts:66). A crashed renderer fails reads withAPP_NOT_OPENuntil the app reloads the window. Analertorconfirmgoes to the handlerelectron.onDialog('accept' | 'dismiss' | fn)registered, the web engine'sbrowser.onDialog(dialogs.ts); with no handler it is dismissed and the test's next step fails withunhandled confirm dialog, as on the web without a handler. A node id carries its window and which of the window's documents it was read in (windows.ts:33), so a ref from before a navigation or a crash is stale instead of naming whatever node the next renderer gives the same number. - Read (
locate.ts:32).observereadsAccessibility.getFullAXTreeand oneDOMSnapshot.captureSnapshot(with each node's computedvisibility) and joins them on backend node ids.projectTree(tree.ts:368) is pure, and its tests run on a read captured from the testbed (packages/electron/tests/fixtures/sign-in.json). It maps Chromium's roles to the semantic tree with the web engine's rules: values,empty,readonly(aria-readonlyon the roles that take it),level, ARIA range values as written,secure, and table cells read apart. Nested documents (iframe,webview) are not read. - Locate (
locate.ts:87). Locators go through core'sresolveExpression. Itsselectoroption answers synchronously, but this engine's CSS queries go over the DevTools protocol. SoexpressionSelectors(packages/core/src/engine/resolve.ts:62) lists the expression's selectors, and the engine queries each one first in an isolated world, in the document and in every open shadow root (devtools.ts:148). The hook answers in document order, as the contract asks. - Page-side code (
in-page/node-ops.ts:59). The checks and edits that run in the page are typed functions, sent as source text withtoString(), as the web engine sends its reader, and tested in Chromium (tests/integration/node-ops.test.ts). - Act (
actions.ts:53, one function per action kind).Budget.send(budget.ts:42) refuses input after the deadline or an abort, checked before every key of a chord (actions.ts:346). One gesture runs on a page at a time (page.ts:130), so a cancelled gesture releases what it pressed before the next input goes out. Before an action, the engine waits until the target is enabled, visible, and not inert, scrolls it into view (to the window's centre after something covered it), and hit-tests the click point (actionability.ts:55). A node the app re-rendered fails as stale, so the harness locates it again. An action that may have committed when it failed reportsACTION_MAY_HAVE_COMMITTED. - Secrets (
capture.ts). A screenshot masks password fields and nested documents, with animations stopped. The masks are a popover the engine paints itself (in-page/masks.ts:23), in the top layer so a modal dialog cannot cover them, and one capture runs per page at a time. The capture is withheld when a mask is gone, altered, or does not cover its element (a zoomed root), or when the document changed under it or could not be read. Their styles are!important, so the app's own rules cannot hide them. Before Electron 25 there is no popover: the layer is the topmost of the page's own, and a capture is withheld while a modal<dialog>or a fullscreen element is open. A window that shows afile:location outside the app's directory (app.getAppPath(),file-bound.ts) is not observed, traced, or captured:observeandapp.screenshot()fail withPOLICY_DENIED. An Electron target has noapp.urlfor core's file-app bound to key on, so the engine holds the bound itself. A test's own locators still read such a window, as on the web. A password field or nested document inside a closed shadow root is out of any mask's reach:unmaskableCount(tree.ts:636) finds them in the DOM snapshot before and after the capture, and the agent's observation carries no pixels andapp.screenshot()fails withPOLICY_DENIED. A masked element that appears during the capture withholds it too.electron.evaluaterefuses a secret as its argument. - Core (
packages/core/src/engine/png.ts:18,packages/core/src/engine/index.ts).readPngSizeandsleepjoin@e2e-dev/core/engine; the web and mobile engines drop their own PNG readers for it.app.open(path)on a target with no app URL namesapp.linkBaseordevice.openLinkonly when the engine opens links (packages/core/src/run/fixtures.ts). - Testbed and docs.
apps/electron-testbedis a small Electron app with context isolation, a preload IPC bridge, a second window, open and closed shadow roots, and a controls page, with 60 deterministic tests and two agent tests. Theelectronjob in.github/workflows/spec.ymlruns the package's integration tests and the testbed under xvfb.pnpm --filter @e2e-dev/electron testis unit tests only;test:integrationlaunches real Electron and needs a display. Docs:docs/electron.mdx(new), an Electron section indocs/migrate/playwright.mdx, the assumptions table indocs/engines.mdx,expressionSelectors,readPngSize, andsleepindocs/reference/engine.mdx,ELECTRON_INSTALL_FAILEDindocs/reference/errors.mdx, thee2eskill,README.md,AGENTS.md, andCONTRIBUTING.md.
What stays open, for a maintainer:
- Some differences from
web()come from core'sresolveExpression, which the mobile engine shares:getByLabelmatches any node's accessible name, role states such as{ expanded: false }match a node with no such state, and role queries skip a node with no size.docs/electron.mdxlists them with every other difference. Changing them changes mobile too.
Issue
ENG-892
Verified
Ran it locally: yes (macOS arm64 and a node:24-bookworm container under xvfb, Electron 45.5.0)
| Suite | macOS | Linux and Windows |
|---|---|---|
pnpm --filter @e2e-dev/electron test (unit) | 97 passed | CI |
pnpm --filter @e2e-dev/electron run test:integration (real Electron) | 42 passed | CI |
pnpm --filter @e2e-dev/electron-testbed test | 73 passed | CI |
- The testbed covers sign-in through the preload IPC bridge,
electron.evaluate, every field kind, ARIA widgets, the keyboard, HTML drag and drop with data, scrolling, re-rendered and covered buttons, a fresh launch per test,app.restart()through abeforeunloadguard, an app that quits on its own, a crashed renderer, downloads kept in the attempt, the second window, masked and withheld screenshots (a password field inside a modal dialog too), text that inline markup splits and text underaria-hidden, a checkbox its app replaces, a custom radio, a hidden fieldfillrefuses, an upload through a button's file chooser, a dialog nothing answers, and a window that left the app for a local file. - The integration tests launch scratch apps: a blank window beside a loaded one, a view that loads nothing, a splash screen that closes or never finishes loading, a slow first page, an app with no inspector (opens, warns, saves a download in the attempt), a cancelled launch (no process left), a binary that cannot start, and screenshots on a 2x display, under CSS zoom, taken three at once, with a page script that removes the masks, under app styles marked
!important, and with a field that appears or moves after the masks are placed. They also cover bothdialogssettings. - On a real Electron 22.3.27 binary (Chromium 108, no popover) a password field was captured masked, and the same field inside a modal dialog was withheld.
- A binary with the
EnableNodeCliInspectArgumentsfuse flipped off launched in about 0.7 s;screenworked andelectron.evaluatefailed with the message naming the fuse. - Killing the runner with SIGINT, SIGTERM, or SIGKILL mid-test left no Electron process.
- The agent suite (
test:agentin the testbed) passed 2 of 2 with a real model on macOS. app.screenshot('filled'). The engine painted the password field black, and the username field beside it stays readable:
- Benchmark table: not captured. On
mainthis engine does not exist, so there is nothing to compare it with. - Video:
--video onover the sign-in suite wrote 32 VP9 files; frames extracted from one show the window. An integration test records two windows of different colours and checks the first and last frame of the file. - A hidden or minimized window: Chromium draws nothing of one, so a screenshot never returned on Windows and Linux. The engine runs a small screencast around each capture, which counts the page as captured; a testbed test hides the window and screenshots it on all three platforms.
- Windows: the
electron-windowsjob inspec.ymlruns the unit, integration, and testbed suites onwindows-latest. Linux runs in theelectronjob. The earlier Linux container run (39 integration, 60 testbed) predates the last three commits. HTML drag and drop is unverified on a foreground macOS window.
Checklist
- Changesets:
@e2e-dev/electron(new package, andevaluate's types),@e2e-dev/corepatches (expressionSelectors,readPngSizeandsleep, theapp.open(path)message), and@e2e-dev/weband@e2e-dev/mobilepatches (core'sreadPngSize, peer floors 0.25.1).e2emoves with core in the fixed group. - Breaking change: none.
- Docs updated in the same PR, and
skills/e2e/. - Contract change:
expressionSelectors,readPngSize, andsleepare new exports on@e2e-dev/core/engine(ande2e/engine), andResolveExpressionOptions.textQuerya new option. No wire change.
🤖 Generated with Claude Code