fix(electron): count the trigger in waitForWindow's and waitForDownload's timeout

Merged
KD
21h ago
+397-32

What

electron.waitForWindow(trigger) and electron.waitForDownload(trigger) now count the trigger in their timeout and stop when their step is cancelled, as browser.waitForPopup() and browser.waitForDownload() do on the web:

  • A trigger that never settles fails the wait at its timeout with electron.waitFor... exceeded its timeout of <n>ms while its trigger ran. A window or download that arrives after the time ran out is not taken.
  • A step cancelled before the trigger runs, while it runs, or while the engine reads the app's downloads ends as CANCELLED at once, runs no trigger it has not started, and shows no window the trigger opened.
  • waitForDownload ends in its time while the app's main process is too busy to answer (no download finished within <n>ms: the app's main process did not answer). On an app with no inspector it fails with UNSUPPORTED_CAPABILITY, and the no-inspector warning now names it.
  • A download the engine saves is numbered around a file of that name the app wrote or saved in the downloads folder itself (note (1).txt) instead of replacing it. A file the app saved elsewhere does not take a name there.

Why

The waits started their clock after the trigger returned. A trigger that hung (a click that never resolved, a dialog it waited on) hung the step past its timeout, and a cancelled step waited for the trigger to finish. Downloads were numbered only against the folder at launch and the engine's own downloads, so a later download of the same name overwrote a file the app had written there.

How it works

  1. runTrigger runs the trigger inside raceAbort, under withTimeout against the wait's one deadline (packages/electron/src/surface.ts:681). A step cancelled before the trigger starts never calls it, and a trigger that returns no promise (plain JavaScript test code) is wrapped in an async function.
  2. waitForWindow takes its deadline before the trigger and throws timedOut() once the trigger used it up, then polls for a new window under the same deadline (surface.ts:508-527). activateWindow raises the window, checks the budget again, and only then makes it the window screen reads, so a step cancelled at any point before that leaves the old window active (surface.ts:456-466).
  3. waitForDownload reads the app's downloads before and after the trigger with withTimeout and raceAbort against that deadline (surface.ts:594-630). A read that times out fails with OPERATION_TIMEOUT. pollCondition ends the poll with the ordinary no-download timeout when that read started with under one poll tick left, as the last read at the deadline does. Any other read timeout, the one before the trigger included, becomes no download finished within <n>ms: the app's main process did not answer. A step cancelled during or right after the first read, or while the trigger runs, ends as CANCELLED with the message electron.waitForDownload cancelled.
  4. placeDownloads numbers a name against the names it gave out, the folder at launch, the file on disk (through process.getBuiltinModule('node:fs'), so Electron 32 and later), and the name of any path the app itself chose in the downloads folder (packages/electron/src/app-process.ts:569-614).
  5. The guide says where downloads go, names the two exceptions (a path the app picks itself, a download before the first window loads), and links the reference, which holds the numbering rules and the extra ACTION_FAILED messages.

Verified

Ran it locally: yes, on macOS (Apple Silicon) with Electron 45.5.0.

  • At this head, the Electron package's unit tests (117, with the new tests/unit/download-wait.test.ts), the download integration tests in tests/integration/launch.test.ts (-t download, 6 tests, run twice), typecheck, and oxlint pass. The testbed's download tests in tests/parity.e2e.ts passed three runs in a row through the built CLI. tests/integration/launch.test.ts as a whole, the rest of the testbed's tests/parity.e2e.ts and tests/sign-in.e2e.ts, and the docs checks last ran at a6535dd9b. The package's other integration tests, the whole testbed (pnpm --filter @e2e-dev/electron-testbed test), and pnpm check last ran at a418b54d5. The commits since then change activateWindow, the download wait's timeout messages, testbed assertions, tests, and docs.
  • The new unit test that lists downloads in 5 ms fails without 539067fda: the last read gets 1 ms and the wait says the main process did not answer. The new integration test numbers a download whose name the app took for one still downloading, with no file of that name on disk fails when the app-chosen name is not reserved in placeDownloads. The older collision test still passes without that reservation.
  • main against this branch: this branch's tests run against origin/main's packages/electron/src. These tests fail there and pass here:
