feat(web): emulate a touchscreen with web({ hasTouch: true })

Draft
SA
4d ago
+353-54

What

web({ hasTouch: true }) emulates a touchscreen on a web target. A single tap() on a node or at a point sends touchstart and touchend before the browser fires click. click() stays a mouse click that waits for navigation, as in Playwright. hasTouch is off by default.

targets: [{ name: 'web', engine: web({ hasTouch: true }), app: { url } }]

await screen.getByRole('button', 'Go').tap();   // sends touchstart and touchend
await screen.tapAt({ x: 40, y: 30 });           // sends the same events at viewport coordinates
await screen.getByRole('link', 'Next').click(); // a mouse click that waits for the next page

Why

Web targets sent taps as mouse clicks, so tests could miss bugs in an app's touch handlers.

How it works

  1. web() throws INVALID_CONFIG when hasTouch isn't a boolean, naming the received type (packages/web/src/engine.ts:100). Like userAgent, hasTouch: true throws INVALID_CONFIG with connect.reconnectEndpoint or an attempt-scoped browser provider, because it applies only when a context is created. hasTouch: false is accepted there (packages/web/src/engine.ts:116, :212).
  2. Every context the engine creates gets the option, including contexts from app.clearState() and session state restores (packages/web/src/surface.ts:618).
  3. Core still dispatches click() as a tap, and marks it with an optional click: true (packages/core/src/locator/screen.ts:429, :431, :446). click is on the single-tap LocatorAction and PointerAction forms (packages/core/src/engine/contract.ts:323, :412) and crosses JSON-RPC as is. Other engines still treat the action as a tap. A new kind would have made every engine declare it.
  4. A single tap on a node goes through Playwright's tap() instead of click(), unless it carries click: true. That includes a tap through a checkbox's label and a tap with held modifiers (packages/web/src/actions.ts:292, :173, :208).
  5. A tap at a point goes through page.touchscreen.tap, unless it carries click: true (packages/web/src/actions.ts:417).
  6. A swipe on a node skips the :hover shortcut when touch is on (packages/web/src/support.ts:611, passed from packages/web/src/actions.ts:394). A touch tap leaves :hover on the tapped node without moving Playwright's mouse, so the wheel would land at the old mouse position instead of the node.
  7. Every other action is unchanged. click(), tap({ count }) above 1, double taps, long presses, check(), uncheck(), drags, and screen.swipe still use the mouse, with or without hasTouch.
  8. A touch tap doesn't wait for a navigation it starts. It leaves the mouse pointer where it was. Both caveats and the toHaveURL link are in the hasTouch reference entry.

Issue

Part of #1374 and ENG-1022. Touch gestures beyond taps are left for later.

Verified

Ran it locally: yes.

  • Built CLI on the testbed /pointer page, with targets web({ hasTouch: true }) and web(). The test calls tap(), click(), and screen.tapAt(), and logs the events the page received after each:
|chromium-touch|
tap(): touchstart,touchend,mousedown,click
click(): mousedown,click
screen.tapAt(): touchstart,touchend,mousedown,click
|chromium-default|
tap(): mousedown,click
click(): mousedown,click
screen.tapAt(): mousedown,click
  • Firefox, from an earlier head that covered a locator tap and a point tap only, with web({ browser: 'firefox', hasTouch: true }) and web({ browser: 'firefox' }):
|firefox-touch| a locator tap and a point tap
events: touchstart,touchend,mousedown,click,touchstart,touchend,mousedown,click
|firefox-default| a locator tap and a point tap
events: mousedown,click,mousedown,click

