fix(electron): return from app.back() once the window shows the previous page

Merged
KD
10h ago
+316-6

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

  1. PageTarget counts 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). AppProcess updates the first counter from Page.navigatedWithinDocument on the main frame, and the second from Page.frameNavigated and Page.navigatedWithinDocument on 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.setAutoAttach filtered to iframe), and theirs, and counts their navigations the same way (app-process.ts:753).
  2. 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.getNavigationHistory cannot be used for this check: it names the pending entry as current as soon as the command answers. Running out of time fails with OPERATION_TIMEOUT (surface.ts:661). The back was already sent, so the budget reports it as ACTION_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 stays NOT_ACTIONABLE.
  3. Integration tests go back across documents, to a fragment, to a pushState entry 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, and document.readyState must be complete right after back(). 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 from 127.0.0.1 and the frame from localhost on another port, and first checks that Chromium gave the frame an iframe target of its own, so the frame is out of process. The nested test puts a 127.0.0.1 frame inside the localhost one; only the localhost frame'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 expects ACTION_MAY_HAVE_COMMITTED, after a back with no page before has failed with NOT_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), and pnpm check. These were not run again after 5736ee017; CI runs pnpm check.
  • 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
integrationapp.back() ends once the window shows the page before, loadedfailspasses
integrationapp.back() ends once the window has gone back to an entry of the same document at the same URLfailspasses
  • The iframe test, app.back() ends once the window has gone back to an entry only a frame inside the page made, fails at 4f4771501 with NOT_ACTIONABLE: the window did not show its previous page in time and passes from 5736ee017 on.

  • Mutations at 817c5cedd, each run against the app.back() tests: replacing return page.hasLoaded with true fails only ends once the page before has fired its load event, not as its document commits. Replacing page.url() === previous.url with true fails only ends at the page before, not at a navigation a frame of the page it leaves makes on its own. That second test also fails at 5736ee017 without 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 with NOT_ACTIONABLE: the window did not show its previous page in time, and the timeout one with NOT_ACTIONABLE where it expects ACTION_MAY_HAVE_COMMITTED. They pass at af439ad77.

  • Rebased onto origin/main (ad564a886) for #2500 and #2504; only launch.test.ts conflicted, and both sides were kept. On the rebased branch at c6eebcfdd: the Electron package's unit tests (166), all of launch.test.ts (70, including 9 app.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.frameNavigated listener fails ends once the window has gone back to the document a cross-origin frame in its own renderer showed before, and dropping the recursive followOtherRenderers(page, frame, browser) call fails ends once the window has gone back to an entry a cross-origin frame inside a cross-origin frame made. Each fails with OPERATION_TIMEOUT, and no other app.back() test fails.

  • At af439ad77 (5b1461282 after 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 at 6087b8d9b, 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 is app.back itself, 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 in fixture.e2e.ts had 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.

Metricbase 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.open24.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.selectOption4.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.tap4.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.fill3.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.restart1.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.dragTo1.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.pressSequentially1.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.title1.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.window954 ms (954 ms-955 ms)955 ms (954 ms-955 ms)0.0% (0.0% to +0.1%)same
step electron.waitForWindow925 ms (924 ms-932 ms)935 ms (930 ms-939 ms)+0.5% (-1.2% to +1.8%)same, under the floor
step locator.scrollIntoView925 ms (924 ms-925 ms)925 ms (925 ms-926 ms)0.0% (-0.2% to +0.3%)same
step electron.launch912 ms (911 ms-912 ms)912 ms (910 ms-915 ms)+0.1% (-0.2% to +0.4%)same, under the floor
step locator.longPress904 ms (903 ms-904 ms)904 ms (903 ms-904 ms)+0.1% (-0.2% to +0.2%)same, under the floor
step locator.waitFor902 ms (902 ms-903 ms)903 ms (901 ms-903 ms)0.0% (-0.2% to +0.2%)same
step expect.toHaveValue750 ms (747 ms-753 ms)745 ms (738 ms-750 ms)-0.3% (-2.4% to +0.2%)same, under the floor
step locator.setInputFiles568 ms (567 ms-569 ms)569 ms (568 ms-570 ms)+0.1% (-0.7% to +0.4%)same, under the floor
step electron.waitForDownload517 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.toHaveValue513 ms (511 ms-516 ms)514 ms (510 ms-518 ms)-0.0% (-1.1% to +1.6%)same, under the floor
step electron.evaluate481 ms (480 ms-482 ms)478 ms (473 ms-488 ms)+0.1% (-2.9% to +2.1%)same, under the floor
step expect.toBeVisible434 ms (429 ms-436 ms)416 ms (408 ms-418 ms)-4.4% (-6.1% to -2.8%)unresolved
step app.clearState331 ms (329 ms-333 ms)330 ms (330 ms-332 ms)-0.2% (-0.9% to +0.6%)same, under the floor
step expect.toHaveText326 ms (324 ms-331 ms)322 ms (313 ms-329 ms)-1.9% (-4.8% to +0.8%)same, under the floor
step locator.check312 ms (311 ms-314 ms)313 ms (310 ms-317 ms)+0.3% (-1.6% to +1.3%)same, under the floor
step app.screenshot120 ms (116 ms-121 ms)119 ms (117 ms-121 ms)0.0% (-7.5% to +5.2%)same
step expect.toHaveCount97 ms (96 ms-98 ms)98 ms (97 ms-98 ms)+1.0% (-1.0% to +4.3%)same, under the floor
step app.stopRecording81 ms (79 ms-84 ms)85 ms (80 ms-86 ms)+0.0% (-1.3% to +6.3%)same, under the floor
step locator.press49 ms (48 ms-50 ms)49 ms (48 ms-49 ms)0.0% (-3.0% to +3.2%)same
step locator.clear47 ms (46 ms-47 ms)47 ms (46 ms-47 ms)0.0% (-3.2% to +2.2%)same
step locator.focus35 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.press34 ms (32 ms-36 ms)33 ms (32 ms-33 ms)-2.9% (-15.8% to +12.7%)same, under the floor
step locator.swipe32 ms (25 ms-35 ms)36 ms (31 ms-38 ms)+8.7% (-18.6% to +58.6%)same, under the floor
step expect.toBeChecked32 ms (30 ms-32 ms)30 ms (29 ms-31 ms)-0.1% (-15.2% to +9.0%)same, under the floor
step expect.toBeHidden22 ms (21 ms-22 ms)22 ms (21 ms-22 ms)+2.4% (-13.1% to +4.9%)same, under the floor
step app.startRecording22 ms (21 ms-22 ms)21 ms (21 ms-22 ms)-2.3% (-4.5% to +7.3%)same, under the floor
step expect.toBeDisabled20 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.type19 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.toBeChecked18 ms (16 ms-19 ms)19 ms (19 ms-19 ms)+3.8% (-2.5% to +24.8%)same, under the floor
step expect.toBeEditable17 ms (16 ms-18 ms)17 ms (17 ms-18 ms)+3.1% (-2.8% to +9.0%)same, under the floor
step locator.hover16 ms (16 ms-17 ms)16 ms (11 ms-17 ms)-3.0% (-43.8% to +6.1%)same, under the floor
step locator.uncheck15 ms (15 ms-15 ms)16 ms (16 ms-16 ms)+6.7% (-5.7% to +6.7%)same, under the floor
step expect.toHaveAttribute13 ms (12 ms-13 ms)13 ms (13 ms-13 ms)+4.1% (-7.4% to +12.4%)same, under the floor
step locator.dblclick12 ms (12 ms-12 ms)12 ms (12 ms-12 ms)0.0% (0.0% to +4.4%)same
step expect.toBeInViewport11 ms (11 ms-11 ms)11 ms (11 ms-11 ms)0.0% (-4.7% to +4.9%)same
step electron.keyboard.down11 ms (10 ms-11 ms)11 ms (10 ms-12 ms)0.0% (-4.7% to +14.4%)same
step electron.keyboard.up10 ms (9 ms-10 ms)10 ms (9 ms-10 ms)0.0% (-14.2% to +11.1%)same
step expect.not.toBeEmpty10 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.toBeInViewport9 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.toBeEditable9 ms (8 ms-9 ms)9 ms (8 ms-9 ms)0.0% (-5.7% to +12.5%)same
step expect.toBeEmpty9 ms (8 ms-10 ms)9 ms (8 ms-10 ms)0.0% (-5.7% to +11.8%)same
step window.reload8 ms (7 ms-8 ms)7 ms (7 ms-8 ms)0.0% (-12.5% to +6.9%)same
step expect.toContainText8 ms (7 ms-8 ms)8 ms (7 ms-8 ms)0.0% (-12.5% to +14.3%)same
step expect.not.toHaveAttribute6 ms (6 ms-7 ms)6 ms (6 ms-6 ms)0.0% (-13.4% to 0.0%)same
step expect.not.toBeHidden6 ms (5 ms-6 ms)6 ms (5 ms-6 ms)0.0% (-16.7% to +20.0%)same
step screen.tree5 ms (5 ms-5 ms)5 ms (4 ms-5 ms)0.0% (-20.0% to 0.0%)same
step expect.toHaveRole5 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.insertText5 ms (5 ms-5 ms)6 ms (5 ms-6 ms)+9.5% (0.0% to +20.0%)same, under the floor
step app.back5 ms (4 ms-6 ms)29 ms (29 ms-29 ms)+537.2% (+383.3% to +637.4%)slower
step expect.toBeFocused4 ms (4 ms-4 ms)4 ms (3 ms-4 ms)-13.4% (-25.0% to +15.5%)same, under the floor
step expect.toHaveAccessibleName4 ms (4 ms-4 ms)4 ms (4 ms-4 ms)0.0% (0.0% to 0.0%)same
step expect.not.toHaveRole4 ms (4 ms-4 ms)4 ms (4 ms-5 ms)0.0% (0.0% to +25.0%)same
step expect.toBeSelected4 ms (3 ms-4 ms)4 ms (4 ms-4 ms)0.0% (-13.4% to +33.3%)same
step window.evaluate4 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.toBeEnabled4 ms (4 ms-4 ms)4 ms (3 ms-4 ms)-10.6% (-25.0% to 0.0%)same, under the floor
step expect.toHaveScreenshot1 ms (1 ms-1 ms)2 ms (1 ms-2 ms)+100.0% (0.0% to +100.0%)same, under the floor
step window.close1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (-29.3% to +41.4%)same

Counters

Counterbasehead
events navigation110-119112-119
steps579579-580
Tests outside the threshold: 0 of 86

With 86 tests compared at 95%, about 4.3 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: added the changeset.
  • Dismissed: a pushState or another navigation the app starts while back() waits ends the wait. Requiring the history entry's id would fail an app whose router pushes or replaces an entry on popstate, and web()'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: the 5736ee017 frame check read Page.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 with enable-features=BackForwardCache. Every back gets a Navigation frame event and a new load.
  • Fixed in 5b1461282 (cubic): the reference said back() 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 in af439ad77: a back that ran out of time after it was sent failed with NOT_ACTIONABLE, which tells agent replay nothing happened. It now fails with ACTION_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 a NOT_ACTIONABLE error.
    • 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.