Skip to content

Commit

Permalink
test(full-page-screenshot): add node verification tests and debug tool
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Aug 1, 2023
1 parent 100c54a commit 35a27c8
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 3 deletions.
58 changes: 58 additions & 0 deletions cli/test/fixtures/screenshot-nodes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!--
* 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;
}
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">Screenshot tester 2</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 @@ -14,7 +14,7 @@ import {waitForNetworkIdle} from '../driver/wait-for-condition.js';
// 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 ? 30 : 100;

// 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 @@ -160,7 +160,7 @@ class FullPageScreenshot extends BaseGatherer {
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 163 in core/gather/gatherers/full-page-screenshot.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L163 was not covered by tests
}

return nodes;
Expand Down
80 changes: 80 additions & 0 deletions core/scripts/full-page-screenshot-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @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.
*/

// node core/scripts/full-page-screenshot-debug.js latest-run/lhr.report.json | xargs "$CHROME_PATH"

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

0 comments on commit 35a27c8

Please sign in to comment.