feat(core): capture the whole page or a clip in screenshots (ENG-1223)

OK
12h ago
+1458-190

What

app.screenshot() and expect(screen).toHaveScreenshot() now take fullPage and clip, as Playwright's page.screenshot() and toHaveScreenshot() do:

await app.screenshot('receipt', { fullPage: true });
await app.screenshot({ clip: { x: 0, y: 0, width: 1280, height: 80 } });
await expect(screen).toHaveScreenshot('landing.png', { fullPage: true, mask: [screen.getByTestId('clock')] });
  • A clip of the viewport works on any engine that observes and writes PNG screenshots. e2e cuts it from the engine's own screenshot.
  • fullPage needs an engine that declares the new fullPageScreenshots capability. The web and Electron engines declare it; mobile and desktop fail with UNSUPPORTED_CAPABILITY before anything is captured, since a native screen has no page past it.
  • A locator's toHaveScreenshot takes neither option.

Why

ENG-1223, a parity gap from the framework audit: a screenshot was always the viewport, so users of Playwright, Cypress, Selenium, and wdio had no full-page capture and no clip, either as an artifact or as a visual comparison. Element capture already shipped as locator.screenshot() (#2138); this PR adds the other two parts and updates the matching rows in docs/migrate/.

How it works

  1. Engine contract. A new Engine.fullPageScreenshots flag (packages/core/src/engine/index.ts), which requires artifacts. An engine that declares it gets the following, and it masks every secure field on the whole page:

    • { fullPage: true, clip? } as the third argument of artifacts.screenshot;
    • fullPage beside pixels in observe;
    • it reports ObservationPixels.scroll, how far the page is scrolled.

    The flag is checked by defineEngine (manifest.ts:314), and it crosses the protocol manifest, engine-manifest-v1.schema.json and both fixtures. artifacts/screenshot gains an options param. An older server never declares the flag, so it never receives options.

  2. Session gate. requireFullPage in packages/core/src/engine/session.ts:321 refuses a full-page screenshot or observation with ConfigurationError('UNSUPPORTED_CAPABILITY') before the engine is called. This means an engine that predates the flag can never return the viewport in place of the page.

  3. Shared region logic. packages/core/src/internal/screenshot-region.ts owns ScreenshotRegion, its parser (finite numbers, positive size, no unknown keys), clipOutsideViewport, and cutScreenshotFile. cutScreenshotFile cuts an engine PNG at its own density and now also serves locator.screenshot(), which had a copy of the same crop.

  4. app.screenshot. packages/core/src/run/app-screenshot.ts takes (label?, options?) or (options).

    • A full page goes to the engine, clipped or not; the clip is in page coordinates.
    • A viewport clip reads the viewport, takes the engine's screenshot on its own budget, and cuts it.
    • A clip with nothing of the viewport inside it is INVALID_ARGUMENT before anything is captured. A clip under one pixel of the screenshot is INVALID_ARGUMENT too, and its file is deleted.
  5. toHaveScreenshot. parseScreenshotCall makes a discriminated ScreenshotTarget, either a locator or a screen region (packages/e2e/src/expect/screenshot-call.ts). captureOnce turns it into one Cut (packages/e2e/src/expect/screenshot-capture.ts), asks for full-page pixels when the region needs them, and cuts the clip.

    • Masks are placed at rect + scroll - corner, so they land on the page wherever it is scrolled (line 85).
    • A viewport clip with nothing inside fails at once.
    • A full-page clip past the end of the page keeps polling, since the page may still grow.
  6. Web engine. It declares the flag (packages/web/src/engine.ts:145).

    • artifacts.screenshot passes fullPage and clip to Playwright (packages/web/src/screenshot-region.ts). Playwright's "Clipped area is either empty or outside" error becomes INVALID_ARGUMENT.
    • Full-page observation pixels come at the CSS scale they ask Playwright for (scale: 1), with scroll read from the page (packages/web/src/observe.ts). A page width measured before Playwright stops animations could disagree with the capture, so none is used. Playwright's own mask covers secure fields below the fold too.
  7. Electron engine. It declares the flag too (packages/electron/src/engine.ts). capturePage in packages/electron/src/capture.ts sends Page.captureScreenshot with captureBeyondViewport: true and a clip over Page.getLayoutMetrics' cssContentSize, cut to the call's clip. A clip with nothing of the page inside it is INVALID_ARGUMENT.

    • The window is not scrolled or resized. Chromium paints a position: fixed element where it shows at the current scroll, so the existing masks, measured in viewport coordinates, land on their password fields and nested documents anywhere on the page. The same placed, moved, and appeared checks run before the image goes out.
    • Full-page observation pixels report scroll from cssVisualViewport and a scale over the page's CSS width.
    • Under browser zoom the capture reads its clip in zoomed pixels, so the page rectangle and the caller's clip are scaled by cssVisualViewport.zoom.
    • Past 16384 image pixels on a side, Electron's Chromium repeats the top of the page instead of painting the rest, so such a capture is UNSUPPORTED_CAPABILITY and the error says to clip it. Playwright's full-page capture on the web engine paints a 22,000 px page correctly at 1x and 2x, so the web engine has no such limit.

Not in this PR: path, a returned buffer, JPEG type/quality, omitBackground, animations, caret, scale, stylePath, subfolder names, fullPage on the MCP screenshot tool, a mobile capture without the status bar, and tiling for Electron pages past 16384 px. A viewport app.screenshot() in Electron under browser zoom was already wrong before this PR and still is; that's for a separate fix.

Issue

ENG-1223

Verified

Ran it locally: yes

  • Testbed, real Chromium: tests/screenshot.e2e.ts passes 34/34. It includes the Playwright page-screenshot.spec.ts cases the file had skipped for want of clip/fullPage: clip rect, clip rect with fullPage, clip trimmed to the viewport, clip outside the viewport, negative clip size, three clips in a row, fullPage, and viewport restored afterwards. Each one compares pixels against the same box cut from a viewport screenshot. New cases:

    • a password field 900 px below the fold is black in app.screenshot({ fullPage: true });
    • toHaveScreenshot({ fullPage, mask }) matches a reference with masks painted in page coordinates, after the page was scrolled 300 px;
    • a full-page clip past the end of the page is INVALID_ARGUMENT;
    • a full-page clip below the fold compares.
  • The scroll test guards the scroll fix: with the web engine's scroll forced to { x: 0, y: 0 }, the scrolled-mask test failed with ASSERTION_FAILED. It passes with the fix.

  • Full testbed: 52 files, 652 tests passed. Web benchmark tests/: 43 files, 95 tests passed.

  • Electron, real app: packages/electron integration tests drive a real Electron window. They cover:

    • whole-page pixels on a 2x display scrolled to 2500 px (height: 4000, scale: 1, scroll: { x: 0, y: 2500 }, window not moved);
    • a password field and a nested document below the window, masked in a whole-page file (no red pixel left);
    • with the window scrolled 500 px, fields above and below the viewport and a field fixed to its bottom, all masked;
    • zoom 1.5: the file is 300x2370 for a 200x1580 CSS page, a clip is cut at the zoom, and observation pixels come back at one per CSS pixel. Without the zoom scaling this test fails;
    • a clip in the page, one past it, and a 20,000 px page refused while a clip of it works.

    The full package passes: 157 tests. The Electron testbed passes 88 tests through the built CLI, including app.screenshot('whole', { fullPage: true }) with the window shrunk to 60 px so the password field sits below it; the field is solid black in the image.

  • iOS simulator and Android emulator: iPhone 17 Pro on iOS 27.2 and a Pixel 7 emulator on API 36, with release builds of the mobile benchmark. tests/controls.e2e.ts passes on both (iOS 26 passed, 1 skipped; Android 27 passed, 1 skipped). Its new test checks that app.screenshot({ clip }) of the header is the same bytes as locator.screenshot() of it, that a clip below the screen is INVALID_ARGUMENT, and that fullPage is UNSUPPORTED_CAPABILITY for both app.screenshot and toHaveScreenshot. A throwaway expect(screen).toHaveScreenshot('header-clip', { clip }) wrote its image on the first run and matched it on the second, on both platforms (iOS 370x22 points, Android 996x65 px).

  • What the devices caught: right after a scenario opens, the push transition still repaints the header between two captures (about 760 of 65,000 pixels differed on the emulator), so the test compares once the screen holds still. The first version of the test also left the app on the scenario and broke the refusals tests after it; it now runs before the test that returns to the home list.

  • Integration and unit tests: screenshot-assertions.test.ts over the fake engine covers a clip with masks at 2x density, full page with scrolled masks, a full-page clip, the refusals, and the viewport clip that fails at once. Unit tests cover parsing, the crop, the manifest flag, and the protocol round trip of options and scroll.

 ✓ |web| tests/screenshot.e2e.ts > page screenshot > should clip rect with fullPage
 ✓ |web| tests/screenshot.e2e.ts > page screenshot > should take fullPage screenshots
 ✓ |web| tests/screenshot.e2e.ts > full page screenshot > masks a password field below the viewport
 ✓ |web| tests/screenshot.e2e.ts > full page screenshot > compares the whole page, masks where they sit on it however far it is scrolled
 ✓ |web| tests/screenshot.e2e.ts (34 tests)
      Tests  34 passed (34)
Full page, reference with masks in page coordinatesFull page, password 900 px below the fold
full pagepassword masked
iOS simulator, toHaveScreenshot({ clip })Android emulator, toHaveScreenshot({ clip })
ios clipandroid clip
  • Desktop: not run. The desktop testbed needs the native driver built with Rust and Accessibility permission granted to the terminal, which this Mac doesn't have. A desktop clip goes through the same core cut as the mobile runs above.
  • Benchmark: pnpm bench:ab was not run. The default capture path does the same work as on main: the full-page branches run only when a call asks for fullPage.
  • Video: none. Screenshots change nothing on screen, so the stills above are the evidence.

Summary by CodeRabbit

  • New Features
    • Page-level screenshots can capture a selected rectangle or an entire scrollable page in supported web and Electron environments.
    • Screen screenshot comparisons support clipped and full-page captures; secure fields remain masked.
  • Bug Fixes
    • Invalid clip regions and unsupported full-page capture requests return clear errors.
  • Documentation
    • Added screenshot option examples and guidance, including mappings from other testing tools and the Electron full-page image size limit.