Skip to content

Instantly share code, notes, and snippets.

@ericmustin
Last active August 14, 2020 12:56
Show Gist options
  • Save ericmustin/e9c07517e8eed00521ceb9b16782136d to your computer and use it in GitHub Desktop.
Save ericmustin/e9c07517e8eed00521ceb9b16782136d to your computer and use it in GitHub Desktop.
puppeteer har file generator with custom responses

Basic Usage

  • npm install
  • node index.js
  • Then, to see results
    • cat results.har | jq '.log.entries | map(.response)'
const puppeteer = require('puppeteer');
const PuppeteerHar = require('puppeteer-har');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const har = new PuppeteerHar(page);
await har.start({ path: 'results.har', saveResponse: true });
await page.setRequestInterception(true);
page.on('request', request => {
request.respond({
status: 200,
contentType: 'text/html',
body: '<!DOCTYPE html><html><head></head><body><div>Hello World</div></body></html>',
headers: {
'Cache-Control': 'max-age=3600'
}
});
});
await page.goto('http://localhost:3000');
await har.stop();
await browser.close();
})();
{
"name": "rum_integration_tests",
"version": "1.0.0",
"description": "generate HAR files based on different request / response headers",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Eric Mustin",
"license": "ISC",
"dependencies": {
"puppeteer": "^5.2.1",
"puppeteer-har": "jonapgar/puppeteer-har#forked"
}
}
{
"log": {
"version": "1.2",
"creator": {
"name": "chrome-har",
"version": "0.11.9",
"comment": "https://github.com/sitespeedio/chrome-har"
},
"pages": [
{
"id": "page_1",
"startedDateTime": "2020-08-14T12:43:37.166Z",
"title": "http://localhost:3000/",
"pageTimings": {}
}
],
"entries": [
{
"cache": {},
"startedDateTime": "2020-08-14T12:43:37.167Z",
"_requestId": "3022B0A83206C0D030568AFA2301B178",
"_initialPriority": "VeryHigh",
"_priority": "VeryHigh",
"pageref": "page_1",
"request": {
"method": "GET",
"url": "http://localhost:3000/",
"queryString": [],
"headersSize": 264,
"bodySize": 0,
"cookies": [],
"headers": [
{
"name": "Upgrade-Insecure-Requests",
"value": "1"
},
{
"name": "User-Agent",
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/85.0.4182.0 Safari/537.36"
},
{
"name": "cache-control",
"value": "max-age=3600"
},
{
"name": "content-type",
"value": "text/html"
}
],
"httpVersion": "http/1.1"
},
"time": 5.824,
"_initiator_detail": "{\"type\":\"other\"}",
"_initiator_type": "other",
"response": {
"httpVersion": "http/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK",
"content": {
"mimeType": "text/html",
"size": 76,
"text": "<!DOCTYPE html><html><head></head><body><div>Hello World</div></body></html>",
"compression": 8
},
"headersSize": 93,
"bodySize": 68,
"cookies": [],
"headers": [
{
"name": "cache-control",
"value": "max-age=3600"
},
{
"name": "content-type",
"value": "text/html"
},
{
"name": "content-length",
"value": "76"
}
],
"_transferSize": 161
},
"connection": "0",
"serverIPAddress": "",
"timings": {
"blocked": -1,
"dns": -1,
"connect": -1,
"send": 0,
"wait": 5.821,
"receive": 0.003,
"ssl": -1,
"_queued": 0.785
},
"_requestTime": 27116.811632
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment