feat(core): scroll a container to an edge with scrollTo (ENG-1226)

Merged
KR
3d ago
+1751-39

What

screen.scrollTo(edge) and locator.scrollTo(edge) scroll a container to its top, bottom, left, or right edge, as far as the content goes, on the web engine and on iOS and Android devices.

await screen.scrollTo('bottom');                          // the page, or an app shell's main pane
await screen.getByTestId('feed').scrollTo('top');         // a list, or the node's nearest scrolling ancestor
await screen.getByTestId('feed').scrollTo('bottom', { timeout: 60_000 });

It resolves at once when the container is already at the edge or nothing scrolls. When the timeout runs out it fails with ACTION_FAILED: scrollTo('bottom') did not reach the edge within <timeout>ms (<engine's reason>). The desktop engine does not declare the action, so it fails with UNSUPPORTED_CAPABILITY.

Why

Cypress cy.scrollTo('bottom'), Detox scrollTo('bottom'), and Appium UiScrollable.scrollToEnd() had no deterministic equivalent. The migration guides told users to loop swipe until allTextContents() stopped changing, or to call window.scrollTo through browser.evaluate. This adds the edge scroll as one action.

Left out on purpose:

  • Positions such as cy.scrollTo(0, 500): a device can neither read nor set a scroll offset. On the web it is one browser.evaluate line, which the Cypress guide shows.
  • Corners: they take two calls.
  • A scroll by an offset: that is swipe distance (#2178).

How it works

flowchart LR
  A["screen.scrollTo / locator.scrollTo"] --> B["LocatorEngine.scrollTo"]
  B -- screen --> C["TargetSession.rootRef, then perform on the root ref"]
  B -- locator --> D["performUntil: perform on the node"]
  C --> E{engine}
  D --> E
  E -- web --> F["jump to ±scrollHeight, read after a frame, again while the end moves"]
  E -- mobile --> G["drag in the region, read, until a drag changes nothing"]
  1. Contract. A new LocatorAction kind, { kind: 'scrollTo', edge: ScrollEdge } (packages/core/src/engine/contract.ts:375). It joins KNOWN_LOCATOR_ACTION_KINDS, not the SPI v1 list (:403), the way blur and multiTap did. ENGINE_SPI_VERSION stays 1, because a bump would rekey every committed recording. scrollTo is added to the engine manifest schema's action enum, and both fixtures list it: the invalid one has a made-up kind beside it. Core 0.25.0 refuses an engine that declares an unknown kind, so the web and mobile engines now peer on @e2e-dev/core and e2e >=0.25.1 <1, as #1919 did for blur.
  2. API. ScreenImpl.scrollTo (packages/core/src/locator/screen.ts:324) checks the edge (:797), takes only timeout, and refuses a frame's screen: scrolling the root from inside a frame would scroll the screen around it. LocatorImpl.scrollTo (:628) labels the step <locator> → <edge>. ScrollEdge is exported beside ScrollDirection.
  3. Dispatch. LocatorEngine.scrollTo (packages/core/src/locator/engine.ts:575) runs both forms within one deadline.
    • Screen form. It takes the root ref from TargetSession.rootRef, which observes first when no observation named it yet. It refuses to send once the deadline has passed, then performs scrollTo on that ref. The perform gets only what is left of the deadline after the observe.
    • Locator form. It goes through performUntil, which re-resolves a stale node.
    • Timeouts. A failure of the scroll itself (an engine timeout, or input that may have landed) becomes did not reach the edge, with the engine's own message kept in parentheses, so a list still moving reads differently from a stuck device read. It names the step's timeout, not the time the engine took. The engines stop a little before the deadline, so this applies whether or not the deadline has passed. A locate or first observe that ran out, a stale retry's included, stays what it was: performUntil takes a performFailure mapper that sees only the perform's own failure.
    • swipe. It had the same observe-then-perform pattern in session.ts, so when it has to observe first it now gets only the budget the observe left (leftSince), and sends nothing when the observe spent all of it. A root the session already knows gets the whole budget.
  4. Web (scrollToEdge in packages/web/src/scroll-to.ts). It waits for the node to be visible, then runs one evaluate per jump, so nothing is left running in the page when a step is cancelled.
    • Container. scrollContainer picks the node, or its nearest ancestor that scrolls along the axis.
      • The walk goes through slots (a closed shadow root's slots through the closed-root registry), and through open and closed shadow roots.
      • A body whose overflow belongs to the viewport counts as not scrolling, unless containment on the root or the body (any contain value, or any container type but scroll-state, which applies none) keeps the overflow its own.
      • On the screen it picks the page when the page scrolls, otherwise the container under the viewport's center, without entering an iframe there.
      • It runs once, and the container is held by an element handle. A located row that unmounts while the list grows therefore does not lose it, and a jump, an ElementHandle.evaluate, cannot end in a Playwright TimeoutError.
      • A start node no longer in the document fails as a retryable NODE_STALE instead of falling back to the page, and so does a container that left the page when its node cannot be found again. On the screen, a re-find that runs out is the budget, since the root can't leave the page. After a container left, no re-find starts with under 50 ms left, since no jump could follow it.
    • Jump. jumpOnce sets the offset to ±scrollHeight with behavior: 'instant', so scroll-behavior: smooth is skipped and the sign covers right-to-left containers.
    • Settle. The edge is reached on a jump that moved nothing, with the extent and offset unchanged after the next rendered frame. Content an IntersectionObserver adds after that frame therefore gets another jump.
      • A frame that renders nothing, such as a cross-origin iframe out of view, is read after 100 ms.
      • Both waits use the native timers Playwright's clock keeps, as nextRenderedFrame does, so a paused page clock doesn't hold them.
      • Right before each jump it checks for a cancel and for at least 50 ms of budget, so a step cancelled or out of budget while the page was busy moves nothing.
    • Out of budget. The reason names the cause: no jump fit the budget, more content kept loading at the edge, or the budget ended before a jump confirmed the edge.
    • Wiring. The engine declares the action (packages/web/src/engine.ts:141). It is dispatched from actions.ts:398, and from the root branch in surface.ts:1273 for the screen. classifyActionError passes scrollTo's own EngineError through rather than rewriting a timeout as generic input that may have landed, so core can quote the reason.
  5. Mobile (scrollToEdge at packages/mobile/src/surface.ts:1722, the loop in dragToEdge in packages/mobile/src/scroll-to.ts:47). Each pass sends an inertial swipe, the same gesture swipeWithin builds for a node swipe, and agent-device holds the next read until the content rests.
    • Region. It is measured once, before the first drag: the node's box, or the screen for the root, clipped to the screen and cut off above the soft keyboard. It refuses, with nothing sent, a region with no area, a keyboard whose bounds are unknown, and window-local bounds (scrollRegion, :1749).
    • Stop rule. It compares regionSignature (role, name, text, value, test id, and rounded box of each node in the region, spinners left out) before and after each drag, and stops when a drag changed nothing.
      • A truncated or sparse capture never reads as unchanged. When no capture came back whole, the timeout says so.
      • The timeout message counts the drags sent.
      • No drag goes out once the step is cancelled.
    • Drag count. A read costs more than a drag, so a pass sends 1, then 2, then up to 3 drags. No pass starts that the budget cannot finish, estimated from the last read plus the last time per drag times the next count, so the loop fails with its own reason rather than being cut off mid-pass.
    • Gesture choice. fling was dropped because it holds at the end, which kills the inertia.
    • No-hidden-content flags. agent-device's "no hidden content" flags were not used: mid-list, Android reported hiddenContentAbove alone, with rows below.
  6. Desktop. The engine does not declare scrollTo, so the harness refuses it before dispatch. agent-desktop gives no edge signal.
  7. Docs.
    • reference/screen.mdx has both methods, the per-engine behavior, and a throws table.
    • reference/engine.mdx (kind table, contract excerpt, exports) and reference/mobile.mdx (perform row) list the new kind.
    • desktop.mdx says the desktop engine does not support it.
    • The Cypress, Detox, and Appium guides map their edge scroll to it. The Cypress guide's position example returns null, so it type-checks.
    • The MCP run_code method list mentions it. The e2e skill now points to reference/screen.mdx for actions (#2309), so it needs no change.
    • A patch changeset covers e2e, @e2e-dev/core, @e2e-dev/web, @e2e-dev/mobile, and @e2e-dev/desktop.

Known limits, documented in reference/screen.mdx:

  • On a device the last drag runs past the edge, so scrollTo('top') on a list with pull-to-refresh refreshes it.
  • Content that changes on its own inside the region (a timer, an auto-advancing carousel) keeps the device dragging until the timeout, so call it on the list.
  • A device drag that lands on something that doesn't scroll reads as the edge.

Verified

Ran it locally: yes, on a Mac mini, against the built packages.

  • Android emulator (pixel_api37, mobile benchmark, Huge Virtualized List of 600 rows): e2e run tests/scroll-to.e2e.ts --target android-emulator --video on, 2 of 2 passed. The existing tests/flows-lists.e2e.ts passed 7 of 7.

  • iOS simulator (iPhone 17, iOS 27.2, same list): the same command with --target ios-simulator, 2 of 2 passed. The last frame shows Row 0600. tests/flows-lists.e2e.ts passed 6 of 7. The one failure is the WebView coupon test, which calls no scroll API. It passes on CI's simulator, and #2178 records it failing on main on this one.

  • Review fixes: every fix has a test that failed on the code before it, except the mobile cancel check, whose race sits in the surface. In the last three rounds, 17 new web tests and 2 new e2e unit tests failed on the head before their fix and pass now. pnpm check, the web suite (1223 passed), the e2e unit suite (4994 passed), and the mobile suite (689 passed, last mobile change in df6d06fe) are green. The device runs are from an earlier round. On mobile, later rounds changed only scrollTo paths, and this round adds just the cancel check before a drag.

  • Paused clock (testbed, a throwaway test calling browser.clock.pauseAt, then screen.scrollTo('bottom', { timeout: 5_000 }) on /scroll): 4821c36 failed with ACTION_FAILED ... (scrollTo timed out) after the whole 5 s. This head resolves in 90 ms, at the bottom.

  • Web (testbed): e2e run tests/scroll.e2e.ts --video on, 6 of 6 passed. The two new tests scroll the page to its bottom and back while a scrollable pane sits under the viewport center, and scroll a node to its end while the page stays at 0.

  • Timeout: a throwaway Android test with an 8 s timeout failed with the output below, captured before the message switched to naming the step's timeout. It now reads within 8000ms.

    ACTION_FAILED: scrollTo('bottom') did not reach the edge within 7841ms (the content was still moving after 6 drags when the budget ran out)
    
Step (head)AndroidiOS
screen.scrollTo('bottom'), row 1 to row 60020.2 s46.8 s
screen.scrollTo('top'), back to row 120.4 s47.5 s
locator.scrollTo('bottom') on the list23.8 s51.3 s
locator.scrollTo('bottom') when already at the bottom0.8 s4.5 s

Every pass waits for agent-device's post-gesture settle, which is slower on iOS. The docs suggest a longer timeout for a list this long.

pnpm bench:ab --suite testbed -- tests/scroll.e2e.ts: the existing tests behave the same and no step changed time. The wall-clock difference is the two added tests. "Uncommitted changes" is a local, untracked mobile-benchmark config this run doesn't read.

bench-ab: c3109278 (merge base with origin/main) vs 5636241b + uncommitted changes

apps/testbed: e2e run --workers 1 tests/scroll.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±5% and 10 ms. darwin arm64, Apple M4 x10, Node 26.13.1, load 3.2.

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/scroll.e2e.ts › scrolling › scrollTo jumps the page to its bottom and back to its top (web)
  • added tests/scroll.e2e.ts › scrolling › scrollTo on a node scrolls that node to its end and leaves the page where it was (web)

Timings

Every row but the process wall clock covers the 635 tests whose behavior matched.

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)7.02 s (6.94 s-7.06 s)9.36 s (9.30 s-9.44 s)+33.2% (+31.6% to +36.7%)slower
suite (sum of attempts)5.99 s (5.98 s-6.04 s)5.97 s (5.95 s-5.99 s)-0.6% (-1.8% to +0.3%)same
action (events)752 ms (740 ms-756 ms)748 ms (736 ms-751 ms)-1.6% (-3.1% to +1.8%)same
step expect.not.toHaveText4.03 s (4.03 s-4.03 s)4.03 s (4.03 s-4.03 s)-0.0% (-0.1% to -0.0%)same, under the floor
step screen.scrollUntilVisible1.13 s (1.12 s-1.13 s)1.14 s (1.13 s-1.15 s)+1.0% (+0.1% to +1.9%)same
step app.open215 ms (209 ms-225 ms)198 ms (195 ms-203 ms)-7.2% (-24.6% to -2.9%)unresolved
step locator.tap144 ms (143 ms-146 ms)133 ms (127 ms-135 ms)-8.3% (-20.3% to -3.2%)unresolved
step screen.swipe87 ms (81 ms-91 ms)90 ms (82 ms-94 ms)+1.7% (-9.6% to +12.9%)same, under the floor
step locator.swipe79 ms (77 ms-80 ms)76 ms (67 ms-78 ms)-2.6% (-19.1% to +13.1%)same, under the floor
step expect.toHaveText55 ms (54 ms-57 ms)57 ms (55 ms-58 ms)+1.8% (-0.9% to +32.6%)same, under the floor
step expect.toHaveCount38 ms (37 ms-39 ms)36 ms (36 ms-38 ms)-4.0% (-7.7% to +2.7%)same, under the floor
step browser.mouse.wheel2 ms (1 ms-2 ms)2 ms (1 ms-2 ms)0.0% (-64.6% to +41.4%)same

Counters

Counterbasehead
attempts46
events engine1419
events poll1219
steps2438
tests passed46
Tests outside the threshold: 0 of 635

With 635 tests compared at 95%, about 31.8 could land outside by chance.

Videos, in order: Android screen.scrollTo, Android locator.scrollTo, iOS screen.scrollTo, iOS locator.scrollTo, web screen.scrollTo, web locator.scrollTo.

Not verified: the desktop engine (it refuses the action), and left/right on a device, since the mobile benchmark has no horizontal list (both edges and right-to-left are covered on the web).

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: not breaking.
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • 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, @e2e-dev/mobile, and @e2e-dev/desktop.

Written by Claude Opus 5.5 in Claude Code (T3 Code). Gemini 4 Neon (Antigravity) critiqued the design and reviewed the code, a fresh-context Claude reviewer checked the diff before it was opened, and the review fixes answer kdy1's, okwasniewski's, and michal-e2e's reviews.

https://github.com/user-attachments/assets/b03fb104-844d-4d2c-b407-162e1756e293

https://github.com/user-attachments/assets/ff47b035-73b1-4b77-bae2-571332d6b14b

https://github.com/user-attachments/assets/d12247e5-faf3-4087-818b-1db1f495f496

https://github.com/user-attachments/assets/df87191a-cb79-434a-8b2a-33a6465b1520

https://github.com/user-attachments/assets/4a5993f9-bac3-4600-8b1b-ef4ff92cd88f

https://github.com/user-attachments/assets/6097b307-3a7e-4b7d-945e-85c0f661785e