Created
May 1, 2020 18:16
-
-
Save weivall/d2e697dcbfc363cd7ee1769efde0b04b to your computer and use it in GitHub Desktop.
HAR in puppeteer
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
const fs = require('fs'); | |
const { promisify } = require('util'); | |
const puppeteer = require('puppeteer'); | |
const { harFromMessages } = require('chrome-har'); | |
// list of events for converting to HAR | |
const events = []; | |
// event types to observe | |
const observe = [ | |
'Page.loadEventFired', | |
'Page.domContentEventFired', | |
'Page.frameStartedLoading', | |
'Page.frameAttached', | |
'Network.requestWillBeSent', | |
'Network.requestServedFromCache', | |
'Network.dataReceived', | |
'Network.responseReceived', | |
'Network.resourceChangedPriority', | |
'Network.loadingFinished', | |
'Network.loadingFailed', | |
]; | |
(async () => { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
// register events listeners | |
const client = await page.target().createCDPSession(); | |
await client.send('Page.enable'); | |
await client.send('Network.enable'); | |
observe.forEach(method => { | |
client.on(method, params => { | |
events.push({ method, params }); | |
}); | |
}); | |
// perform tests | |
await page.goto('https://en.wikipedia.org'); | |
page.click('#n-help > a'); | |
await page.waitForNavigation({ waitUntil: 'networkidle2' }); | |
await browser.close(); | |
// convert events to HAR file | |
const har = harFromMessages(events); | |
await promisify(fs.writeFile)('en.wikipedia.org.har', JSON.stringify(har)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://michaljanaszek.com/blog/generate-har-with-puppeteer/