Firefox doesn't launch on this machine, so click() under hasTouch hasn't run there.

  • In Chromium, packages/web/tests/integration/touch.test.ts covers: a node tap, a tap through a checkbox's label, a tap with Shift held, a point tap, a tap after the context is replaced, a list swipe after a tap inside the list, click() on a node and at a point (mouse events only), click() waiting for a navigation to a page that answers after 400 ms, and a plain click with hasTouch left out. The navigation test fails when the tap goes through the touchscreen. The list swipe test fails without the fix in step 6. On an earlier head, the original 6 tests passed in Firefox 159 with the surface switched to browser: 'firefox'. The tests added since haven't run in Firefox.

  • packages/e2e/tests/unit/engine-protocol.test.ts checks that click: true crosses JSON-RPC in perform and performAt. packages/e2e/tests/types/sdk-types.ts pins it as true | undefined.

  • pnpm bench:ab on the testbed pointer tests, the default hasTouch: false path:

    Behavior and counters match on every run. The timings are unresolved, since the machine's load was 44 during the run.

bench-ab: c371f78c (merge base with origin/main) vs 9468d06d

apps/testbed: e2e run --workers 1 tests/pointer.e2e.ts. 8 pairs in random order, stopped at the 8-pair budget, unresolved. Threshold ±5% and 10 ms. darwin arm64, Apple M5 Pro x18, Node 24.22.1, load 44.1.

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)2.91 s (2.82 s-3.34 s)2.83 s (2.77 s-3.13 s)-0.8% (-6.7% to +5.9%)unresolved
suite (sum of attempts)1.96 s (1.94 s-2.17 s)1.98 s (1.91 s-2.03 s)-1.7% (-7.4% to +6.3%)unresolved
action (events)1.06 s (1.03 s-1.08 s)1.05 s (1.03 s-1.06 s)-1.4% (-3.9% to +3.2%)same
step locator.tap831 ms (820 ms-840 ms)836 ms (824 ms-838 ms)+0.1% (-0.2% to +1.0%)same, under the floor
step app.open452 ms (435 ms-495 ms)438 ms (421 ms-473 ms)-2.2% (-15.0% to +9.7%)same, under the floor
step screen.swipe136 ms (125 ms-144 ms)126 ms (108 ms-134 ms)-7.3% (-20.0% to +9.8%)unresolved
step locator.click46 ms (42 ms-46 ms)43 ms (43 ms-45 ms)-4.3% (-8.7% to +2.4%)same, under the floor
step screen.tapAt32 ms (30 ms-36 ms)31 ms (30 ms-31 ms)-3.2% (-13.9% to +17.8%)same, under the floor
step expect.toHaveText30 ms (28 ms-35 ms)30 ms (27 ms-31 ms)-2.2% (-22.6% to +30.9%)same, under the floor
step locator.fill28 ms (26 ms-30 ms)28 ms (26 ms-31 ms)+3.3% (-17.2% to +20.8%)same, under the floor
step browser.mouse.move18 ms (17 ms-21 ms)20 ms (18 ms-31 ms)-3.0% (-15.6% to +72.2%)same, under the floor
step screen.doubleTapAt8 ms (8 ms-8 ms)8 ms (8 ms-10 ms)+6.9% (0.0% to +37.5%)same, under the floor
step browser.evaluate4 ms (3 ms-4 ms)4 ms (4 ms-5 ms)+26.5% (-13.4% to +66.7%)same, under the floor
step expect.toHaveValue3 ms (2 ms-3 ms)2 ms (2 ms-2 ms)-18.4% (-65.2% to +33.3%)same, under the floor
step browser.mouse.down1 ms (1 ms-1 ms)1 ms (1 ms-1 ms)0.0% (0.0% to 0.0%)same
step browser.mouse.up1 ms (1 ms-1 ms)1 ms (1 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 617

With 617 tests compared at 95%, about 30.9 could land outside by chance.

  • Video: the chromium-touch run, attached below.
  • Docs screenshots: not captured. The repo doesn't have a local docs preview, so the changed reference/web.mdx entry was checked with pnpm docs:check only.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • 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.
  • 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.

https://github.com/user-attachments/assets/1551ed1e-9950-4634-9f2b-0ef51206044a