Chrome Dev tools Lighthouse + puppeteer
Lighthouse functions
- Classic navigations
- Page loads with cold cache
- New
- Page loads with a warm cache
- Pages with an activated Service Worker
- Accounting for potential user interactions
From recording to meassure UI-UX
Preparation - recording
- Get a script user flow (e.g. via Chrome Dev Tools recorder).
- Test it works
1
2
3
4
5
6# create node module, non interative
npm init -y
# install dependencies
npm install puppeteer lighthouse
# test
node demo_user_flow_recording.js
Modify the recording file to use Lighthouse
Modify from just replay…
1
2
3
4
5const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();… into this to meassure UI/UX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43const puppeteer = require('puppeteer');
const { startFlow } = require('lighthouse/lighthouse-core/fraggle-rock/api.js');
const fs = require("fs");
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
const flow = await startFlow(page, {
name: 'Go to homepage',
configContext: {
settingsOverrides: {
screenEmulation: {
mobile: false,
width: 1350,
height: 940,
deviceScaleFactor: 1,
disabled: false,
},
formFactor: "desktop",
},
},
});
// update viewport
await targetPage.setViewport({ "width": 940, "height": 1350 })
// add some steps
{
// capture data
await flow.navigate("https://gitlab.com/")
const targetPage = page;
const promises = [];
promises.push(targetPage.waitForNavigation());
await targetPage.goto('https://gitlab.com/');
await Promise.all(promises);
}
// generate report
const report = flow.generateReport();
fs.writeFileSync('report.html', report);
await browser.close();
Mode examples
Navigations
- Use puppeteer to open the browser.
- Start a Lighthouse user flow.
- Navigate to the target URL.
1 | import fs from 'fs'; |
Capturing a warm load
- Use puppeteer to open the browser.
- Start a Lighthouse user flow.
- Navigate to the target URL.
- Do a second navigation, disabling the clearing of cache and storage that Lighthouse does by default in navigations
1 | async function captureReport() { |
Snapshots
- Audit a single point in time: set up a page and test it in its exact state: (e.g. with a drop-down open or a form partially filled in).
- Many of the performance metrics are currently defined as beginning with a page load and so are not applicable in a snapshot, but the accessibility audits and many of the performance best practices can still yield important checks.
1 | async function captureReport() { |
Timespans
- Runs Lighthouse audits over some period of time, which may or may not include a navigation.
1 | async function captureReport() { |