SuiteTestmainthis PR
integrationelectron.waitForWindow shows no window for a wait cancelled while its trigger ranfailspasses
integrationelectron.waitForWindow shows no window for a wait cancelled while the window is brought to the frontfailspasses
integrationelectron.waitForWindow ends as cancelled, then, when its step is cancelled while its trigger still runsfailspasses
integrationelectron.waitForWindow runs no trigger for a step cancelled before it startsfailspasses
integrationelectron.waitForDownload fails in its time, and runs no trigger, while the app's main process is too busy to answerfailspasses
integrationelectron.waitForDownload ends as cancelled, at once, when its step is cancelled while the busy main process is askedfailspasses
integrationelectron.waitForDownload runs no trigger for a step cancelled while the app was asked for its downloadsfailspasses
integrationdownloads numbers a download whose name the app took itself, by a download or a file written after launchfailspasses
testbedtests/parity.e2e.ts > what an Electron target shares with the web > waits for a download and keeps the file with the attempt, and says when none startsfailspasses
testbedtests/sign-in.e2e.ts > windows > drives a second window the app opensfailspasses
  • Benchmark: pnpm bench:ab --suite electron-testbed, the merge base with main against this head, on a Mac. Both builds ran with -ApplePersistenceIgnoreState (#2500) applied locally and uncommitted, since without it a launch after a killed app stalls 120 s on the reopen-windows alert. The "uncommitted changes" in the header are that line.
    Result: the 84 tests whose behavior matches run in the same time (suite +0.0%, 95% CI -0.2% to +0.2%), and every step is the same. The process wall clock is 4 s longer (+6.3%) because of the tests this PR adds or changes, which bench:ab leaves out of the timing rows: the new test that keeps the main process busy for 2 s, the no-download case that now waits 1000 ms, and the two window tests that add a trigger that times out.
bench:ab output

bench-ab: 7c1b2b25 (merge base with origin/main) vs 7b0cf60a + uncommitted changes

apps/electron-testbed: e2e run --workers 1. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 2.8.

The suite's tests or config differ between base and head, so each side ran its own. A timing change can be the test change.

Behavior

These tests are left out of the timing rows below.

  • added tests/parity.e2e.ts › what an Electron target shares with the web › says the app did not answer when its main process is too busy to list its downloads (electron)

  • changed tests/parity.e2e.ts › what an Electron target shares with the web › waits for a download and keeps the file with the attempt, and says when none starts (electron)

    + resource electron.waitForDownload "" failed ACTION_FAILED
    
  • changed tests/sign-in.e2e.ts › windows › closes a window from its handle, and the one active before it is active again (electron)

    + resource electron.waitForWindow "" passed
    + locator locator.tap "getByRole(\"button\", name: \"Open settings\")" passed
    + resource window.title "" passed
    
  • changed tests/sign-in.e2e.ts › windows › drives a second window the app opens (electron)

    + resource electron.waitForWindow "" failed ACTION_FAILED
    + locator locator.tap "getByRole(\"button\", name: \"Open settings\")" passed
    

Timings

Every row but the process wall clock covers the 84 tests whose behavior matched.

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)63.55 s (63.50 s-63.59 s)67.53 s (67.47 s-67.53 s)+6.3% (+6.0% to +6.4%)slower
suite (sum of attempts)60.02 s (60.00 s-60.06 s)60.04 s (59.99 s-60.06 s)+0.0% (-0.2% to +0.2%)same
action (events)16.78 s (16.78 s-16.79 s)16.78 s (16.77 s-16.79 s)+0.0% (-0.1% to +0.1%)same, under the floor
step app.open23.71 s (23.70 s-23.71 s)23.71 s (23.70 s-23.72 s)+0.0% (-0.0% to +0.1%)same, under the floor
step locator.selectOption4.72 s (4.72 s-4.72 s)4.72 s (4.72 s-4.73 s)+0.1% (+0.0% to +0.2%)same, under the floor
step locator.tap4.29 s (4.28 s-4.30 s)4.29 s (4.28 s-4.29 s)+0.0% (-0.6% to +0.4%)same, under the floor
step locator.fill3.37 s (3.37 s-3.37 s)3.37 s (3.37 s-3.37 s)-0.0% (-0.3% to +0.1%)same, under the floor
step app.restart1.97 s (1.97 s-1.98 s)1.97 s (1.97 s-1.98 s)+0.2% (-0.9% to +0.8%)same, under the floor
step locator.dragTo1.43 s (1.43 s-1.43 s)1.43 s (1.43 s-1.43 s)+0.1% (-0.0% to +0.5%)same, under the floor
step locator.pressSequentially1.08 s (1.08 s-1.08 s)1.08 s (1.08 s-1.08 s)+0.1% (-0.2% to +0.2%)same, under the floor
step window.title1.00 s (1.00 s-1.00 s)1.00 s (1.00 s-1.00 s)0.0% (-0.1% to +0.1%)same
step locator.scrollIntoView925 ms (924 ms-926 ms)926 ms (925 ms-926 ms)+0.1% (-0.2% to +0.2%)same, under the floor
step electron.launch913 ms (911 ms-913 ms)913 ms (913 ms-914 ms)+0.1% (-0.2% to +0.2%)same, under the floor
step locator.longPress904 ms (904 ms-905 ms)904 ms (903 ms-904 ms)-0.1% (-0.3% to +0.2%)same, under the floor
step locator.waitFor902 ms (902 ms-902 ms)901 ms (901 ms-902 ms)-0.1% (-0.2% to +0.1%)same, under the floor
step expect.toHaveValue739 ms (735 ms-743 ms)743 ms (738 ms-746 ms)+0.3% (-0.9% to +2.0%)same, under the floor
step locator.setInputFiles571 ms (570 ms-573 ms)571 ms (569 ms-571 ms)-0.3% (-0.4% to -0.1%)same, under the floor
step expect.not.toHaveValue516 ms (514 ms-518 ms)511 ms (510 ms-512 ms)-0.9% (-1.5% to -0.2%)same, under the floor
step electron.evaluate468 ms (462 ms-470 ms)468 ms (465 ms-470 ms)-0.1% (-1.7% to +2.7%)same, under the floor
step expect.toBeVisible433 ms (421 ms-436 ms)427 ms (424 ms-430 ms)-1.5% (-3.1% to +3.0%)same, under the floor
step app.clearState331 ms (329 ms-332 ms)333 ms (332 ms-336 ms)+0.8% (+0.0% to +1.8%)same, under the floor
step expect.toHaveText329 ms (324 ms-335 ms)324 ms (322 ms-326 ms)-1.4% (-3.7% to +0.8%)same, under the floor
step locator.check302 ms (301 ms-305 ms)303 ms (301 ms-303 ms)-0.2% (-2.0% to +2.0%)same, under the floor
step electron.waitForWindow260 ms (256 ms-263 ms)258 ms (255 ms-260 ms)+0.4% (-3.2% to +2.0%)same, under the floor
step app.screenshot120 ms (118 ms-126 ms)117 ms (116 ms-124 ms)-1.3% (-10.6% to +8.7%)same, under the floor
step expect.toHaveCount101 ms (99 ms-104 ms)99 ms (97 ms-100 ms)-0.0% (-8.5% to +3.7%)same, under the floor
step app.stopRecording84 ms (80 ms-84 ms)77 ms (77 ms-82 ms)-7.0% (-8.9% to +3.8%)same, under the floor
step electron.window51 ms (51 ms-51 ms)51 ms (51 ms-52 ms)+1.0% (0.0% to +2.0%)same, under the floor
step locator.press49 ms (48 ms-50 ms)50 ms (49 ms-50 ms)+2.1% (-3.8% to +5.2%)same, under the floor
step locator.clear48 ms (47 ms-50 ms)49 ms (48 ms-49 ms)+2.1% (-1.0% to +2.2%)same, under the floor
step locator.focus36 ms (35 ms-37 ms)35 ms (34 ms-35 ms)-4.2% (-10.3% to 0.0%)same, under the floor
step electron.keyboard.press34 ms (33 ms-36 ms)36 ms (35 ms-37 ms)+4.5% (-0.0% to +10.8%)same, under the floor
step locator.swipe34 ms (33 ms-37 ms)36 ms (34 ms-37 ms)+5.9% (-7.9% to +26.0%)same, under the floor
step expect.toBeChecked31 ms (29 ms-33 ms)29 ms (28 ms-30 ms)-7.9% (-17.0% to +15.3%)same, under the floor
step app.startRecording22 ms (21 ms-22 ms)22 ms (22 ms-22 ms)0.0% (-2.2% to +7.3%)same
step expect.toBeHidden21 ms (21 ms-23 ms)22 ms (21 ms-23 ms)0.0% (-2.2% to +15.0%)same
step expect.toBeDisabled20 ms (20 ms-20 ms)21 ms (19 ms-21 ms)+5.0% (-4.9% to +5.3%)same, under the floor
step electron.keyboard.type19 ms (19 ms-19 ms)19 ms (18 ms-19 ms)-2.5% (-5.3% to +2.6%)same, under the floor
step expect.not.toBeChecked19 ms (17 ms-19 ms)18 ms (17 ms-20 ms)+0.5% (-19.6% to +25.7%)same, under the floor
step expect.toBeEditable18 ms (17 ms-18 ms)18 ms (18 ms-19 ms)0.0% (-2.8% to +11.8%)same
step locator.hover16 ms (15 ms-17 ms)16 ms (15 ms-17 ms)+6.5% (-28.0% to +13.9%)same, under the floor
step locator.uncheck15 ms (15 ms-16 ms)16 ms (15 ms-16 ms)0.0% (0.0% to +9.5%)same
step electron.mouse.down15 ms (14 ms-15 ms)15 ms (14 ms-15 ms)-3.2% (-6.7% to +7.1%)same, under the floor
step expect.toHaveAttribute13 ms (12 ms-13 ms)13 ms (12 ms-13 ms)0.0% (-7.7% to +18.2%)same
step locator.dblclick12 ms (12 ms-12 ms)12 ms (12 ms-12 ms)0.0% (-7.4% to +4.4%)same
step electron.mouse.up11 ms (11 ms-11 ms)11 ms (11 ms-11 ms)0.0% (0.0% to +4.9%)same
step expect.not.toBeEmpty11 ms (10 ms-11 ms)11 ms (10 ms-12 ms)-4.3% (-13.8% to +34.2%)same, under the floor
step electron.keyboard.down11 ms (10 ms-11 ms)10 ms (10 ms-11 ms)0.0% (-9.1% to +4.9%)same
step expect.toBeInViewport11 ms (10 ms-11 ms)11 ms (11 ms-11 ms)+9.5% (-4.7% to +10.0%)same, under the floor
step electron.keyboard.up10 ms (9 ms-10 ms)10 ms (10 ms-10 ms)0.0% (0.0% to +11.1%)same
step expect.toBeEmpty10 ms (10 ms-10 ms)10 ms (9 ms-10 ms)-5.1% (-18.4% to +5.4%)same, under the floor
step expect.not.toBeInViewport9 ms (9 ms-10 ms)10 ms (9 ms-10 ms)+5.4% (-5.1% to +17.9%)same, under the floor
step electron.mouse.move9 ms (7 ms-10 ms)10 ms (9 ms-10 ms)+5.4% (-13.0% to +43.6%)same, under the floor
step expect.not.toBeEditable8 ms (8 ms-8 ms)9 ms (8 ms-9 ms)+12.5% (0.0% to +12.5%)same, under the floor
step expect.toContainText8 ms (7 ms-8 ms)8 ms (7 ms-8 ms)+6.9% (-12.5% to +15.5%)same, under the floor
step window.reload7 ms (7 ms-8 ms)8 ms (7 ms-8 ms)+6.9% (-6.5% to +14.3%)same, under the floor
step expect.not.toHaveAttribute6 ms (6 ms-6 ms)6 ms (6 ms-6 ms)0.0% (0.0% to 0.0%)same
step expect.not.toBeHidden6 ms (5 ms-6 ms)5 ms (5 ms-6 ms)-16.7% (-16.7% to +20.0%)same, under the floor
step screen.tree5 ms (5 ms-5 ms)5 ms (5 ms-5 ms)0.0% (-10.6% to 0.0%)same
step electron.mouse.wheel5 ms (5 ms-5 ms)5 ms (5 ms-6 ms)0.0% (-8.7% to +20.0%)same
step electron.keyboard.insertText5 ms (5 ms-5 ms)6 ms (5 ms-6 ms)+9.5% (-8.7% to +20.0%)same, under the floor
step expect.toHaveRole5 ms (3 ms-5 ms)5 ms (5 ms-5 ms)+11.8% (-10.6% to +66.7%)same, under the floor
step expect.toBeFocused4 ms (4 ms-4 ms)4 ms (4 ms-5 ms)+11.8% (-13.4% to +29.1%)same, under the floor
step expect.toHaveAccessibleName4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (0.0% to +15.5%)same
step expect.not.toHaveRole4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-10.6% to +11.8%)same
step expect.toBeSelected4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-13.4% to +15.5%)same
step window.evaluate4 ms (4 ms-5 ms)5 ms (3 ms-5 ms)+9.5% (-50.0% to +25.0%)same, under the floor
step app.back4 ms (4 ms-5 ms)4 ms (4 ms-5 ms)0.0% (-18.4% to +11.8%)same
step expect.not.toBeEnabled4 ms (4 ms-4 ms)4 ms (4 ms-5 ms)0.0% (-22.5% to +25.0%)same
step expect.toHaveScreenshot1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (0.0% to 0.0%)same

