Skip to content

Instantly share code, notes, and snippets.

@asamountain
Created January 6, 2020 04:19
Show Gist options
  • Save asamountain/1ad31699defe60ce67c1ba9f14aa361d to your computer and use it in GitHub Desktop.
Save asamountain/1ad31699defe60ce67c1ba9f14aa361d to your computer and use it in GitHub Desktop.
get matric time for debugging in puppeteer
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Emitted when the DOM is parsed and ready (without waiting for resources)
page.once('domcontentloaded', () => console.info('βœ… DOM is ready'));
// Emitted when the page is fully loaded
page.once('load', () => console.info('βœ… Page is loaded'));
// Emitted when the page attaches a frame
page.on('frameattached', () => console.info('βœ… Frame is attached'));
// Emitted when a frame within the page is navigated to a new URL
page.on('framenavigated', () => console.info('πŸ‘‰ Frame is navigated'));
// Emitted when a script within the page uses `console.timeStamp`
page.on('metrics', data => console.info(`πŸ‘‰ Timestamp added at ${data.metrics.Timestamp}`));
// Emitted when a script within the page uses `console`
page.on('console', message => console[message.type()](`πŸ‘‰ ${message.text()}`));
// Emitted when the page emits an error event (for example, the page crashes)
page.on('error', error => console.error(`❌ ${error}`));
// Emitted when a script within the page has uncaught exception
page.on('pageerror', error => console.error(`❌ ${error}`));
// Emitted when a script within the page uses `alert`, `prompt`, `confirm` or `beforeunload`
page.on('dialog', async dialog => {
console.info(`πŸ‘‰ ${dialog.message()}`);
await dialog.dismiss();
});
// Emitted when a new page, that belongs to the browser context, is opened
page.on('popup', () => console.info('πŸ‘‰ New page is opened'));
// Emitted when the page produces a request
page.on('request', request => console.info(`πŸ‘‰ Request: ${request.url()}`));
// Emitted when a request, which is produced by the page, fails
page.on('requestfailed', request => console.info(`❌ Failed request: ${request.url()}`));
// Emitted when a request, which is produced by the page, finishes successfully
page.on('requestfinished', request => console.info(`πŸ‘‰ Finished request: ${request.url()}`));
// Emitted when a response is received
page.on('response', response => console.info(`πŸ‘‰ Response: ${response.url()}`));
// Emitted when the page creates a dedicated WebWorker
page.on('workercreated', worker => console.info(`πŸ‘‰ Worker: ${worker.url()}`));
// Emitted when the page destroys a dedicated WebWorker
page.on('workerdestroyed', worker => console.info(`πŸ‘‰ Destroyed worker: ${worker.url()}`));
// Emitted when the page detaches a frame
page.on('framedetached', () => console.info('βœ… Frame is detached'));
// Emitted after the page is closed
page.once('close', () => console.info('βœ… Page is closed'));
await page.goto('https://pptr.dev');
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment