feat(core): capture one match with locator.screenshot()

Merged
OK
5d ago
+588-14

What

locator.screenshot(label?) captures the one match and returns the file's absolute path, as app.screenshot() does for the screen.

const card = await screen.getByTestId('order-summary').screenshot('summary');
  • It scrolls the match into view, waits until its box and the viewport hold still across the capture, and cuts the engine's screenshot to that box. Masking and pixel density are app.screenshot()'s.
  • A match larger than the viewport keeps the part inside it.
  • It works on every engine whose screenshot is a PNG of the viewport (web, mobile, desktop): nothing in the engine contract changes.
  • The file is an attempt artifact under screenshots/, attached to a locator.screenshot step, and the list reporter names it in its Screenshots row.

Why

A screenshot of one element meant app.screenshot() plus a hand-written crop from boundingBox(), and the testbed's own suite carries that crop helper. A @e2e-dev/core script that saves one image per card of a page could not do it at all without a PNG library of its own.

How it works

  1. Locator.screenshot (packages/core/src/types.ts:655) is implemented in LocatorImpl (packages/core/src/locator/screen.ts:658): it refuses anything but a string label before the step starts, runs captureMatch inside a locator.screenshot step, registers the file with the attempt's artifacts, and returns the joined path. ScreenContext carries the attempt's ArtifactSink for that, wired in createScreenContext and in run_code.
  2. captureMatch (packages/core/src/locator/element-screenshot.ts:27) scrolls the match into view when the engine declares scrollIntoView, then polls rounds until one keeps a file:
    1. read the match's box; a missing or hidden match ends the round;
    2. read the viewport from one observation. When no part of the box is inside it, the round ends with no capture, after scrolling the match back: a layout shift can push it out after the first scroll (:58);
    3. take the engine's artifacts.screenshot (:65);
    4. read the box and the viewport again; if either changed, the capture shows the wrong pixels, so the round ends (:69);
    5. decode the file, take the part of the box inside the viewport, scale it by image.width / viewport.width for both axes, as the engines scale their own masks (:75), cut, and write the file back.
  3. A round that does not keep its file deletes it in finally (:91), whatever ended it. A retryable engine error (the document was replaced mid-round) ends the round and the next one reads again (:88); any other error is translated like a locator action's. The artifact is registered under the path the engine returned, which is /-separated on every platform (:84); only the returned absolute path is joined natively.
  4. At the deadline the error is LOCATOR_NOT_FOUND with the last reason: never visible, kept moving, or outside the viewport. An engine screenshot that is not a PNG is UNSUPPORTED_CAPABILITY (:105): app.screenshot() passes any format on, a crop has to decode it.
  5. The list reporter's Screenshots row takes locator.screenshot steps beside app.screenshot ones (packages/e2e/src/report/list.ts:217).
  6. run_code's recorder classed any call on screen that returns data as a read and left it out of the written test. A locator screenshot scrolls the app and leaves a file, so ANSWERING_ACTIONS keeps it as an action (packages/e2e/src/session-tools/code-recorder.ts:339), as an app.screenshot() line already is.

Dropped: asking the engine for element pixels through a new contract method. The whole-screen capture already masks secure fields at the source on every engine, so a crop keeps each guarantee app.screenshot() has and needs no engine work. Also dropped: cropping the observation's pixels, as toHaveScreenshot does. Those are one image pixel per CSS pixel by design, and a saved image should have the device's density.

Not done: an engine with no artifacts reports UNSUPPORTED_CAPABILITY only after the match was found and scrolled to, since a session exposes no screenshot capability flag to check first.

Docs: the entry and table rows in docs/reference/screen.mdx, the capability row in docs/reference/engine.mdx, the reporter line in docs/reference/app.mdx, docs/debugging.mdx, the Playwright migration rows, and skills/e2e/references/debugging.md with its plugin copy.

Issue

None. Requested by a maintainer.

Verified

Ran it locally: yes

  • A @e2e-dev/core script on the built packages: one page with three cards, 1000 by 700 viewport, app.screenshot() then getByTestId(id).screenshot(id) for each card. The third card starts below the viewport.
app.screenshot        screenshots/001-screen.png   1000 x 700
locator.screenshot s1 screenshots/002-s1.png       1000 x 395
locator.screenshot s2 screenshots/003-s2.png       1000 x 490
locator.screenshot s3 screenshots/004-s3.png       1000 x 593
no match -> LOCATOR_NOT_FOUND
app.screenshot()locator.screenshot() of the second card
screencard
  • Testbed, the built CLI: e2e run tests/screenshot.e2e.ts --grep "locator screenshot". Two new tests compare the file byte for byte with a crop of app.screenshot() at boundingBox(), one of them for a match that starts below the viewport. The summary names the labeled file:
 ✓ |web| tests/screenshot.e2e.ts (2 tests) 540ms
      Tests  2 passed (2)
