-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(full-page-screenshot): add node verification tests and debug tool
- Loading branch information
1 parent
100c54a
commit 35a27c8
Showing
5 changed files
with
479 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.