@@ -14,39 +14,32 @@ The example below shows how to inject CSS into the page before Lighthouse audits
14
14
A similar approach can be taken for injecting JavaScript.
15
15
16
16
``` 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' ;
20
19
21
- (async () => {
22
20
const url = ' https://chromestatus.com/features' ;
23
21
24
22
// Use Puppeteer to launch headful Chrome and don't use its default 800x600 viewport.
25
23
const browser = await puppeteer .launch ({
26
24
headless: false ,
27
25
defaultViewport: null ,
28
26
});
27
+ const page = await browser .newPage ();
29
28
30
29
// Wait for Lighthouse to open url, then inject our stylesheet.
31
30
browser .on (' targetchanged' , async target => {
32
- const page = await target .page ();
33
31
if (page && page .url () === url) {
34
32
await page .addStyleTag ({content: ' * {color: red}' });
35
33
}
36
34
});
37
35
38
36
// Lighthouse will open the URL.
39
37
// 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);
45
39
46
40
console .log (` Lighthouse scores: ${ Object .values (lhr .categories ).map (c => c .score ).join (' , ' )} ` );
47
41
48
42
await browser .close ();
49
- })();
50
43
```
51
44
52
45
### 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
55
48
Puppeteer can reconnect to this existing browser instance like so:
56
49
57
50
``` 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' ;
65
55
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' ;
73
57
74
58
// Launch chrome using chrome-launcher.
75
- const chrome = await chromeLauncher .launch (opts);
76
- opts .port = chrome .port ;
59
+ const chrome = await chromeLauncher .launch ();
77
60
78
61
// 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 ( );
81
64
const browser = await puppeteer .connect ({browserWSEndpoint: webSocketDebuggerUrl});
65
+ const page = await browser .newPage ();
82
66
83
67
// Run Lighthouse.
84
- const {lhr } = await lighthouse (URL , opts, null );
68
+ const {lhr } = await lighthouse (url, undefined , undefined , page );
85
69
console .log (` Lighthouse scores: ${ Object .values (lhr .categories ).map (c => c .score ).join (' , ' )} ` );
86
70
87
71
await browser .disconnect ();
88
72
await chrome .kill ();
89
-
90
- })();
91
73
```
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