Skip to content

Commit f6593c7

Browse files
authored
docs(puppeteer): update to reflect FR changes (#14319)
1 parent a36d9d0 commit f6593c7

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

docs/puppeteer.md

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,32 @@ The example below shows how to inject CSS into the page before Lighthouse audits
1414
A similar approach can be taken for injecting JavaScript.
1515

1616
```js
17-
const puppeteer = require('puppeteer');
18-
const lighthouse = require('lighthouse');
19-
const {URL} = require('url');
17+
import puppeteer from 'puppeteer';
18+
import lighthouse from 'lighthouse';
2019

21-
(async() => {
2220
const url = 'https://chromestatus.com/features';
2321

2422
// Use Puppeteer to launch headful Chrome and don't use its default 800x600 viewport.
2523
const browser = await puppeteer.launch({
2624
headless: false,
2725
defaultViewport: null,
2826
});
27+
const page = await browser.newPage();
2928

3029
// Wait for Lighthouse to open url, then inject our stylesheet.
3130
browser.on('targetchanged', async target => {
32-
const page = await target.page();
3331
if (page && page.url() === url) {
3432
await page.addStyleTag({content: '* {color: red}'});
3533
}
3634
});
3735

3836
// Lighthouse will open the URL.
3937
// Puppeteer will observe `targetchanged` and inject our stylesheet.
40-
const {lhr} = await lighthouse(url, {
41-
port: (new URL(browser.wsEndpoint())).port,
42-
output: 'json',
43-
logLevel: 'info',
44-
});
38+
const {lhr} = await lighthouse(url, undefined, undefined, page);
4539

4640
console.log(`Lighthouse scores: ${Object.values(lhr.categories).map(c => c.score).join(', ')}`);
4741

4842
await browser.close();
49-
})();
5043
```
5144

5245
### Option 2: Launch Chrome with Lighthouse/chrome-launcher and handoff to Puppeteer
@@ -55,41 +48,26 @@ When using Lighthouse programmatically, you'll often use chrome-launcher to laun
5548
Puppeteer can reconnect to this existing browser instance like so:
5649

5750
```js
58-
const chromeLauncher = require('chrome-launcher');
59-
const puppeteer = require('puppeteer');
60-
const lighthouse = require('lighthouse');
61-
const request = require('request');
62-
const util = require('util');
63-
64-
(async() => {
51+
import chromeLauncher from 'chrome-launcher';
52+
import puppeteer from 'puppeteer';
53+
import lighthouse from 'lighthouse';
54+
import fetch from 'node-fetch';
6555

66-
const URL = 'https://chromestatus.com/features';
67-
68-
const opts = {
69-
//chromeFlags: ['--headless'],
70-
logLevel: 'info',
71-
output: 'json'
72-
};
56+
const url = 'https://chromestatus.com/features';
7357

7458
// Launch chrome using chrome-launcher.
75-
const chrome = await chromeLauncher.launch(opts);
76-
opts.port = chrome.port;
59+
const chrome = await chromeLauncher.launch();
7760

7861
// Connect to it using puppeteer.connect().
79-
const resp = await util.promisify(request)(`http://localhost:${opts.port}/json/version`);
80-
const {webSocketDebuggerUrl} = JSON.parse(resp.body);
62+
const resp = await fetch(`http://localhost:${chrome.port}/json/version`);
63+
const {webSocketDebuggerUrl} = await resp.json();
8164
const browser = await puppeteer.connect({browserWSEndpoint: webSocketDebuggerUrl});
65+
const page = await browser.newPage();
8266

8367
// Run Lighthouse.
84-
const {lhr} = await lighthouse(URL, opts, null);
68+
const {lhr} = await lighthouse(url, undefined, undefined, page);
8569
console.log(`Lighthouse scores: ${Object.values(lhr.categories).map(c => c.score).join(', ')}`);
8670

8771
await browser.disconnect();
8872
await chrome.kill();
89-
90-
})();
9173
```
92-
93-
--------------
94-
95-
**Note**: https://github.com/GoogleChrome/lighthouse/issues/3837 tracks the overall discussion for making Lighthouse work in concert with Puppeteer. Some things, like A/B testing the perf of UI changes, are tricky or not yet possible.

0 commit comments

Comments
 (0)