Screenshots  .e2e/results/screenshot-locator-screenshot-captures-the-8902006a1d5af07c/attempt-1/screenshots/001-box.png
  • packages/e2e/tests/unit/locator-screenshot.test.ts: the crop at 1x and 2x, a match larger than the viewport, a match that moves during the first capture (two captures, one file), a match outside the viewport (no capture), an image that shows more than the viewport (the crop stops at the viewport's edge), a viewport that changes size during the capture (retaken), a match a layout shift moves out after the first scroll (scrolled back, one capture), the registered path as the engine named it, a JPEG from the engine (UNSUPPORTED_CAPABILITY, no file left), an engine failure after the capture (no file left), and a document replaced mid-round.
  • packages/e2e/tests/integration/locator-screenshot-report.test.ts: a real run's locator.screenshot step links one screenshot artifact, its path has no backslash, and the report passes report-1 validation.
  • Not run on a device or the desktop engine. The crop relates the screenshot to Observation.viewport by the width ratio, which is how the mobile engine places its own masks on the same file (packages/mobile/src/surface.ts).

bench-ab: 0007636f (merge base with origin/main) vs b828b996

apps/testbed: e2e run --workers 1 tests/forms.e2e.ts. 6 pairs in random order, resolved after 6 pairs. Threshold ±3% and 10 ms. darwin arm64, Apple M5 Pro x15, Node 24.19.0, load 3.7.

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/screenshot.e2e.ts › locator screenshot › captures the match and nothing around it (web)
  • added tests/screenshot.e2e.ts › locator screenshot › scrolls a match below the viewport into view first (web)

Timings

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

Metricbase median (IQR)head median (IQR)change (95% CI)verdict
wall (process)2.47 s (2.47 s-2.47 s)2.46 s (2.45 s-2.47 s)-0.7% (-0.9% to -0.2%)same
suite (sum of attempts)1.78 s (1.78 s-1.78 s)1.78 s (1.77 s-1.78 s)-0.1% (-0.4% to +0.5%)same, under the floor
action (events)307 ms (306 ms-310 ms)311 ms (307 ms-321 ms)+0.2% (-2.5% to +5.5%)same, under the floor
step expect.not.toBeChecked1.01 s (1.01 s-1.01 s)1.01 s (1.00 s-1.01 s)-0.0% (-0.3% to +0.1%)same, under the floor
step app.open187 ms (186 ms-188 ms)186 ms (184 ms-188 ms)-0.8% (-1.6% to +1.9%)same, under the floor
step locator.fill146 ms (145 ms-147 ms)145 ms (144 ms-147 ms)-0.7% (-2.0% to +2.1%)same, under the floor
step locator.tap57 ms (56 ms-60 ms)62 ms (60 ms-62 ms)+3.5% (-8.5% to +21.8%)same, under the floor
step locator.pressSequentially42 ms (41 ms-42 ms)41 ms (41 ms-42 ms)0.0% (-4.9% to 0.0%)same
step locator.check34 ms (33 ms-35 ms)34 ms (33 ms-35 ms)-1.4% (-7.3% to +22.4%)same, under the floor
step locator.uncheck33 ms (32 ms-33 ms)33 ms (32 ms-33 ms)-1.5% (-7.5% to +6.4%)same, under the floor
step expect.toBeVisible16 ms (15 ms-17 ms)16 ms (16 ms-16 ms)0.0% (-8.7% to +6.7%)same
step locator.clear16 ms (15 ms-16 ms)15 ms (15 ms-16 ms)0.0% (-8.7% to 0.0%)same
step locator.selectOption15 ms (15 ms-15 ms)16 ms (15 ms-16 ms)+3.3% (-3.2% to +6.7%)same, under the floor
step expect.toHaveValue8 ms (7 ms-8 ms)7 ms (6 ms-7 ms)-6.5% (-19.8% to +8.0%)same, under the floor
step expect.toHaveCount5 ms (4 ms-5 ms)5 ms (5 ms-5 ms)+9.5% (0.0% to +25.0%)same, under the floor
step expect.toHaveText4 ms (4 ms-4 ms)5 ms (4 ms-5 ms)+11.8% (0.0% to +25.0%)same, under the floor
step expect.toBeChecked2 ms (2 ms-3 ms)2 ms (2 ms-2 ms)0.0% (-33.3% to 0.0%)same

Counters

Counterbasehead
tests skipped602604
Tests outside the threshold: 0 of 606

With 606 tests compared at 95%, about 30.3 could land outside by chance.

  • Docs pages: not rendered. The edits add one method entry and table rows.

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.