Skip to content
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

Wait for a Page event before collecting events #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions example/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const puppeteer = require('puppeteer');
const PuppeteerHar = require('../');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();

const har = new PuppeteerHar(page);
await har.start();

await page.goto('http://www.yahoo.com');

const harResult = await har.stop();
console.log(JSON.stringify(harResult, null, 2));

await browser.close();
})();
25 changes: 20 additions & 5 deletions lib/PuppeteerHar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ const { harFromMessages } = require('chrome-har');
const observe = [
'Page.loadEventFired',
'Page.domContentEventFired',
'Page.frameStartedLoading',
'Page.frameAttached',
'Page.frameStartedLoading',
'Page.frameScheduledNavigation',
'Network.requestWillBeSent',
'Network.requestServedFromCache',
'Network.dataReceived',
'Network.responseReceived',
'Network.resourceChangedPriority',
'Network.dataReceived',
'Network.loadingFinished',
'Network.loadingFailed',
'Network.resourceChangedPriority'
];

class PuppeteerHar {
Expand All @@ -24,7 +25,6 @@ class PuppeteerHar {
*/
constructor(page) {
this.page = page;
this.mainFrame = this.page.mainFrame();
this.events = [];
}

Expand All @@ -37,9 +37,24 @@ class PuppeteerHar {
this.client = await this.page.target().createCDPSession();
await this.client.send('Page.enable');
await this.client.send('Network.enable');
let haveSeenPageEvent = false;
let holdUntilPageEvent = [];
observe.forEach(async method => {
await this.client.on(method, params => {
this.events.push({ method, params });
// these are the methods that chrome-har adds a page entry for
if (method === 'Page.frameStartedLoading' || method === 'Page.frameScheduledNavigation') {
haveSeenPageEvent = true;
}

if (!haveSeenPageEvent) {
holdUntilPageEvent.push({ method, params })
} else {
this.events.push({ method, params });
if (holdUntilPageEvent.length > 0) {
this.events = this.events.concat(holdUntilPageEvent);
holdUntilPageEvent = []
}
}
});
});
}
Expand Down
Loading