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

tests(full-page-screenshot): add node verification and debug tool #15324

Merged
merged 7 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cli/test/fixtures/screenshot-nodes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!--
* Copyright 2023 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-->

<head>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<style>
p {
background-color: white;
}

div {
width: 150px;
}
div,span,img {
margin: 10px;
padding: 5px;
}
/* Want a LCP, but not any image or text visible! */
img {
opacity: 0.001;
}
span {
display: inline-block;
}

[id*='red'],span[id*='red'] {
background-color: #8a4343;
}
[id*='green'],span[id*='green'] {
background-color: #427f36;
}
</style>
</head>

<body>
<p id="textEl"></p>

<script>
const params = new URLSearchParams(document.location.search);
if (params.has('grow')) {
textEl.style.backgroundColor = 'lightgrey';
let padding = 1;
setInterval(() => {
if (padding > 300) return;

textEl.style.paddingTop = padding + 'px';
padding += 1;
}, 50);
}
</script>

<div id="el-1-red" role="treeitem"></div>
<div id="red">
<span id="green"><img id="el-2-green" src="launcher-icon-4x.png" width="50" height="50"></span>
</div>
</body>
4 changes: 2 additions & 2 deletions core/gather/gatherers/full-page-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// JPEG quality setting
// Exploration and examples of reports using different quality settings: https://docs.google.com/document/d/1ZSffucIca9XDW2eEwfoevrk-OTl7WQFeMf0CgeJAA8M/edit#
// Note: this analysis was done for JPEG, but now we use WEBP.
const FULL_PAGE_SCREENSHOT_QUALITY = 30;
const FULL_PAGE_SCREENSHOT_QUALITY = process.env.LH_FPS_TEST ? 100 : 30;

// https://developers.google.com/speed/webp/faq#what_is_the_maximum_size_a_webp_image_can_be
const MAX_WEBP_SIZE = 16383;
Expand Down Expand Up @@ -159,7 +159,7 @@
for (const [node, id] of lhIdToElements.entries()) {
// @ts-expect-error - getBoundingClientRect put into scope via stringification
const rect = getBoundingClientRect(node);
nodes[id] = rect;
nodes[id] = {id: node.id, ...rect};

Check warning on line 162 in core/gather/gatherers/full-page-screenshot.js

View check run for this annotation

Codecov / codecov/patch

core/gather/gatherers/full-page-screenshot.js#L162

Added line #L162 was not covered by tests
}

return nodes;
Expand Down
83 changes: 83 additions & 0 deletions core/scripts/full-page-screenshot-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @license Copyright 2023 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

// To open result in chrome:
// node core/scripts/full-page-screenshot-debug.js latest-run/lhr.report.json | xargs "$CHROME_PATH"
// But, it might be too large for xargs:
// node core/scripts/full-page-screenshot-debug.js latest-run/lhr.report.json | code -

import * as fs from 'fs';

import * as puppeteer from 'puppeteer-core';
import {getChromePath} from 'chrome-launcher';

/**
* @param {LH.Result} lhr
* @return {Promise<string>}
*/
async function getDebugImage(lhr) {
if (!lhr.fullPageScreenshot) {
return '';
}

const browser = await puppeteer.launch({
headless: true,
executablePath: getChromePath(),
ignoreDefaultArgs: ['--enable-automation'],
});
const page = await browser.newPage();

const debugDataUrl = await page.evaluate(async (fullPageScreenshot) => {
const img = await new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = fullPageScreenshot.screenshot.data;
});

// eslint-disable-next-line no-undef
const canvasEl = document.createElement('canvas');
canvasEl.width = img.width;
canvasEl.height = img.height;
const ctx = canvasEl.getContext('2d');
if (!ctx) return '';

ctx.drawImage(img, 0, 0);
for (const [lhId, node] of Object.entries(fullPageScreenshot.nodes)) {
if (!node.width && !node.height) continue;

ctx.strokeStyle = '#D3E156';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.rect(node.left, node.top, node.width, node.height);
ctx.stroke();

const txt = node.id || lhId;
const txtWidth = Math.min(ctx.measureText(txt).width, node.width);
const txtHeight = 10;
const txtTop = node.top - 3;
const txtLeft = node.left;
ctx.fillStyle = '#FFFFFF88';
ctx.fillRect(txtLeft, txtTop, txtWidth, txtHeight);
ctx.fillStyle = '#000000';
ctx.lineWidth = 1;
ctx.textBaseline = 'top';
ctx.fillText(txt, txtLeft, txtTop);
}

return canvasEl.toDataURL();
}, lhr.fullPageScreenshot);

await browser.close();

return debugDataUrl;
}

const lhr = JSON.parse(fs.readFileSync(process.argv[2], 'utf-8'));
const result = await getDebugImage(lhr);

console.log(result);
Loading
Loading