Skip to content

fix(screenshot): scroll elements into view before downscaling - #3708

Closed
SoroushRF wants to merge 1 commit into
ChromeDevTools:mainfrom
SoroushRF:fix/screenshot-capped-element-scroll
Closed

SoroushRF wants to merge 1 commit into
ChromeDevTools:mainfrom
SoroushRF:fix/screenshot-capped-element-scroll

Conversation

@SoroushRF

Copy link
Copy Markdown
Contributor

Summary

take_screenshot with a uid can capture the wrong region when a size limit downscales the image and the target is hidden inside a scrolling container. The downscale path measures the element before scrolling and gives that clip to page.screenshot(). Capturing beyond the viewport cannot reveal content clipped by an inner overflow: auto container.

Related to #2684 and #2685, which corrected the window scroll offsets used in the screenshot clip.

Changes

  • With --screenshotMaxWidth or --screenshotMaxHeight set, scroll the target into view before measuring its bounding box and the window scroll offsets. This can move the window or an inner scroll container, even when the element fits within the limit. If no downscale clip is needed, capture with element.screenshot().
  • Extend the capped-element tests to verify that the handler scrolls the element before measuring its bounding box.

Testing

  • The added call-order assertion fails on main because the handler measures the box without scrolling. It passes with the fix.
  • I ran the built handler against Chrome for Testing 158 with a green target inside an overflow: auto container. Each limit produced an 80 × 40 PNG. The center pixel changed from the red page background on main to the green target with the fix. The uncapped capture and the capture that fit within the limit both returned the green target at 160 × 80 on each revision.
  • On Ubuntu 24.04 under WSL2, after npm run build, the unfiltered npm run test:no-build -- --test-concurrency=2 passed with 2,365 passes, four skips, zero failures and zero cancellations (Node 24.11.1, Chrome for Testing 157).
  • On Windows, tests/tools/screenshot.test.ts passes with one pre-existing omitBackground test excluded. That test gets 18,18,18 where it expects 255,255,255 on both revisions.
  • npm run format, npm run check-format, and npm run gen pass without changes to tracked files.

🤖 Generated with Claude Code

@Lightning00Blade
Lightning00Blade self-requested a review December 3, 2026 13:21
@Lightning00Blade

Copy link
Copy Markdown
Collaborator

Thanks for the fix! A couple of edge cases and inconsistencies to address before merging:

1. Unconditional scrolling before checking whether downscaling is needed (src/tools/screenshot.ts)

Scrolling whenever element && hasSizeLimit is true runs even when the element already fits within the size limit, is already fully visible, or has no bounding box (display: none / 0×0):

  • Double-scrolling on the non-downscaled path: When computeDownscaleClip() returns undefined, execution falls through to element.screenshot(), which runs Puppeteer's scrollIntoViewIfNeeded() a second time.
  • Inconsistent scroll behavior vs. element.screenshot(): Puppeteer's element.screenshot() guards scrolling with isIntersectingViewport({threshold: 1}) (which also handles SVG owner elements). Calling element.scrollIntoView() directly can unnecessarily scroll/center an already-visible element on fallback, and triggers duplicate failed scroll attempts on hidden (display: none) elements before element.screenshot() throws.
    Suggestion: Compute the initial clip first, and only scroll and re-measure when downscaling is actually needed (clip && element) and isIntersectingViewport({threshold: 1}) is false. That keeps the non-downscaled path identical to uncapped element.screenshot() and avoids unnecessary scrolling when the element already fits or is already in view.

2. Test coverage gaps (tests/tools/screenshot.test.ts)

  • Verify handle.scrollIntoView is called once with no arguments and runs before both handle.boundingBox and pptrPage.evaluate (so the scroll offset is measured post-scroll). Using distinct pre-scroll and post-scroll values would also verify that the final clip uses the post-scroll coordinates.
  • Cover the negative/fallback branches:
    • No scroll or viewport check when no size limit is set.
    • No scroll or viewport check when the element fits within the size limit.
    • No scroll when a downscaled element is already in view (ideally also covering --screenshotMaxHeight).

@Lightning00Blade

Copy link
Copy Markdown
Collaborator

Closing due to #3648

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants