fix(desktop): read container text, poll stale expect reads, and follow an agent's new window (ENG-1533)

KD
3h ago
+963-84

What

  • textContent(), toHaveText, and toContainText on a desktop list, tree, group, or row read the text its rows and labels show and the names of its controls, leaving out what is hidden.
  • expect and waitFor no longer fail with node became stale when the screen changes under a read: the read is polled again, and a timeout reports the assertion's own failure with the engine's reason the last reads failed (a locked screen, an app that re-created its elements), keeping that error as its cause. This holds on every engine.
  • A desktop tap or secondaryTap on static text or an image goes to the row or button around it.
  • While a test has made a window active with window.activate(), an agent step that opens a focused window (Preferences) reads and acts in it for the rest of that step, replayed from the cache too. Each engine operation of an agent.act step carries a new agentStep id for this.
  • The agent's type tool says a slider or stepper takes its number there.
  • In the foreground, app.open() waits for the app's key window before it returns.
  • The desktop guide names a SwiftUI TabView tab's role, tab, and says an installed app keeps its unsaved documents between runs.

Why

okwasniewski's API pass on #2323 found each of these on a SwiftUI app:

  • a list's text read empty;
  • an assertion with a short timeout surfaced node became stale;
  • a tap on a row's text was refused;
  • the agent could not follow Preferences while the main window was pinned;
  • the agent gave up on a slider with AUTOMATION_UNSUPPORTED;
  • headed typing right after app.open() lost its first line.

How it works

  1. withContainerText (packages/desktop/src/nodes.ts:538) gives a located container the text of its descendants, one line each (containerText, :544). A hidden node adds nothing, a text or heading with no text of its own reads what is under it, and a field keeps its own text: its value is left out as on the web, and a hint listed under it is not its text. The agent's observation is unchanged.
  2. Core's locator reads and expect's polling turn a stale read at the deadline into a "stale" sample that carries the engine's error (staleCause, packages/e2e/src/expect/async.ts:49). Main's #2253 already marked a ref superseded between resolve and read as stale; this adds a resolve the engine found stale until its deadline, and the error on both. A stale sample is never evaluated, so .not, toBeHidden, toHaveCount(0), and waitFor({ state: 'detached' }) cannot pass on one, and none replaces an earlier good sample. On timeout, staleReason (packages/core/src/locator/engine.ts:955) gives the observed line its message (no read until the deadline: <reason>), and the error becomes the failure's cause. The reads that use only the node (scrollUntilVisible, a tap with a position, locator.screenshot(), and a screenshot assertion's capture) call readNode (packages/core/src/locator/engine.ts:468), which throws a stale read as before, so their timeout keeps the engine's reason as its cause. A sample is now never reported as stale, so #2253's observedOf is removed and its test expects the new line, with #2253's match count of 1 for a superseded ref.
  3. pressTarget (surface.ts:1789) sends a tap on text or an image to the nearest row or button, at most three levels up. It stops at a list, scroll area, field, or the window, and refuses when there is none. A cell passes a press on unless it takes both a click and a right click (nodes.ts:414), so a secondary tap on text in a clickable cell reaches the row's menu, and so does one on a cell that has no menu of its own.
  4. OperationContext.agentStep (packages/core/src/engine/contract.ts:66) names the agent step on each of its operations: agent.act, agent.assert on a custom executor, and an e2e mcp session, which all dispatch through dispatchAgentStep; stamped (packages/core/src/agent/step-accounting.ts:226) sets it live and in a replay, where origin is test, and the engine protocol carries it. A changeset bumps @e2e-dev/core, e2e, @e2e-dev/web, and @e2e-dev/mobile for the contract field.
  5. follow (surface.ts:\1) keeps, for one agentStep, the windows the step's first read listed, so a window the step opens is followed whenever it takes focus, and one that was there before never is. A new window that is the app's key window, not a system dialog, is the one the agent reads while it is open. A test's read, or another step, starts over from the active window, which #2403's activeChoice gives. app.back()'s key-window check (#2356) goes by the same window. A point taken in a window the step followed carries the step (pointTaken, surface.ts:646), so the test's own coordinate tap after it measures from the active window.
  6. The type tool's description (packages/core/src/agent/primitives.ts:446) names sliders and steppers. A patch changeset covers @e2e-dev/core and e2e for this and step 2.
  7. keyWindow (surface.ts:528) polls the window list for up to 2 s after a foreground launch until a window of the app, not a dialog, is focused. A cancel or a quit during the wait fails the launch. It runs after main's windowReadable (launch-wait.ts, #2368), within that wait's deadline.

Following a new window was chosen over a window-switching tool for the agent: the agent already acts on "what my click opened", and a test that activates a window keeps it for its own steps.

