feat(electron): add @e2e-dev/electron, the reference third-party engine (ENG-892)

Merged
KD
2d ago
+11348-86

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.

  1. 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. Connection carries 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 by devtools-protocol, a dev dependency the published types never reach. cdp/input.ts sends keys and mouse events through Input.* 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 the EnableNodeCliInspectArguments fuse off never launched.
  2. Launch (app-process.ts:121, launcher.ts:79). prepare downloads 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 no session.open to call, so the runner calls session.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 answers Page.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 ignore beforeunload guards, 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) plus electron({ 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 firing ready. init and doctor refuse an appPath with no main script, and the options refuse launch switches the engine owns (options.ts).
  3. Main process (main-process.ts:58). electron.evaluate runs over the Node inspector the app prints on stderr, reached with Node's built-in WebSocket. The argument goes as JSON and the result comes back as plain data (functions and cycles dropped). An app with no inspector still launches: screen and the agent work, electron.evaluate fails with a message naming the fuse, and downloads are placed with Browser.setDownloadBehavior (default session only).
  4. Windows (windows.ts). The app's windows are EngineSnapshot.tabs, switchTab, and closeTab; closeTab refuses 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's activate() and close() 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 with APP_NOT_OPEN until the app reloads the window. An alert or confirm goes to the handler electron.onDialog('accept' | 'dismiss' | fn) registered, the web engine's browser.onDialog (dialogs.ts); with no handler it is dismissed and the test's next step fails with unhandled 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.
  5. Read (locate.ts:32). observe reads Accessibility.getFullAXTree and one DOMSnapshot.captureSnapshot (with each node's computed visibility) 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-readonly on the roles that take it), level, ARIA range values as written, secure, and table cells read apart. Nested documents (iframe, webview) are not read.
  6. Locate (locate.ts:87). Locators go through core's resolveExpression. Its selector option answers synchronously, but this engine's CSS queries go over the DevTools protocol. So expressionSelectors (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.
  7. 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 with toString(), as the web engine sends its reader, and tested in Chromium (tests/integration/node-ops.test.ts).
  8. 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 reports ACTION_MAY_HAVE_COMMITTED.
  9. 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 a file: location outside the app's directory (app.getAppPath(), file-bound.ts) is not observed, traced, or captured: observe and app.screenshot() fail with POLICY_DENIED. An Electron target has no app.url for 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 and app.screenshot() fails with POLICY_DENIED. A masked element that appears during the capture withholds it too. electron.evaluate refuses a secret as its argument.
  10. Core (packages/core/src/engine/png.ts:18, packages/core/src/engine/index.ts). readPngSize and sleep join @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 names app.linkBase or device.openLink only when the engine opens links (packages/core/src/run/fixtures.ts).
  11. Testbed and docs. apps/electron-testbed is 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. The electron job in .github/workflows/spec.yml runs the package's integration tests and the testbed under xvfb. pnpm --filter @e2e-dev/electron test is unit tests only; test:integration launches real Electron and needs a display. Docs: docs/electron.mdx (new), an Electron section in docs/migrate/playwright.mdx, the assumptions table in docs/engines.mdx, expressionSelectors, readPngSize, and sleep in docs/reference/engine.mdx, ELECTRON_INSTALL_FAILED in docs/reference/errors.mdx, the e2e skill, README.md, AGENTS.md, and CONTRIBUTING.md.

What stays open, for a maintainer:

  • Some differences from web() come from core's resolveExpression, which the mobile engine shares: getByLabel matches 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.mdx lists 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)

SuitemacOSLinux and Windows
pnpm --filter @e2e-dev/electron test (unit)97 passedCI
pnpm --filter @e2e-dev/electron run test:integration (real Electron)42 passedCI
pnpm --filter @e2e-dev/electron-testbed test73 passedCI
  • 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 a beforeunload guard, 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 under aria-hidden, a checkbox its app replaces, a custom radio, a hidden field fill refuses, 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 both dialogs settings.
  • 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 EnableNodeCliInspectArguments fuse flipped off launched in about 0.7 s; screen worked and electron.evaluate failed with the message naming the fuse.
  • Killing the runner with SIGINT, SIGTERM, or SIGKILL mid-test left no Electron process.
  • The agent suite (test:agent in 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:

the testbed window with its password field masked

  • Benchmark table: not captured. On main this engine does not exist, so there is nothing to compare it with.
  • Video: --video on over 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-windows job in spec.yml runs the unit, integration, and testbed suites on windows-latest. Linux runs in the electron job. 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, and evaluate's types), @e2e-dev/core patches (expressionSelectors, readPngSize and sleep, the app.open(path) message), and @e2e-dev/web and @e2e-dev/mobile patches (core's readPngSize, peer floors 0.25.1). e2e moves with core in the fixed group.
  • Breaking change: none.
  • Docs updated in the same PR, and skills/e2e/.
  • Contract change: expressionSelectors, readPngSize, and sleep are new exports on @e2e-dev/core/engine (and e2e/engine), and ResolveExpressionOptions.textQuery a new option. No wire change.

🤖 Generated with Claude Code