-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to split har per page (#12)
* add function to split har per page * update readme
- Loading branch information
Showing
8 changed files
with
86 additions
and
31 deletions.
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
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
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
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
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
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,45 @@ | ||
const playwright = require('playwright-aws-lambda'); | ||
const { harFromMessages } = require('chrome-har'); | ||
|
||
module.exports = { | ||
getHarFile: async function (pageUrl) { | ||
// 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', | ||
]; | ||
// Create a URL object for use later | ||
const url = new URL(pageUrl); | ||
|
||
const browser = await playwright.launchChromium(false); | ||
const page = await browser.newPage(); | ||
|
||
// register events listeners | ||
const client = await page.context().newCDPSession(page); | ||
await client.send('Page.enable'); | ||
await client.send('Network.enable'); | ||
observe.forEach((method) => { | ||
client.on(method, (params) => { | ||
events.push({ method, params }); | ||
}); | ||
}); | ||
|
||
await page.goto(url.href); | ||
await browser.close(); | ||
|
||
// convert events to HAR file | ||
return harFromMessages(events); | ||
}, | ||
}; |
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,15 @@ | ||
const { getHarFile } = require('./getHarFile'); | ||
const { writeFileSync } = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports = pageHandler = async (page, count) => { | ||
const result = await getHarFile(page.url()); | ||
if (count) { | ||
writeFileSync( | ||
path.join(__dirname, '..', '..', 'tmp', `${count}.har`), | ||
JSON.stringify(result), | ||
); | ||
} else { | ||
return harObject; | ||
} | ||
}; |
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