Issue

Review items from okwasniewski's API pass on #2323.

Verified

Ran it locally: yes (macOS 27.2.1, background mode). Rebased onto main 7c1b2b258. Main's #2367 added its own shownText to nodes.ts, so this PR's container-text helper is containerText. Earlier rebase onto aaf923188: the following starts from #2403's active window, and the stale-read change was merged with #2253's.

  • Live on TaskBoard:
    • the task list reads "Buy milk Medium Delete Buy milk Call the bank …";
    • tapping a row's priority text selects the row, and a secondary tap on it opens the row's menu;
    • status text with no row around it is refused.
  • Live with a scripted executor (no model key here), before the rebase: an agent step with the main window pinned taps Preferences and sets its slider. With following turned off, the same run fails the way the review reported.
  • expect-stale.test.ts: 4 of its 5 original tests fail on the code before this layer. The new ones, a locked screen's reason and cause and toBeInViewport keeping its last good sample, fail without the cause handling.
  • The review fixes, each with a test that fails on the code before it: a replayed step follows the window, two agent steps in a row start from the chosen window, a list read from the whole tree leaves out its fold line, a field keeps its text over a hint, and a secondary tap on text in a clickable cell opens the row's menu.
  • From michal-e2e's review at 07c29a69a, each with a test that fails on the code before it: a test's tapAt and hoverAt right after an agent step that followed Preferences, and after a test key press, go to the active window, while a tap inside the step still goes to Preferences (surface.test.ts); scrollUntilVisible on reads that stay stale keeps the engine's reason as its cause and swipes nothing (scroll-until-visible.test.ts).
  • From CodeRabbit, each with a test that fails on the code before it: Preferences listed once without focus and then focused is followed; a located cell with an untitled heading reads the text under it; a secondary tap on a cell with no menu opens its row's. CodeRabbit's readNode finding is answered on its thread: the throw comes only after the resolve retried until the read's deadline, as on the merge base. Its zero-width-text finding is the open maintainer question below.
  • At this layer (cc704f9f7), the desktop suite passes at the top of the stack (340). Earlier at this layer (ef6154741), after the rebase onto 7c1b2b258: pnpm run build, pnpm check, the e2e unit suite (5141 passed, 2 skipped), core (137), the desktop suite with the addon (332 passed), and the expect integration tests #2253 changed (33 passed). The desktop testbed at the top of the stack: 31 of 31, and the SwiftUI suite 18 of 18.
  • Earlier, at 07c29a69a, after okwasniewski's nits (an unreachable branch in follow dropped, keyWindow's doc corrected): the desktop suite with the addon (327 passed) and pnpm check at the top of the stack. Before them, after the rebase: pnpm run build, pnpm check, the e2e unit suite (5134 passed, 2 skipped), the desktop suite with the addon (327 passed), and the expect integration tests #2253 changed (scripted-polling, scripted-matchers, screenshot-assertions: 33 passed). The desktop testbed at the top of the stack: 30 of 30, and the SwiftUI suite 18 of 18.
  • Typing right after app.open(): 18 runs in the background, no lost key. In the background keyWindow returns at once, so those runs say nothing about the new wait, which has unit tests only, since a headed run moves the pointer of the person at this Mac. The late cancel and quit in it are checked by reading only.
  • The radio line the review asked for would be wrong on this driver: getByRole('tab', 'Advanced') works and nothing matches radio, so the docs say tab.
  • expect's polling change runs on every engine, and every operation of an agent step now carries agentStep, so pnpm bench:ab --suite testbed --threshold 3 -- tests/forms.e2e.ts tests/todos.e2e.ts tests/locator-list.e2e.ts tests/selectors-get-by.e2e.ts measured them on the web testbed, against main 7c1b2b258, with the head at the top of the stack (55fae86f3); the layer above this one changes no code the web engine runs. Same steps, same outcomes, same counters, no timing change:

bench-ab: 7c1b2b25 (merge base with origin/main) vs 55fae86f

apps/testbed: e2e run --workers 1 tests/forms.e2e.ts tests/todos.e2e.ts tests/locator-list.e2e.ts tests/selectors-get-by.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 26.13.1, load 4.6.

Behavior

Every test ran the same steps with the same outcome on every run of both builds.

