-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(puppeteer.md): examples of combining the two #4408
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,101 @@ | ||
# Recipes Puppeteer with Lighthouse | ||
|
||
**Note**: https://github.com/GoogleChrome/lighthouse/issues/3837 tracks the discussion for making Lighthouse work in concert with Puppeteer. | ||
Some things are possible today (login to a page using Puppeteer, audit it using Lighthouse) while others (A/B testing the perf of UI changes) are not. | ||
|
||
### Custom network throttle settings using Puppeteer | ||
|
||
By default, Lighthouse uses a mobile 3G connecton for it's network throttlings. It's possible to use Puppeter | ||
to launch Chrome and customize these settings. | ||
|
||
Flow: | ||
1. disable the throttling settings in Lighthouse. | ||
2. Launch Chrome using Puppeteer and tell Lighthouse to reuse Puppeteer's chrome instance. | ||
3. Tell lighthouse to programmatically load the page. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'Lighthouse' rather than 'lighthouse' I think... |
||
4. Watch for the page to open and set the emulation conditions: | ||
|
||
```js | ||
const puppeteer = require('puppeteer'); | ||
const lighthouse = require('lighthouse');; | ||
const {URL} = require('url'); | ||
|
||
(async() => { | ||
const url = 'https://www.chromestatus.com/features'; | ||
const networkConditions = { | ||
offline: false, | ||
latency: 800, | ||
downloadThroughput: Math.floor(1.6 * 1024 * 1024 / 8), // 1.6Mbps | ||
uploadThroughput: Math.floor(750 * 1024 / 8) // 750Kbps | ||
}; | ||
|
||
// Use Puppeteer to launch headless Chrome. | ||
const browser = await puppeteer.launch({headless: true}); | ||
|
||
// Wait for Lighthouse to open url, then customize network conditions. | ||
// Note: this will re-establish these conditions when LH reloads the page. Think that's ok.... | ||
browser.on('targetchanged', async target => { | ||
const page = await target.page(); | ||
|
||
if (page && page.url() === url) { | ||
const client = await page.target().createCDPSession(); | ||
await client.send('Network.emulateNetworkConditions', networkConditions); | ||
} | ||
}); | ||
|
||
// Lighthouse will open URL. Puppeteer observes `targetchanged` and sets up network conditions. | ||
// Possible race condition. | ||
const lhr = await lighthouse(url, { | ||
port: (new URL(browser.wsEndpoint())).port, | ||
output: 'json', | ||
logLevel: 'info', | ||
disableNetworkThrottling: true, | ||
// disableCpuThrottling: true, | ||
disableDeviceEmulation: true, | ||
}); | ||
|
||
console.log(`Lighthouse score: ${lhr.score}`); | ||
|
||
await browser.close(); | ||
})(); | ||
``` | ||
|
||
### Connecting Puppeteer to a browser instance launched by chrome-launcher. | ||
|
||
When using Lighthouse programmatically, you'll often use chrome-launcher to launch Chrome. | ||
Puppeteer can reconnect to this existing browser instance like so: | ||
|
||
```js | ||
onst chromeLauncher = require('chrome-launcher'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Const typo |
||
const puppeteer = require('puppeteer'); | ||
const lighthouse = require('lighthouse'); | ||
const request = require('request'); | ||
const util = require('util'); | ||
|
||
(async() => { | ||
|
||
const URL = 'https://www.chromestatus.com/features'; | ||
|
||
const opts = { | ||
//chromeFlags: ['--headless'], | ||
logLevel: 'info', | ||
output: 'json' | ||
}; | ||
|
||
// Launch chrome using chrome-launcher. | ||
const chrome = await chromeLauncher.launch(opts); | ||
opts.port = chrome.port; | ||
|
||
// Connect to it using puppeteer.connect(). | ||
const resp = await util.promisify(request)(`http://localhost:${opts.port}/json/version`); | ||
const {webSocketDebuggerUrl} = JSON.parse(resp.body); | ||
const browser = await puppeteer.connect({browserWSEndpoint: webSocketDebuggerUrl}); | ||
|
||
// Run Lighthouse. | ||
const lhr = await lighthouse(URL, opts, null); | ||
console.log(`Lighthouse score: ${lhr.score}`); | ||
|
||
await browser.disconnect(); | ||
await chrome.kill(); | ||
|
||
})(); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we advertise a different example of using puppeteer? Perhaps just logging in to your page with the second example you've already got?
Our preferred method of customizing throttling is already documented and jumping in to muck with those settings in puppeteer is bound to result in over confidence in the accuracy of the performance metrics for those conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea that's fair. I can cook up a slightly different example of doing something in pptr then use lighthouse.