feat(desktop): fold out-of-view rows, and add app.back, clearState, sessions, and video (ENG-1533)

KD
3h ago
+4481-1074

What

  • A long list reads as fast as a short one: rows a scroll area holds out of view become one hidden line (182 rows out of view) that no locator finds. scrollUntilVisible(locator) brings a row in, and selectOption(label) still reads every row.
  • A SwiftUI toolbar Menu's items match once, so getByRole('menuitem', 'By Priority').tap() needs no .first().
  • app.back() presses the app's Back menu item (titled Back or Go Back, else the one whose shortcut is Command-[) in the active window, and fails when the app has neither.
  • app.clearState() and test.setup sessions work for an app.appPath target.
  • video: 'on', --video on, and app.startRecording() record the app's window. Without Screen Recording permission video fails as it starts, a file whose recording broke off is dropped, and a recording that got no file fails at the stop with the reason.
  • selectOption names a table row by a numeric cell as every locator does.

Why

Follow-ups agreed on #2322 and #2323:

  • A 200-row SwiftUI List was 1,034 nodes and 1.1 s per read. scrollUntilVisible to its end took 24-31 s against the 30 s default, and one agent.act there cost 142k tokens.
  • app.back(), app.clearState(), sessions, and video were left out with reasons: a key that does nothing reports success, and resetting state reaches the user's own copy of an app. A screen-rectangle recording does not follow the window and records what covers it.

This layer answers each reason.

