fix(electron): return from app.back() once the window shows the previous page
What
app.back() on an Electron target returns once the window shows the previous page: the document that loaded, or, for an entry in the same document (a fragment or pushState, at the same URL too), the navigation to it. For an entry that only a frame inside the page made, it returns once that frame has navigated, including a frame of another site that runs in a renderer of its own. When there is no page to go back to, it fails with NOT_ACTIONABLE, and the reference now says so instead of ACTION_FAILED. When the step's timeout runs out after the back was sent (for example, the page before is still loading), it fails with ACTION_MAY_HAVE_COMMITTED.
Why
app.back() returned as soon as Page.navigateToHistoryEntry answered, which is when the navigation starts. The next step could then read the page the window was leaving. web()'s back waits for the page to load.
How it works
PageTargetcounts the window's same-document navigations (packages/electron/src/page.ts:111) and the navigations of frames inside the page (page.ts:121), and says whether its current document has loaded (page.ts:131).AppProcessupdates the first counter fromPage.navigatedWithinDocumenton the main frame, and the second fromPage.frameNavigatedandPage.navigatedWithinDocumenton any other frame (packages/electron/src/app-process.ts:385). A frame of another site runs in a renderer of its own, and its navigations never reach the page's session: each page auto-attaches those frames (Target.setAutoAttachfiltered toiframe), and theirs, and counts their navigations the same way (app-process.ts:753).back()records the window's document generation and both counters, sends the history navigation through the action's budget, and polls until one of these happens (packages/electron/src/surface.ts:634): a new document has loaded, the main frame made another same-document navigation, or a frame inside the page navigated while the window's committed URL equals the entry's URL. An entry that only an iframe made changes nothing in the page's own document and is at the URL the window already shows. A back to another URL reaches that URL only when its main frame commits, so a frame of the page being left that navigates on its own cannot end it.Page.getNavigationHistorycannot be used for this check: it names the pending entry as current as soon as the command answers. Running out of time fails withOPERATION_TIMEOUT(surface.ts:661). The back was already sent, so the budget reports it asACTION_MAY_HAVE_COMMITTED, and agent replay hands the uncertain action to the next executor, which checks the window before acting again (packages/core/src/agent/replay.ts:384). Empty history is found before anything is sent and staysNOT_ACTIONABLE.- Integration tests go back across documents, to a fragment, to a
pushStateentry at the same URL, to a fragment entry made inside an iframe, and to entries made inside a cross-origin iframe: a fragment, another document, and a fragment of a cross-origin iframe inside another one (packages/electron/tests/integration/launch.test.ts:490). Two of them pin the wait's checks. The page before holds its load on an image the server answers 1.5 s late, anddocument.readyStatemust becompleteright afterback(). A back to a page the server answers late must not end while the page it leaves holds an iframe that rewrites its fragment every 20 ms. The cross-origin test serves the page from127.0.0.1and the frame fromlocalhoston another port, and first checks that Chromium gave the frame aniframetarget of its own, so the frame is out of process. The nested test puts a127.0.0.1frame inside thelocalhostone; only thelocalhostframe's session is told about it. Another test goes back with a 1.5 s timeout to a page whose image the server answers after 5 s and expectsACTION_MAY_HAVE_COMMITTED, after a back with no page before has failed withNOT_ACTIONABLE.
A navigation the app starts while back() waits can end the wait early. So can a frame navigation during a back to a main-frame pushState entry at the URL the window already shows, or a second frame's navigation during a back to an entry that only one frame made. Telling these apart would mean tracking which frame the traversal navigates, which this PR leaves out. Comparing history entry ids would fail when an app's router pushes or replaces an entry on popstate, so this follows web(), which also waits for the navigation without checking the entry.
Verified
Ran it locally: yes, on macOS (Apple Silicon) with Electron 45.5.0.
- At
4f4771501, this branch passed the Electron package's unit and integration tests, the Electron testbed run through the built CLI (pnpm --filter @e2e-dev/electron-testbed test), andpnpm check. These were not run again after5736ee017; CI runspnpm check. - 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 | app.back() ends once the window shows the page before, loaded | fails | passes |
| integration | app.back() ends once the window has gone back to an entry of the same document at the same URL | fails | passes |
-
The iframe test,
app.back() ends once the window has gone back to an entry only a frame inside the page made, fails at4f4771501withNOT_ACTIONABLE: the window did not show its previous page in timeand passes from5736ee017on. -
Mutations at
817c5cedd, each run against theapp.back()tests: replacingreturn page.hasLoadedwithtruefails onlyends once the page before has fired its load event, not as its document commits. Replacingpage.url() === previous.urlwithtruefails onlyends at the page before, not at a navigation a frame of the page it leaves makes on its own. That second test also fails at5736ee017without any mutation:back()returned while the window still showed/b. -
At
817c5cedd, the Electron package's unit tests (115),launch.test.ts(42), typecheck, and oxlint pass. -
The two tests added for michal-e2e's review fail without their fixes (
8dcb11691,c946afbb1): the cross-origin one withNOT_ACTIONABLE: the window did not show its previous page in time, and the timeout one withNOT_ACTIONABLEwhere it expectsACTION_MAY_HAVE_COMMITTED. They pass ataf439ad77. -
Rebased onto
origin/main(ad564a886) for #2500 and #2504; onlylaunch.test.tsconflicted, and both sides were kept. On the rebased branch atc6eebcfdd: the Electron package's unit tests (166), all oflaunch.test.ts(70, including 9app.back()tests), typecheck, and oxlint pass. -
The two tests added for okwasniewski's second review fail without the code they cover: dropping the cross-origin frame session's
Page.frameNavigatedlistener failsends once the window has gone back to the document a cross-origin frame in its own renderer showed before, and dropping the recursivefollowOtherRenderers(page, frame, browser)call failsends once the window has gone back to an entry a cross-origin frame inside a cross-origin frame made. Each fails withOPERATION_TIMEOUT, and no otherapp.back()test fails. -
At
af439ad77(5b1461282after it changes only the reference's wording): the Electron package's unit tests (115),launch.test.ts(44),node-ops.test.ts(26), typecheck, and oxlint pass, and the built Electron testbed (e2e run --workers 2, 87 tests) 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, now on main) 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: measured at6087b8d9b, before the rebase onto main; since then the branch only gained two integration tests, so the table still describes this code. The 86 tests whose behavior matched run in the same time (suite -0.1%, 95% CI -0.4% to +0.1%). The one slower row isapp.backitself, 5 ms to 29 ms: main returned as soon as it sent the history command, and this PR waits until the window has gone back, which is the fix. One mouse test infixture.e2e.tshad two step sequences across head runs, so it is left out of the timings; this PR does not touch mouse input.
bench:ab output
bench-ab: 7c1b2b25 (merge base with origin/main) vs 6087b8d9 + 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.6.
Behavior
These tests are left out of the timing rows below.
- unstable on head (2 different step sequences) tests/fixture.e2e.ts › electron.keyboard and a window's page › moves, presses, and scrolls with the mouse where the test points it (electron)
Timings
Every row but the process wall clock covers the 86 tests whose behavior matched.
| Metric | base median (IQR) | head median (IQR) | change (95% CI) | verdict |
|---|---|---|---|---|
| wall (process) | 63.59 s (63.58 s-63.60 s) | 63.64 s (63.58 s-63.66 s) | +0.1% (-0.3% to +0.2%) | same |
| suite (sum of attempts) | 62.85 s (62.83 s-62.87 s) | 62.81 s (62.78 s-62.83 s) | -0.1% (-0.4% to +0.1%) | same |
| action (events) | 16.86 s (16.85 s-16.86 s) | 16.85 s (16.85 s-16.87 s) | -0.0% (-0.1% to +0.1%) | same, under the floor |
| step app.open | 24.31 s (24.30 s-24.32 s) | 24.30 s (24.30 s-24.31 s) | -0.0% (-0.1% to +0.1%) | same, under the floor |
| step locator.selectOption | 4.72 s (4.72 s-4.73 s) | 4.72 s (4.72 s-4.73 s) | -0.0% (-0.1% to +0.1%) | same, under the floor |
| step locator.tap | 4.37 s (4.36 s-4.38 s) | 4.37 s (4.36 s-4.38 s) | +0.0% (-0.4% to +0.5%) | same, under the floor |
| step locator.fill | 3.36 s (3.36 s-3.37 s) | 3.37 s (3.36 s-3.37 s) | +0.0% (-0.1% to +0.3%) | same, under the floor |
| step app.restart | 1.98 s (1.97 s-1.98 s) | 1.98 s (1.96 s-1.98 s) | +0.2% (-1.0% to +0.5%) | same, under the floor |
| step locator.dragTo | 1.43 s (1.43 s-1.43 s) | 1.43 s (1.42 s-1.43 s) | -0.1% (-0.4% to +0.1%) | 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.5%) | 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.2% to +0.2%) | same |
| step electron.window | 954 ms (954 ms-955 ms) | 955 ms (954 ms-955 ms) | 0.0% (0.0% to +0.1%) | same |
| step electron.waitForWindow | 925 ms (924 ms-932 ms) | 935 ms (930 ms-939 ms) | +0.5% (-1.2% to +1.8%) | same, under the floor |
| step locator.scrollIntoView | 925 ms (924 ms-925 ms) | 925 ms (925 ms-926 ms) | 0.0% (-0.2% to +0.3%) | same |
| step electron.launch | 912 ms (911 ms-912 ms) | 912 ms (910 ms-915 ms) | +0.1% (-0.2% to +0.4%) | same, under the floor |
| step locator.longPress | 904 ms (903 ms-904 ms) | 904 ms (903 ms-904 ms) | +0.1% (-0.2% to +0.2%) | same, under the floor |
| step locator.waitFor | 902 ms (902 ms-903 ms) | 903 ms (901 ms-903 ms) | 0.0% (-0.2% to +0.2%) | same |
| step expect.toHaveValue | 750 ms (747 ms-753 ms) | 745 ms (738 ms-750 ms) | -0.3% (-2.4% to +0.2%) | same, under the floor |
| step locator.setInputFiles | 568 ms (567 ms-569 ms) | 569 ms (568 ms-570 ms) | +0.1% (-0.7% to +0.4%) | same, under the floor |
| step electron.waitForDownload | 517 ms (516 ms-517 ms) | 516 ms (516 ms-516 ms) | -0.1% (-0.3% to +0.1%) | same, under the floor |
| step expect.not.toHaveValue | 513 ms (511 ms-516 ms) | 514 ms (510 ms-518 ms) | -0.0% (-1.1% to +1.6%) | same, under the floor |
| step electron.evaluate | 481 ms (480 ms-482 ms) | 478 ms (473 ms-488 ms) | +0.1% (-2.9% to +2.1%) | same, under the floor |
| step expect.toBeVisible | 434 ms (429 ms-436 ms) | 416 ms (408 ms-418 ms) | -4.4% (-6.1% to -2.8%) | unresolved |
| step app.clearState | 331 ms (329 ms-333 ms) | 330 ms (330 ms-332 ms) | -0.2% (-0.9% to +0.6%) | same, under the floor |
| step expect.toHaveText | 326 ms (324 ms-331 ms) | 322 ms (313 ms-329 ms) | -1.9% (-4.8% to +0.8%) | same, under the floor |
| step locator.check | 312 ms (311 ms-314 ms) | 313 ms (310 ms-317 ms) | +0.3% (-1.6% to +1.3%) | same, under the floor |
| step app.screenshot | 120 ms (116 ms-121 ms) | 119 ms (117 ms-121 ms) | 0.0% (-7.5% to +5.2%) | same |
| step expect.toHaveCount | 97 ms (96 ms-98 ms) | 98 ms (97 ms-98 ms) | +1.0% (-1.0% to +4.3%) | same, under the floor |
| step app.stopRecording | 81 ms (79 ms-84 ms) | 85 ms (80 ms-86 ms) | +0.0% (-1.3% to +6.3%) | same, under the floor |
| step locator.press | 49 ms (48 ms-50 ms) | 49 ms (48 ms-49 ms) | 0.0% (-3.0% to +3.2%) | same |
| step locator.clear | 47 ms (46 ms-47 ms) | 47 ms (46 ms-47 ms) | 0.0% (-3.2% to +2.2%) | same |
| step locator.focus | 35 ms (35 ms-36 ms) | 35 ms (34 ms-36 ms) | -1.4% (-8.2% to +4.3%) | same, under the floor |
| step electron.keyboard.press | 34 ms (32 ms-36 ms) | 33 ms (32 ms-33 ms) | -2.9% (-15.8% to +12.7%) | same, under the floor |
| step locator.swipe | 32 ms (25 ms-35 ms) | 36 ms (31 ms-38 ms) | +8.7% (-18.6% to +58.6%) | same, under the floor |
| step expect.toBeChecked | 32 ms (30 ms-32 ms) | 30 ms (29 ms-31 ms) | -0.1% (-15.2% to +9.0%) | same, under the floor |
| step expect.toBeHidden | 22 ms (21 ms-22 ms) | 22 ms (21 ms-22 ms) | +2.4% (-13.1% to +4.9%) | same, under the floor |
| step app.startRecording | 22 ms (21 ms-22 ms) | 21 ms (21 ms-22 ms) | -2.3% (-4.5% to +7.3%) | same, under the floor |
| step expect.toBeDisabled | 20 ms (19 ms-20 ms) | 19 ms (18 ms-20 ms) | -2.5% (-10.0% to +10.8%) | same, under the floor |
| step electron.keyboard.type | 19 ms (19 ms-19 ms) | 20 ms (19 ms-21 ms) | +2.6% (-2.7% to +10.5%) | same, under the floor |
| step expect.not.toBeChecked | 18 ms (16 ms-19 ms) | 19 ms (19 ms-19 ms) | +3.8% (-2.5% to +24.8%) | same, under the floor |
| step expect.toBeEditable | 17 ms (16 ms-18 ms) | 17 ms (17 ms-18 ms) | +3.1% (-2.8% to +9.0%) | same, under the floor |
| step locator.hover | 16 ms (16 ms-17 ms) | 16 ms (11 ms-17 ms) | -3.0% (-43.8% to +6.1%) | same, under the floor |
| step locator.uncheck | 15 ms (15 ms-15 ms) | 16 ms (16 ms-16 ms) | +6.7% (-5.7% to +6.7%) | same, under the floor |
| step expect.toHaveAttribute | 13 ms (12 ms-13 ms) | 13 ms (13 ms-13 ms) | +4.1% (-7.4% to +12.4%) | same, under the floor |
| step locator.dblclick | 12 ms (12 ms-12 ms) | 12 ms (12 ms-12 ms) | 0.0% (0.0% to +4.4%) | same |
| step expect.toBeInViewport | 11 ms (11 ms-11 ms) | 11 ms (11 ms-11 ms) | 0.0% (-4.7% to +4.9%) | same |
| step electron.keyboard.down | 11 ms (10 ms-11 ms) | 11 ms (10 ms-12 ms) | 0.0% (-4.7% to +14.4%) | same |
| step electron.keyboard.up | 10 ms (9 ms-10 ms) | 10 ms (9 ms-10 ms) | 0.0% (-14.2% to +11.1%) | same |
| step expect.not.toBeEmpty | 10 ms (9 ms-10 ms) | 10 ms (10 ms-10 ms) | +5.4% (0.0% to +16.5%) | same, under the floor |
| step expect.not.toBeInViewport | 9 ms (9 ms-9 ms) | 10 ms (9 ms-10 ms) | +5.4% (-10.1% to +11.1%) | same, under the floor |
| step expect.not.toBeEditable | 9 ms (8 ms-9 ms) | 9 ms (8 ms-9 ms) | 0.0% (-5.7% to +12.5%) | same |
| step expect.toBeEmpty | 9 ms (8 ms-10 ms) | 9 ms (8 ms-10 ms) | 0.0% (-5.7% to +11.8%) | same |
| step window.reload | 8 ms (7 ms-8 ms) | 7 ms (7 ms-8 ms) | 0.0% (-12.5% to +6.9%) | same |
| step expect.toContainText | 8 ms (7 ms-8 ms) | 8 ms (7 ms-8 ms) | 0.0% (-12.5% to +14.3%) | same |
| step expect.not.toHaveAttribute | 6 ms (6 ms-7 ms) | 6 ms (6 ms-6 ms) | 0.0% (-13.4% to 0.0%) | same |
| step expect.not.toBeHidden | 6 ms (5 ms-6 ms) | 6 ms (5 ms-6 ms) | 0.0% (-16.7% to +20.0%) | same |
| step screen.tree | 5 ms (5 ms-5 ms) | 5 ms (4 ms-5 ms) | 0.0% (-20.0% to 0.0%) | same |
| step expect.toHaveRole | 5 ms (5 ms-5 ms) | 4 ms (4 ms-5 ms) | -20.0% (-30.7% to +11.8%) | same, under the floor |
| step electron.keyboard.insertText | 5 ms (5 ms-5 ms) | 6 ms (5 ms-6 ms) | +9.5% (0.0% to +20.0%) | same, under the floor |
| step app.back | 5 ms (4 ms-6 ms) | 29 ms (29 ms-29 ms) | +537.2% (+383.3% to +637.4%) | slower |
| step expect.toBeFocused | 4 ms (4 ms-4 ms) | 4 ms (3 ms-4 ms) | -13.4% (-25.0% to +15.5%) | same, under the floor |
| step expect.toHaveAccessibleName | 4 ms (4 ms-4 ms) | 4 ms (4 ms-4 ms) | 0.0% (0.0% to 0.0%) | same |
| step expect.not.toHaveRole | 4 ms (4 ms-4 ms) | 4 ms (4 ms-5 ms) | 0.0% (0.0% to +25.0%) | same |
| step expect.toBeSelected | 4 ms (3 ms-4 ms) | 4 ms (4 ms-4 ms) | 0.0% (-13.4% to +33.3%) | same |
| step window.evaluate | 4 ms (3 ms-4 ms) | 4 ms (3 ms-5 ms) | +9.5% (-13.4% to +93.6%) | same, under the floor |
| step expect.not.toBeEnabled | 4 ms (4 ms-4 ms) | 4 ms (3 ms-4 ms) | -10.6% (-25.0% to 0.0%) | same, under the floor |
| step expect.toHaveScreenshot | 1 ms (1 ms-1 ms) | 2 ms (1 ms-2 ms) | +100.0% (0.0% to +100.0%) | same, under the floor |
| step window.close | 1 ms (1 ms-1 ms) | 1 ms (1 ms-1 ms) | 0.0% (-29.3% to +41.4%) | same |
Counters
| Counter | base | head |
|---|---|---|
| events navigation | 110-119 | 112-119 |
| steps | 579 | 579-580 |
Tests outside the threshold: 0 of 86
With 86 tests compared at 95%, about 4.3 could land outside by chance.
Fresh-context review (Codex gpt-6.2-sol, read-only, on this branch's diff):
- Fixed: added the changeset.
- Dismissed: a
pushStateor another navigation the app starts whileback()waits ends the wait. Requiring the history entry's id would fail an app whose router pushes or replaces an entry onpopstate, andweb()'s back waits for its navigation the same way.
Bot review:
- Fixed in
5736ee017(CodeRabbit): going back to an entry that only an iframe made waited out the budget and failed. - Fixed in
d247b834a: the5736ee017frame check readPage.getNavigationHistory, which names the pending entry as current, so an unrelated iframe navigation could end a back to another document early. The test that okwasniewski's review asked for found it. - Dismissed (cubic): a back-forward cache restore fires no load event. Electron turns that cache off for its WebContents delegate (
BackForwardCacheDisabledForDelegate), and it stays off withenable-features=BackForwardCache. Every back gets aNavigationframe event and a new load. - Fixed in
5b1461282(cubic): the reference saidback()returns once the window shows the entry; it now also says another navigation can end the wait early.
Human review (michal-e2e):
- Fixed in
8dcb11691: a back to an entry a cross-origin iframe made timed out, since that frame's navigations reach only its own target's session. - Fixed in
c946afbb1, docs and changeset inaf439ad77: a back that ran out of time after it was sent failed withNOT_ACTIONABLE, which tells agent replay nothing happened. It now fails withACTION_MAY_HAVE_COMMITTED.
🤖 Generated with Claude Code
Summary by CodeRabbit
- Bug Fixes
app.back()now waits until the previous page or same-page history entry is shown before completing, including entries created by iframe navigation.- Calling
app.back()when there is no previous history entry now returns aNOT_ACTIONABLEerror. - Back navigation completes only after the prior entry is confirmed as current, rather than returning before the window reaches it.
- If the operation times out after the window has been sent back, it reports that the action may have completed. Navigation started by the app or its frames can end the wait early.