Verification

Screenshots

  1. // Save to file
  2. await page.screenshot({path: 'screenshot.png'});
  3. // Capture full page
  4. await page.screenshot({path: 'screenshot.png', fullPage: true});
  5. // Capture into buffer
  6. const buffer = await page.screenshot();
  7. console.log(buffer.toString('base64'));
  8. // Capture given element
  9. const elementHandle = await page.$('.header');
  10. await elementHandle.screenshot({ path: 'screenshot.png' });

API reference

Console logs

Console messages logged in the page can be brought into the Node.js context.

  1. // Listen for all console logs
  2. page.on('console', msg => console.log(msg.text()))
  3. // Listen for all console events and handle errors
  4. page.on('console', msg => {
  5. if (msg.type() === 'error')
  6. console.log(`Error text: "${msg.text()}"`);
  7. });
  8. // Get the next console log
  9. const [msg] = await Promise.all([
  10. page.waitForEvent('console'),
  11. // Issue console.log inside the page
  12. page.evaluate(() => {
  13. console.log('hello', 42, {foo: 'bar'});
  14. }),
  15. ]);
  16. // Deconstruct console log arguments
  17. await msg.args[0].jsonValue() // hello
  18. await msg.args[1].jsonValue() // 42

API reference

Page errors

Listen for uncaught exceptions in the page with the pagerror event.

  1. // Log all uncaught errors to the terminal
  2. page.on('pageerror', exception => {
  3. console.log(`Uncaught exception: "${exception}"`);
  4. });
  5. // Navigate to a page with an exception.
  6. await page.goto('data:text/html,<script>throw new Error("Test")</script>');

API reference

Page events

“requestfailed”

  1. page.on('requestfailed', request => {
  2. console.log(request.url() + ' ' + request.failure().errorText);
  3. });

“dialog” - handle alert, confirm, prompt

  1. page.on('dialog', dialog => {
  2. dialog.accept();
  3. });

“popup” - handle popup windows

  1. const [popup] = await Promise.all([
  2. page.waitForEvent('popup'),
  3. page.click('#open')
  4. ]);

API reference