How it works

  1. Every request the driver makes of an element goes through a new Accessible trait (crates/e2e-desktop-napi/src/ax.rs:315), which Element implements. A fake tree for tests (src/fake.rs) implements it too.
    • 24 new tests cover the walk's hole handling (a stalled read, a failed AXChildren, the depth limit, a window that closed mid-read), title-bar chrome, the scroll rule, and row refs.
  2. A row of a list, table, or outline that lies wholly outside its scroll area costs one frame read. A run of such rows becomes one folded node (snapshot.rs:98). A row that holds focus or is selected is never folded (snapshot.rs:683), so getByRole('row', { selected: true }) finds the row selectOption chose.
    • nodes.ts:327 projects a fold as a hidden, role-less line. locate resolves on the tree without fold lines (locatable, nodes.ts:491), so no text query finds one; observe and screen.tree() keep them. A folded row matches no locator, so an absence check on a list's rows covers only the rows read; the guide says so.
    • An open menu or popover under a node a scroll hides is not hidden with it (FLOATING_ROLES, nodes.ts:436): the driver reads it as on screen, since it floats above the window.
    • own_label (actions/select.rs) reads a text's value through the snapshot's shown_value, so selectOption('Plan 2') finds a row whose second cell holds the number 2, and a heading's number (its level) is never part of a row's label.
    • A walk to the focused element that the app stalls on leaves the read incomplete (ancestors in snapshot.rs), so a fold never hides a focused field in a read that says it is complete.
    • seen (snapshot.rs:206) skips an element already read under another parent.
  3. app.back(): pressBack (actions/menu.rs:87) reads the menu bar for an item titled Back or Go Back, else one whose shortcut is Command-[ alone, and presses it with AXPress. It never sends a key.
    • The menu-bar walk is back_candidates (menu.rs:173), generic over Accessible so a fake bar drives it. An item counts as enabled only when its menu and every submenu item above it are.
    • choose (menu.rs:67) takes the best kind of match nearest the menu bar, first in menu order. When that item is disabled it refuses, and never presses a Back in the Window menu or under Open Recent instead.
    • AppKit updates a menu item's enabled state about a second late, so it watches the item for 1.2 s first. A budget too short for that watch refuses with TIMEOUT before the press (watch_window, menu.rs:149).
    • The watch reads the item and every menu and submenu item above it again (enabled_now, menu.rs:130), so a Back under a disabled Go menu or Open Recent is never pressed. press_back (menu.rs:104) holds the whole decision, generic over the tree, so a fake menu bar drives it from the walk to the press.
    • The tests of snapshot.rs and actions/mod.rs live in snapshot/tests.rs and actions/tests.rs, which keeps both files under 1000 lines.
    • The menu bar acts on the key window. With another window made active, keyWindowForMenu (surface.ts:1463) refuses with NOT_ACTIONABLE; with physicalInput it brings the active window forward first.
    • No item is UNSUPPORTED_CAPABILITY; a disabled one is NOT_ACTIONABLE.
  4. app.clearState() and sessions (packages/desktop/src/state.ts). The refusals all come before anything is changed:
    • a bundleId target is refused;
    • requireSole (:142) refuses while LaunchServices (bundleCopies) or Spotlight knows another copy of the bundle id, or a process the engine did not launch runs one. A Spotlight lookup that timed out fails the check (:81); Spotlight turned off finds nothing and leaves LaunchServices' answer;
    • a bundle id must be two or more dot-separated segments of letters, digits, hyphens, and underscores, not starting with a hyphen (:45). So .. never names a directory, and -g, -globalDomain, and NSGlobalDomain never reach defaults delete.
    • Reset (:171) quits the app, then clears its defaults domain, its Saved Application State/<id>.savedState, and Containers/<id>.
    • A session saves the defaults domain. Restore resets first and imports the defaults, since defaults import merges.
    • The state commands run within what is left of the operation's budget (withinBudget in surface.ts), though each has a longer limit of its own; a budget that runs out fails with OPERATION_TIMEOUT, no directory removal starts after it, and no command starts with no time left.
  5. Video (crates/e2e-desktop-napi/src/video.rs, packages/desktop/src/video.ts:37): ScreenCaptureKit records only the target window and writes H.264 to .mp4 through SCRecordingOutput.
    • The window is chosen by id, so covering windows are not recorded and the recording follows the window when it moves.
    • Each window screen reads is its own file, and a stop goes out even when the operation was cancelled. window.activate() moves the recording at once (surface.ts:517), not at the next read.
    • finish (video.ts:196) drops a file whose stop failed, since a recording that broke off has no index and no player opens it, and an empty one. Either loss, like a failed start, is the reason end throws when the video got no file.
    • A window whose start failed is not tried again for the rest of the video (follow in video.ts), unless the read's budget or a cancel cut the start off. While a start or stop is queued, follow leaves the comparison with the recorded window to switchTo, in turn.
    • A failed start removes the file it left, a segment's startedAt is taken before the start goes, abandon removes the files of a recording the attempt left running, and startVideo fails when its window lookup was cancelled or ran out of time.
    • It needs macOS 15 and Screen Recording, both checked in begin (video.ts:65): earlier versions fail with UNSUPPORTED_CAPABILITY, and no permission with ENGINE_FAILURE. The first window's start comes later, in follow, which never fails a read; its first failure is kept, and end throws it when the video got no file (video.ts:118).
    • stop counts the 500 ms minimum length from the first frame and gives a first frame that arrives during the stop the whole deadline. A permission refusal is told by SCStreamErrorDomain and UserDeclined (declined, video.rs:153), not by its localized text.
  6. CI's macOS native job runs the whole desktop unit suite with the addon it built, so the macOS-only state tests that guard what clearState removes run in CI (spec.yml).
  7. The new native calls (pressBack, bundleCopies, startVideo, stopVideo) are typed in packages/desktop/src/native.ts, which main's #2458 writes from the crate. docs/reference/desktop.mdx gets Back, Long lists, State, and Video sections, with one row each in docs/desktop.mdx's table. packages/desktop/README.md, the platform packages' THIRD_PARTY_LICENSES.md (for objc2-screen-capture-kit), and a changeset follow.

Issue

Follow-ups agreed on #2322 and #2323.

Verified

Ran it locally: yes (macOS 27.2.1, arm64, background mode). Rebased onto main 7c1b2b258. Main's #2367 added shown_value beside the tree walk this PR makes generic, so it takes the element type too. #2448 moved the desktop guide's details into docs/reference/desktop.mdx, so this PR's docs moved there. #2458 generates the driver's types from the crate, so this PR's calls are declared in Rust, with readonly return types as main's are.

Long-list reads, TaskBoard's 200-row SwiftUI List, before (base of this layer) and after:

beforeafter
driver snapshot1.9-2.9 s, 1039 nodes55-87 ms, 130 nodes
screen.tree()2225 ms, 1034 lines, 47.8k chars111 ms, 125 lines, 4.6k chars
scrollUntilVisible to row 20031.3 s1.4 s
  • The toolbar Menu item tap passes without .first() (it failed with LOCATOR_AMBIGUOUS before), checked live after confirming with CFEqual that both listings are one element.
  • app.back() on a scratch app with a Back item: two backs in a row work, a disabled Back is refused, an app without one is refused, and the app stayed in the background.
  • clearState and sessions on a scratch app with its own bundle id: clearState, state kept across a restart, a setup save, and two restores that replace rather than merge. A second copy registered with LaunchServices was refused with the domain unchanged, and a bundleId target was refused. Not run live: a sandboxed build's container removal.
  • Video, checked with ffprobe and stills:
    • the file follows a window move;
    • a full-screen window covering it is not recorded;
    • a sheet is recorded with its window;
    • a password field shows bullets;
    • a restart inside startRecording gives two files.
    • Not checked: resize and context menus.
  • Unit tests: the trait-driven walk tests, a row partly in view that is read rather than folded, the menu-bar walk on a fake bar (a title beats Command-[ on another item, a stalled menu fails), state.test.ts (a .. bundle id never reaches rm, and an empty nested dictionary survives a restore), video.test.ts (a stop goes out on a cancelled operation, no permission is refused before any start, a failed deferred start fails the stop), and nodes.test.ts (a loose getByText('View') beside a fold matches only the button; rows and 182 match nothing). The desktop unit suite passes with the native addon required.
  • Fixes from michal-e2e's probe comment, each with a test that fails on the code before it: the numeric row label (names_a_row_by_a_numeric_cell_as_the_snapshot_does), the bundle id rule (state.test.ts), the selected row out of view (folds_no_row_that_holds_the_focus…), the open menu under a scrolled-out button (nodes.test.ts), Back in a non-key active window (back.test.ts), the menu walk (refuses_a_disabled_back_rather_than_press_one_in_another_menu, reads_an_item_under_a_disabled_menu_or_submenu_as_disabled), and video (video.test.ts: a failed stop, an empty file, activate() with no read, a failed window beside a recorded one). The probe's folded-row focus failures went through holdsFocus, which main's #2374 removed: the driver now checks focus by the element, not by its box.
  • DesktopSurface.locate with a fold and a loose text query (surface.test.ts): putting locate back on the whole screen fails it.
  • From okwasniewski's re-review: never_presses_a_back_its_menu_keeps_disabled drives press_back on a fake bar with Go disabled and its Back enabled, and with Back under a disabled Open Recent. It fails when the watch reads the item alone. Two more tests cover a Back that AppKit enables, menu included, during the watch (pressed) and one it disables (refused). A live app.back() on the testbed app, which has no Back, is refused with nothing pressed.
  • This round's fixes, from CodeRabbit and cubic, each with a test that fails on the code before it, except the queued-switch check: a focus walk that stalls leaves the read incomplete; state commands stop at a 200 ms budget with OPERATION_TIMEOUT; a failed start's file, and an abandoned recording's files, are removed; a window whose start a budget cut off is recorded on the next read; a start cancelled while it looked for the window fails with CANCELLED; an untitled heading leaves its level out of a row's label; the fake table's selection replaces the one before; no state directory removal starts once the budget ran out; a copy lookup LaunchServices did not answer fails the check. A test of two overlapping activate() calls passed on the old code too, so the queued-switch change has none.
  • At this layer (01d581dc4), with Rust 1.100.0: cargo fmt --check, clippy, cargo test (112 passed), and native.ts regenerated with no change; the desktop suite with the addon (321 passed). pnpm run build and pnpm check at the top of the stack.
  • The desktop testbed at the top of the stack (#2358), after this round: 31 of 31, and the SwiftUI suite 18 of 18, with --video on too. Two rounds before: 18 of 18 with --video on, all 21 .mp4 files opening in ffprobe. At this layer before the round: 25 of 25, and with --video on every test wrote its file. With video on, the Controls window tests see an extra "Window": the window macOS draws in a recorded window's title bar, which #2358 leaves out.
  • Not run: a refused Screen Recording permission live (this Mac has it granted; the unit test covers the refusal), and a Spotlight timeout, which is checked by reading only, as is the EPIPE listener on defaults import.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. .changeset/desktop-back-state-video.md, a patch for @e2e-dev/desktop naming app.back(), clearState, sessions, video, and folded rows.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it. docs/reference/desktop.mdx, docs/desktop.mdx, and packages/desktop/README.md.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added app.back() to activate an enabled Back menu command without sending keyboard input.
    • Added app-state capture, restore, and reset for eligible apps launched from a path. These actions are unavailable for installed apps and require the app to be the only running copy.
    • Added window video recording on macOS 15 and later, with a separate recording file for each window.
  • Improvements
    • Large off-screen list sections appear as row-count markers. Markers aren’t searchable or actionable; visible, selected, and focused rows remain readable.
    • Improved display of numeric values in selectable rows. Open menus remain readable when their parent controls are off-screen.
  • Documentation
    • Updated desktop guidance for Back, app state, video, Screen Recording permission, and off-screen list behavior.