Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(full-page-screenshot): revise logic for determining dimensions #14920

Merged
merged 19 commits into from
Mar 8, 2024

Conversation

alexnj
Copy link
Member

@alexnj alexnj commented Mar 22, 2023

Fixes #14824. More details on the issue.

  • Fixes the issue that resulted in full height of the page being clipped in some cases.
  • Fixes discrepancy in dimensions due to horizontal overflow (overflow-x) of elements on the page.
  • Simplifies the calculation a bit by relying solely on DevTools metrics API.

@alexnj alexnj requested a review from a team as a code owner March 22, 2023 21:21
@alexnj alexnj requested review from brendankenny and removed request for a team March 22, 2023 21:21
Copy link
Member

@adamraine adamraine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to play around with this, but can you add a smoke test that tests the horizontal overflow case.

screenshotData = [];
mockContext = createMockContext();
mockContext.driver.defaultSession.sendCommand.mockImplementation((method) => {
if (method === 'Page.getLayoutMetrics') {
return {
cssContentSize: contentSize,
// See comment within _takeScreenshot() implementation
cssLayoutViewport: {clientWidth: screenSize.width, clientHeight: screenSize.height},
cssVisualViewport: {clientWidth: contentSize.width, clientHeight: contentSize.height},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I think we should just remove this unit test file. It requires us to effectively simulate how chrome handles all these different width/height scales. We're better off using smoke tests for this gatherer IMO.

@adamraine
Copy link
Member

adamraine commented Mar 22, 2023

Interesting I'm getting a report error on this page:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=0.5">
  <title>Document</title>
</head>
<body style="overflow: scroll;">
  <h1>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</h1>
</body>
</html>

EDIT: looks like a rounding error. height needs to be an integer when passed into Emulation.setDeviceMetricOverrides

@alexnj
Copy link
Member Author

alexnj commented Mar 22, 2023

EDIT: looks like a rounding error. height needs to be an integer when passed into Emulation.setDeviceMetricOverrides

Thanks! That explains the Math.round() in the old code. I'll add that back and add a comment for future.

// Since we resized emulated viewport to match the desired screenshot size,
// it is safe to rely on scaled visual viewport css dimensions.
width: Math.round(metrics.cssVisualViewport.clientWidth * metrics.cssVisualViewport.scale),
height: Math.round(metrics.cssVisualViewport.clientHeight * metrics.cssVisualViewport.scale),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this set to the above height variable, the one given the setDeviceMetricsOverride?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it should be exactly the same I guess. Seems good.

@connorjclark
Copy link
Collaborator

connorjclark commented Jul 26, 2023

We must do a lot of manual testing whenever we update this code, since we can't really automate "does this line up ok?" . It'd be good to collect a list of test URLs and write down some methodology (look at desktop, mobile, under userflow w/ pupeteer emulation provided, etc...), so we are all looking at the same thing in review (and for future changes). And really, we ought to just have a script to generate all such reports so we can just peruse them.

I could make this script as part of reviewing. Please share any URLs / thoughts you have on how to best verify the change.

@alexnj
Copy link
Member Author

alexnj commented Jul 27, 2023

@connorjclark so the reason for the minor mismatch in lining up horizontal coordinates was macOS scrollbar setting, depending on if you set auto/always/while-scrolling. Same issue that @adamraine found earlier as well. I don't know if there's a reliable way to test this in automation.

The scenarios that we need to cover are (that I tested manually today):

  • device dpi differences
  • zoom/scaling factor
  • horizontal scroll
  • on desktop, specifically macOS: scrollbar setting (always vs. while scrolling)

The fps-* series of smoke tests cover the first two.

Copy link
Member

@adamraine adamraine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to remove my request changes blocker but someone else should review.

After testing this myself, I think our calculation for the desired emulated height was correct. However we were using the incorrect metrics for the final screenshot size.

@connorjclark connorjclark changed the title core: revise logic for determining screenshot dimensions core(full-page-screenshot): revise logic for determining dimensions Mar 8, 2024
Copy link
Collaborator

@connorjclark connorjclark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this as well, looks great, lets land it.

@connorjclark connorjclark merged commit 5854c54 into main Mar 8, 2024
29 checks passed
@connorjclark connorjclark deleted the fullpage-screenshot-recalc branch March 8, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect width and height of full page screenshot
5 participants