-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
.puppeteerrc.js
to helpers
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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,57 @@ | ||
// https://pptr.dev/guides/configuration | ||
// https://github.com/GoogleChrome/lighthouse-ci/blob/main/docs/configuration.md#puppeteerscript | ||
/** | ||
* @param {puppeteer.Browser} browser | ||
* @param {{url: string, options: LHCI.CollectCommand.Options}} context | ||
*/ | ||
module.exports = async (browser, context) => { | ||
// launch browser for LHCI | ||
const page = await browser.newPage(context.options) | ||
const session = await page.target().createCDPSession() | ||
await page.goto(context.url, { waitUntil: 'networkidle0' }) | ||
await startEcoindexPageMesure(page, session) | ||
await endEcoindexPageMesure() | ||
// close session for next run | ||
await page.close() | ||
} | ||
|
||
async function startEcoindexPageMesure(page, session) { | ||
page.setViewport({ | ||
width: 1920, | ||
height: 1080, | ||
}) | ||
await new Promise(r => setTimeout(r, 3 * 1000)) | ||
const dimensions = await page.evaluate(() => { | ||
var body = document.body, | ||
html = document.documentElement | ||
|
||
var height = Math.max( | ||
body.scrollHeight, | ||
body.offsetHeight, | ||
html.clientHeight, | ||
html.scrollHeight, | ||
html.offsetHeight, | ||
) | ||
return { | ||
width: document.documentElement.clientWidth, | ||
height: height, | ||
deviceScaleFactor: window.devicePixelRatio, | ||
} | ||
}) | ||
// console.log('dimensions', dimensions) | ||
// We need the ability to scroll like a user. There's not a direct puppeteer function for this, but we can use the DevTools Protocol and issue a Input.synthesizeScrollGesture event, which has convenient parameters like repetitions and delay to somewhat simulate a more natural scrolling gesture. | ||
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-synthesizeScrollGesture | ||
await session.send('Input.synthesizeScrollGesture', { | ||
x: 100, | ||
y: 600, | ||
yDistance: -dimensions.height, | ||
speed: 1000, | ||
}) | ||
} | ||
|
||
/** | ||
* End Ecoindex flow. Wait 3s. | ||
*/ | ||
async function endEcoindexPageMesure() { | ||
await new Promise(r => setTimeout(r, 15 * 1000)) | ||
} |