Timings

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)3.78 s (3.77 s-3.81 s)3.76 s (3.74 s-3.78 s)-0.3% (-1.7% to +0.4%)same
suite (sum of attempts)3.11 s (3.09 s-3.12 s)3.10 s (3.08 s-3.12 s)+0.2% (-1.4% to +0.7%)same, under the floor
action (events)1.01 s (1.00 s-1.02 s)1.00 s (1.00 s-1.00 s)-0.9% (-2.6% to +0.4%)same, under the floor
step browser.goto622 ms (615 ms-632 ms)625 ms (621 ms-634 ms)+0.4% (-1.8% to +3.4%)same, under the floor
step app.open506 ms (502 ms-507 ms)512 ms (507 ms-513 ms)+0.9% (-0.5% to +2.3%)same, under the floor
step locator.fill357 ms (356 ms-363 ms)361 ms (357 ms-365 ms)+0.7% (-2.9% to +4.1%)same, under the floor
step locator.tap332 ms (329 ms-333 ms)321 ms (318 ms-325 ms)-3.2% (-5.4% to -0.2%)unresolved
step locator.check173 ms (171 ms-177 ms)179 ms (175 ms-185 ms)+2.3% (-4.5% to +7.5%)same, under the floor
step expect.toHaveCount120 ms (118 ms-121 ms)121 ms (121 ms-123 ms)+0.4% (-0.4% to +3.8%)same, under the floor
step locator.press69 ms (68 ms-70 ms)71 ms (70 ms-71 ms)+1.4% (-0.7% to +10.7%)same, under the floor
step expect.toHaveText61 ms (60 ms-62 ms)60 ms (58 ms-62 ms)-1.7% (-7.2% to +8.4%)same, under the floor
step expect.toHaveAttribute59 ms (58 ms-60 ms)61 ms (60 ms-63 ms)+4.3% (+0.8% to +8.3%)same, under the floor
step app.restart43 ms (42 ms-43 ms)43 ms (42 ms-44 ms)0.0% (-4.7% to +4.7%)same
step app.clearState43 ms (42 ms-45 ms)43 ms (43 ms-44 ms)0.0% (-4.4% to +3.6%)same
step locator.pressSequentially41 ms (40 ms-41 ms)40 ms (40 ms-41 ms)-1.2% (-2.4% to +1.2%)same, under the floor
step locator.uncheck36 ms (33 ms-46 ms)32 ms (32 ms-32 ms)-12.4% (-33.3% to 0.0%)same, under the floor
step expect.toBeVisible17 ms (16 ms-17 ms)17 ms (16 ms-17 ms)-3.0% (-8.7% to +3.1%)same, under the floor
step locator.selectOption16 ms (16 ms-17 ms)16 ms (16 ms-16 ms)-3.0% (-19.2% to +6.5%)same, under the floor
step locator.clear16 ms (16 ms-17 ms)16 ms (15 ms-16 ms)0.0% (-11.4% to +6.1%)same
step expect.toHaveValue7 ms (6 ms-8 ms)6 ms (5 ms-7 ms)-13.4% (-29.3% to 0.0%)same, under the floor
step expect.toContainText6 ms (5 ms-6 ms)5 ms (5 ms-6 ms)-8.7% (-18.4% to +22.5%)same, under the floor
step browser.evaluate4 ms (4 ms-5 ms)4 ms (4 ms-4 ms)0.0% (-20.0% to +11.8%)same
step expect.toBeChecked3 ms (3 ms-4 ms)3 ms (2 ms-3 ms)-25.0% (-33.3% to 0.0%)same, under the floor
step expect.not.toBeChecked2 ms (1 ms-2 ms)2 ms (2 ms-2 ms)0.0% (0.0% to +100.0%)same
step expect.toBeSelected1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (-50.0% to 0.0%)same
step expect.toBeHidden1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (-50.0% to 0.0%)same
step expect.not.toBeAttached1 ms (1 ms-1 ms)1 ms (0 ms-1 ms)0.0% (0.0% to 0.0%)same

Counters

Every counter matched on every run of both builds.

Tests outside the threshold: 0 of 640

With 640 tests compared at 95%, about 32.0 could land outside by chance.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. Patches for e2e and @e2e-dev/core (the expect polling and the type tool). The desktop changes need none of their own: the pending .changeset/desktop-native-driver.md bumps @e2e-dev/desktop for the release that first ships this driver.
  • 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 and docs/desktop.mdx, and docs/reference/engine.mdx for agentStep.
  • 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. .changeset/engine-agent-step.md, for OperationContext.agentStep.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Desktop agent steps can follow newly opened, focused windows for the duration of the step; test actions continue to use the active window.
    • Taps on text or images can target a nearby containing row or button. Desktop locator results include visible text from containers.
    • Foreground app launches wait briefly for a key window to become available.
    • Slider and spin-button guidance clarifies how to enter and adjust numeric values.
    • Operations within an agent step share an identifier, supporting step-scoped engine state.
  • Bug Fixes

    • Polling assertions retry reads interrupted by screen changes and report the last readable result and failure reason on timeout. Negated assertions and detached waits still require a successful read.