feat(core): capture the whole page or a clip in screenshots (ENG-1223)
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
clipof the viewport works on any engine that observes and writes PNG screenshots. e2e cuts it from the engine's own screenshot. fullPageneeds an engine that declares the newfullPageScreenshotscapability. The web and Electron engines declare it; mobile and desktop fail withUNSUPPORTED_CAPABILITYbefore anything is captured, since a native screen has no page past it.- A locator's
toHaveScreenshottakes 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
-
Engine contract. A new
Engine.fullPageScreenshotsflag (packages/core/src/engine/index.ts), which requiresartifacts. 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 ofartifacts.screenshot;fullPagebesidepixelsinobserve;- 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.jsonand both fixtures.artifacts/screenshotgains anoptionsparam. An older server never declares the flag, so it never receivesoptions. -
Session gate.
requireFullPageinpackages/core/src/engine/session.ts:321refuses a full-page screenshot or observation withConfigurationError('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. -
Shared region logic.
packages/core/src/internal/screenshot-region.tsownsScreenshotRegion, its parser (finite numbers, positive size, no unknown keys),clipOutsideViewport, andcutScreenshotFile.cutScreenshotFilecuts an engine PNG at its own density and now also serveslocator.screenshot(), which had a copy of the same crop. -
app.screenshot.packages/core/src/run/app-screenshot.tstakes(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_ARGUMENTbefore anything is captured. A clip under one pixel of the screenshot isINVALID_ARGUMENTtoo, and its file is deleted.
-
toHaveScreenshot.parseScreenshotCallmakes a discriminatedScreenshotTarget, either a locator or a screen region (packages/e2e/src/expect/screenshot-call.ts).captureOnceturns it into oneCut(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.
- Masks are placed at
-
Web engine. It declares the flag (
packages/web/src/engine.ts:145).artifacts.screenshotpassesfullPageandclipto Playwright (packages/web/src/screenshot-region.ts). Playwright's "Clipped area is either empty or outside" error becomesINVALID_ARGUMENT.- Full-page observation pixels come at the CSS scale they ask Playwright for (
scale: 1), withscrollread 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.
-
Electron engine. It declares the flag too (
packages/electron/src/engine.ts).capturePageinpackages/electron/src/capture.tssendsPage.captureScreenshotwithcaptureBeyondViewport: trueand a clip overPage.getLayoutMetrics'cssContentSize, cut to the call's clip. A clip with nothing of the page inside it isINVALID_ARGUMENT.- The window is not scrolled or resized. Chromium paints a
position: fixedelement 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
scrollfromcssVisualViewportand 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_CAPABILITYand 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.
- The window is not scrolled or resized. Chromium paints a
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.tspasses 34/34. It includes the Playwrightpage-screenshot.spec.tscases the file had skipped for want ofclip/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.
- a password field 900 px below the fold is black in
-
The scroll test guards the scroll fix: with the web engine's
scrollforced to{ x: 0, y: 0 }, the scrolled-mask test failed withASSERTION_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/electronintegration 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. - whole-page pixels on a 2x display scrolled to 2500 px (
-
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.tspasses on both (iOS 26 passed, 1 skipped; Android 27 passed, 1 skipped). Its new test checks thatapp.screenshot({ clip })of the header is the same bytes aslocator.screenshot()of it, that a clip below the screen isINVALID_ARGUMENT, and thatfullPageisUNSUPPORTED_CAPABILITYfor bothapp.screenshotandtoHaveScreenshot. A throwawayexpect(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
refusalstests after it; it now runs before the test that returns to the home list. -
Integration and unit tests:
screenshot-assertions.test.tsover 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 ofoptionsandscroll.
✓ |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 coordinates | Full page, password 900 px below the fold |
|---|---|
iOS simulator, toHaveScreenshot({ clip }) | Android emulator, toHaveScreenshot({ 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:abwas not run. The default capture path does the same work as on main: the full-page branches run only when a call asks forfullPage. - 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.