playwright/docs/verification.md
Arjun Attam 6cec2dfb7c
docs: add assertions doc (#2585)
* docs: add assertions doc

* docs: assertions and verification split
2020-06-22 16:53:56 -07:00

2.3 KiB

Verification


Screenshots

// Save to file
await page.screenshot({path: 'screenshot.png'});

// Capture full page
await page.screenshot({path: 'screenshot.png', fullPage: true});

// Capture into buffer
const buffer = await page.screenshot();
console.log(buffer.toString('base64'));

// Capture given element
const elementHandle = await page.$('.header');
await elementHandle.screenshot({ path: 'screenshot.png' });

API reference


Console logs

You can listen for various events on the page object. Following are just some of the examples of the events you can assert and handle:

"console" - get all console messages from the page

page.on('console', msg => {
  // Handle only errors.
  if (msg.type() !== 'error')
    return;
  console.log(`text: "${msg.text()}"`);
});

API reference


Page errors

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

// Log all uncaught errors to the terminal
page.on('pageerror', exception => {
  console.log(`Uncaught exception: "${exception}"`);
});

// Navigate to a page with an exception.
await page.goto('data:text/html,<script>throw new Error("Test")</script>');

API reference


Page events

"requestfailed"

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

"dialog" - handle alert, confirm, prompt

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

"popup" - handle popup windows

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

API reference