Counters

Counterbasehead
attempts8788
events engine121123
events navigation110-129110-123
events poll225226
steps579590
tests passed8788
Tests outside the threshold: 0 of 84

With 84 tests compared at 95%, about 4.2 could land outside by chance.

- Video: none attached. The change is in what the engine reads and when it acts, which the tests above pin down; a recording would show the same window either way. - Local macOS runs, on both sides, started Electron with `-ApplePersistenceIgnoreState YES` from #2500, applied without committing it. Without it, an Electron the tests killed makes macOS ask to reopen its windows, and the next launch waits out its timeout. The Linux and Windows CI jobs do not need it.

Fresh-context review (Codex gpt-6.2-sol, read-only, on this branch's diff):

  • Fixed: a step cancelled before the trigger ran still ran it; the trigger now starts inside raceAbort (e050a7a42, with a test).
  • Fixed: a file the app saved outside the downloads folder took its name inside it (9727ec002, with a test).
  • Fixed: added the changeset.

Bot review (cubic, CodeRabbit):

  • Fixed: a step cancelled while bringToFront ran still left the new window active (59091afc8, with a test).
  • Fixed: the guide and the reference now say a path the app sets in its own will-download handler keeps its name, and the guide names the before-first-window exception (a6535dd9b).
  • Fixed: the testbed's slow-trigger download assertion accepts the busy-main-process message, since the read before the trigger shares the 500ms (0337141fa).
  • Dismissed: the file-on-disk check needs process.getBuiltinModule, so Electron 22 to 31 number only around the folder at launch and the engine's downloads, as main does for every version. The reference states that limit. A function sent to the main process has no require in scope since Electron 28.
  • Dismissed: downloads-folder paths are compared without case on Linux too. The only cost is a needless number when an app saves into a sibling folder that differs from Downloads only in case; names were already compared that way on main.
  • Dismissed: a trigger still running after its wait failed can keep acting. runTrigger leaves it to finish, as browser.waitForPopup() does on the web; cancelling test code is not something the engine can do.
  • Dismissed: the last read of the app's downloads can start at the deadline. It is the poll's last check, as core's assertion polling makes one, and a download that finished by then is a correct result.

Human review (michal-e2e, at e9ba4f066):

  • Fixed: the last read starts at the deadline with 1 ms to answer, and its timeout said the main process did not answer for a responsive app with no download. That read now ends with the ordinary timeout, the testbed's no-download case requires the ordinary message, and a separate testbed case holds the busy main process (539067fda).
  • Fixed: added a test that numbers a second download while the app's first is unfinished and its file is not on disk, so it fails without the name reservation (07c95621c). Electron writes the file as soon as the app names it, even paused, so the test waits for the file and removes it to take the disk check out (7b0cf60ad, after cubic and CodeRabbit noted the test could remove it before Chromium created it).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Window and download waits now include the time spent running their starting action in the timeout and respond promptly to cancellation.
    • A window opened after a wait times out is not selected; successful activation brings the window forward.
    • Download waits provide clearer errors when the main process is delayed or unavailable, including when it lacks the required inspector.
    • Downloads avoid overwriting existing files in the test’s Downloads folder, including files created by the app; duplicate names receive a number.
  • Documentation
    • Clarified download locations, filename behavior, version-specific limitations, and timeout and inspector-related errors.