fix(electron): count the trigger in waitForWindow's and waitForDownload's timeout
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
CANCELLEDat once, runs no trigger it has not started, and shows no window the trigger opened. waitForDownloadends 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 withUNSUPPORTED_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
runTriggerruns the trigger insideraceAbort, underwithTimeoutagainst 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.waitForWindowtakes its deadline before the trigger and throwstimedOut()once the trigger used it up, then polls for a new window under the same deadline (surface.ts:508-527).activateWindowraises the window, checks the budget again, and only then makes it the windowscreenreads, so a step cancelled at any point before that leaves the old window active (surface.ts:456-466).waitForDownloadreads the app's downloads before and after the trigger withwithTimeoutandraceAbortagainst that deadline (surface.ts:594-630). A read that times out fails withOPERATION_TIMEOUT.pollConditionends 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, becomesno 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 asCANCELLEDwith the messageelectron.waitForDownload cancelled.placeDownloadsnumbers a name against the names it gave out, the folder at launch, the file on disk (throughprocess.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).- 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_FAILEDmessages.
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 intests/integration/launch.test.ts(-t download, 6 tests, run twice), typecheck, and oxlint pass. The testbed's download tests intests/parity.e2e.tspassed three runs in a row through the built CLI.tests/integration/launch.test.tsas a whole, the rest of the testbed'stests/parity.e2e.tsandtests/sign-in.e2e.ts, and the docs checks last ran ata6535dd9b. The package's other integration tests, the whole testbed (pnpm --filter @e2e-dev/electron-testbed test), andpnpm checklast ran ata418b54d5. The commits since then changeactivateWindow, 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 testnumbers a download whose name the app took for one still downloading, with no file of that name on diskfails when the app-chosen name is not reserved inplaceDownloads. The older collision test still passes without that reservation. - main against this branch: this branch's tests run against
origin/main'spackages/electron/src. These tests fail there and pass here:
| Suite | Test | main | this PR |
|---|---|---|---|
| integration | electron.waitForWindow shows no window for a wait cancelled while its trigger ran | fails | passes |
| integration | electron.waitForWindow shows no window for a wait cancelled while the window is brought to the front | fails | passes |
| integration | electron.waitForWindow ends as cancelled, then, when its step is cancelled while its trigger still runs | fails | passes |
| integration | electron.waitForWindow runs no trigger for a step cancelled before it starts | fails | passes |
| integration | electron.waitForDownload fails in its time, and runs no trigger, while the app's main process is too busy to answer | fails | passes |
| integration | electron.waitForDownload ends as cancelled, at once, when its step is cancelled while the busy main process is asked | fails | passes |
| integration | electron.waitForDownload runs no trigger for a step cancelled while the app was asked for its downloads | fails | passes |
| integration | downloads numbers a download whose name the app took itself, by a download or a file written after launch | fails | passes |
| testbed | 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 | fails | passes |
| testbed | tests/sign-in.e2e.ts > windows > drives a second window the app opens | fails | passes |
- 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.
| Metric | base 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.open | 23.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.selectOption | 4.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.tap | 4.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.fill | 3.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.restart | 1.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.dragTo | 1.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.pressSequentially | 1.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.title | 1.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.scrollIntoView | 925 ms (924 ms-926 ms) | 926 ms (925 ms-926 ms) | +0.1% (-0.2% to +0.2%) | same, under the floor |
| step electron.launch | 913 ms (911 ms-913 ms) | 913 ms (913 ms-914 ms) | +0.1% (-0.2% to +0.2%) | same, under the floor |
| step locator.longPress | 904 ms (904 ms-905 ms) | 904 ms (903 ms-904 ms) | -0.1% (-0.3% to +0.2%) | same, under the floor |
| step locator.waitFor | 902 ms (902 ms-902 ms) | 901 ms (901 ms-902 ms) | -0.1% (-0.2% to +0.1%) | same, under the floor |
| step expect.toHaveValue | 739 ms (735 ms-743 ms) | 743 ms (738 ms-746 ms) | +0.3% (-0.9% to +2.0%) | same, under the floor |
| step locator.setInputFiles | 571 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.toHaveValue | 516 ms (514 ms-518 ms) | 511 ms (510 ms-512 ms) | -0.9% (-1.5% to -0.2%) | same, under the floor |
| step electron.evaluate | 468 ms (462 ms-470 ms) | 468 ms (465 ms-470 ms) | -0.1% (-1.7% to +2.7%) | same, under the floor |
| step expect.toBeVisible | 433 ms (421 ms-436 ms) | 427 ms (424 ms-430 ms) | -1.5% (-3.1% to +3.0%) | same, under the floor |
| step app.clearState | 331 ms (329 ms-332 ms) | 333 ms (332 ms-336 ms) | +0.8% (+0.0% to +1.8%) | same, under the floor |
| step expect.toHaveText | 329 ms (324 ms-335 ms) | 324 ms (322 ms-326 ms) | -1.4% (-3.7% to +0.8%) | same, under the floor |
| step locator.check | 302 ms (301 ms-305 ms) | 303 ms (301 ms-303 ms) | -0.2% (-2.0% to +2.0%) | same, under the floor |
| step electron.waitForWindow | 260 ms (256 ms-263 ms) | 258 ms (255 ms-260 ms) | +0.4% (-3.2% to +2.0%) | same, under the floor |
| step app.screenshot | 120 ms (118 ms-126 ms) | 117 ms (116 ms-124 ms) | -1.3% (-10.6% to +8.7%) | same, under the floor |
| step expect.toHaveCount | 101 ms (99 ms-104 ms) | 99 ms (97 ms-100 ms) | -0.0% (-8.5% to +3.7%) | same, under the floor |
| step app.stopRecording | 84 ms (80 ms-84 ms) | 77 ms (77 ms-82 ms) | -7.0% (-8.9% to +3.8%) | same, under the floor |
| step electron.window | 51 ms (51 ms-51 ms) | 51 ms (51 ms-52 ms) | +1.0% (0.0% to +2.0%) | same, under the floor |
| step locator.press | 49 ms (48 ms-50 ms) | 50 ms (49 ms-50 ms) | +2.1% (-3.8% to +5.2%) | same, under the floor |
| step locator.clear | 48 ms (47 ms-50 ms) | 49 ms (48 ms-49 ms) | +2.1% (-1.0% to +2.2%) | same, under the floor |
| step locator.focus | 36 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.press | 34 ms (33 ms-36 ms) | 36 ms (35 ms-37 ms) | +4.5% (-0.0% to +10.8%) | same, under the floor |
| step locator.swipe | 34 ms (33 ms-37 ms) | 36 ms (34 ms-37 ms) | +5.9% (-7.9% to +26.0%) | same, under the floor |
| step expect.toBeChecked | 31 ms (29 ms-33 ms) | 29 ms (28 ms-30 ms) | -7.9% (-17.0% to +15.3%) | same, under the floor |
| step app.startRecording | 22 ms (21 ms-22 ms) | 22 ms (22 ms-22 ms) | 0.0% (-2.2% to +7.3%) | same |
| step expect.toBeHidden | 21 ms (21 ms-23 ms) | 22 ms (21 ms-23 ms) | 0.0% (-2.2% to +15.0%) | same |
| step expect.toBeDisabled | 20 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.type | 19 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.toBeChecked | 19 ms (17 ms-19 ms) | 18 ms (17 ms-20 ms) | +0.5% (-19.6% to +25.7%) | same, under the floor |
| step expect.toBeEditable | 18 ms (17 ms-18 ms) | 18 ms (18 ms-19 ms) | 0.0% (-2.8% to +11.8%) | same |
| step locator.hover | 16 ms (15 ms-17 ms) | 16 ms (15 ms-17 ms) | +6.5% (-28.0% to +13.9%) | same, under the floor |
| step locator.uncheck | 15 ms (15 ms-16 ms) | 16 ms (15 ms-16 ms) | 0.0% (0.0% to +9.5%) | same |
| step electron.mouse.down | 15 ms (14 ms-15 ms) | 15 ms (14 ms-15 ms) | -3.2% (-6.7% to +7.1%) | same, under the floor |
| step expect.toHaveAttribute | 13 ms (12 ms-13 ms) | 13 ms (12 ms-13 ms) | 0.0% (-7.7% to +18.2%) | same |
| step locator.dblclick | 12 ms (12 ms-12 ms) | 12 ms (12 ms-12 ms) | 0.0% (-7.4% to +4.4%) | same |
| step electron.mouse.up | 11 ms (11 ms-11 ms) | 11 ms (11 ms-11 ms) | 0.0% (0.0% to +4.9%) | same |
| step expect.not.toBeEmpty | 11 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.down | 11 ms (10 ms-11 ms) | 10 ms (10 ms-11 ms) | 0.0% (-9.1% to +4.9%) | same |
| step expect.toBeInViewport | 11 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.up | 10 ms (9 ms-10 ms) | 10 ms (10 ms-10 ms) | 0.0% (0.0% to +11.1%) | same |
| step expect.toBeEmpty | 10 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.toBeInViewport | 9 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.move | 9 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.toBeEditable | 8 ms (8 ms-8 ms) | 9 ms (8 ms-9 ms) | +12.5% (0.0% to +12.5%) | same, under the floor |
| step expect.toContainText | 8 ms (7 ms-8 ms) | 8 ms (7 ms-8 ms) | +6.9% (-12.5% to +15.5%) | same, under the floor |
| step window.reload | 7 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.toHaveAttribute | 6 ms (6 ms-6 ms) | 6 ms (6 ms-6 ms) | 0.0% (0.0% to 0.0%) | same |
| step expect.not.toBeHidden | 6 ms (5 ms-6 ms) | 5 ms (5 ms-6 ms) | -16.7% (-16.7% to +20.0%) | same, under the floor |
| step screen.tree | 5 ms (5 ms-5 ms) | 5 ms (5 ms-5 ms) | 0.0% (-10.6% to 0.0%) | same |
| step electron.mouse.wheel | 5 ms (5 ms-5 ms) | 5 ms (5 ms-6 ms) | 0.0% (-8.7% to +20.0%) | same |
| step electron.keyboard.insertText | 5 ms (5 ms-5 ms) | 6 ms (5 ms-6 ms) | +9.5% (-8.7% to +20.0%) | same, under the floor |
| step expect.toHaveRole | 5 ms (3 ms-5 ms) | 5 ms (5 ms-5 ms) | +11.8% (-10.6% to +66.7%) | same, under the floor |
| step expect.toBeFocused | 4 ms (4 ms-4 ms) | 4 ms (4 ms-5 ms) | +11.8% (-13.4% to +29.1%) | same, under the floor |
| step expect.toHaveAccessibleName | 4 ms (4 ms-4 ms) | 4 ms (4 ms-4 ms) | 0.0% (0.0% to +15.5%) | same |
| step expect.not.toHaveRole | 4 ms (4 ms-4 ms) | 4 ms (4 ms-4 ms) | 0.0% (-10.6% to +11.8%) | same |
| step expect.toBeSelected | 4 ms (4 ms-4 ms) | 4 ms (4 ms-4 ms) | 0.0% (-13.4% to +15.5%) | same |
| step window.evaluate | 4 ms (4 ms-5 ms) | 5 ms (3 ms-5 ms) | +9.5% (-50.0% to +25.0%) | same, under the floor |
| step app.back | 4 ms (4 ms-5 ms) | 4 ms (4 ms-5 ms) | 0.0% (-18.4% to +11.8%) | same |
| step expect.not.toBeEnabled | 4 ms (4 ms-4 ms) | 4 ms (4 ms-5 ms) | 0.0% (-22.5% to +25.0%) | same |
| step expect.toHaveScreenshot | 1 ms (1 ms-1 ms) | 1 ms (1 ms-1 ms) | 0.0% (0.0% to 0.0%) | same |
Counters
| Counter | base | head |
|---|---|---|
| attempts | 87 | 88 |
| events engine | 121 | 123 |
| events navigation | 110-129 | 110-123 |
| events poll | 225 | 226 |
| steps | 579 | 590 |
| tests passed | 87 | 88 |
Tests outside the threshold: 0 of 84
With 84 tests compared at 95%, about 4.2 could land outside by chance.
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
bringToFrontran 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-downloadhandler 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 norequirein 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
Downloadsonly in case; names were already compared that way on main. - Dismissed: a trigger still running after its wait failed can keep acting.
runTriggerleaves it to finish, asbrowser.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.