playwright/docs/out/verification.md

211 lines
7.4 KiB
Markdown
Raw Normal View History

2021-01-01 15:17:27 -08:00
---
id: verification
title: "Verification"
---
- [Videos](#videos)
- [Screenshots](#screenshots)
- [Console logs](#console-logs)
- [Page errors](#page-errors)
2020-04-20 17:17:10 -07:00
- [Page events](#page-events)
2020-04-20 16:56:46 -07:00
## Videos
2021-01-01 15:17:27 -08:00
Playwright can record videos for all pages in a [browser context](core-concepts.md#browser-contexts). Videos are saved upon context closure, so make sure to await `browserContext.close()`.
```js
// With browser.newContext()
const context = await browser.newContext({ recordVideo: { dir: 'videos/' } });
// Make sure to await close, so that videos are saved.
await context.close();
// With browser.newPage()
const page = await browser.newPage({ recordVideo: { dir: 'videos/' } });
// Make sure to await close, so that videos are saved.
await page.close();
// [Optional] Specify video size; defaults to viewport size
const context = await browser.newContext({
recordVideo: {
dir: 'videos/',
size: { width: 800, height: 600 },
}
});
```
#### API reference
- [BrowserContext]
2021-01-01 15:17:27 -08:00
- [browser.newContext([options])](api/class-browser.md#browsernewcontextoptions)
- [browser.newPage([options])](api/class-browser.md#browsernewpageoptions)
- [browserContext.close()](api/class-browsercontext.md#browsercontextclose)
## Screenshots
2020-04-20 16:56:46 -07:00
```js
// Save to file
await page.screenshot({ path: 'screenshot.png' });
2020-04-20 17:17:10 -07:00
// Capture full page
await page.screenshot({ path: 'screenshot.png', fullPage: true });
2020-04-20 17:17:10 -07:00
// Capture into buffer
2020-04-20 17:17:10 -07:00
const buffer = await page.screenshot();
console.log(buffer.toString('base64'));
2020-04-20 16:56:46 -07:00
// Capture given element
const elementHandle = await page.$('.header');
await elementHandle.screenshot({ path: 'screenshot.png' });
```
2020-04-20 16:56:46 -07:00
#### API reference
2021-01-01 15:17:27 -08:00
- [page.screenshot([options])](api/class-page.md#pagescreenshotoptions)
- [elementHandle.screenshot([options])](api/class-elementhandle.md#elementhandlescreenshotoptions)
2020-04-20 16:56:46 -07:00
<br/>
## Console logs
2020-04-20 16:56:46 -07:00
Console messages logged in the page can be brought into the Node.js context.
2020-04-20 16:56:46 -07:00
```js
// Listen for all console logs
page.on('console', msg => console.log(msg.text()))
// Listen for all console events and handle errors
2020-04-20 17:17:10 -07:00
page.on('console', msg => {
if (msg.type() === 'error')
console.log(`Error text: "${msg.text()}"`);
2020-04-20 17:17:10 -07:00
});
// Get the next console log
const [msg] = await Promise.all([
page.waitForEvent('console'),
// Issue console.log inside the page
page.evaluate(() => {
console.log('hello', 42, { foo: 'bar' });
}),
]);
// Deconstruct console log arguments
await msg.args[0].jsonValue() // hello
await msg.args[1].jsonValue() // 42
2020-04-20 16:56:46 -07:00
```
#### API reference
- [ConsoleMessage]
- [Page]
2021-01-01 15:17:27 -08:00
- [page.on('console')](api/class-page.md#pageonconsole)
2020-04-20 16:56:46 -07:00
<br/>
## Page errors
2020-04-20 16:56:46 -07:00
Listen for uncaught exceptions in the page with the `pagerror` event.
2020-04-20 17:17:10 -07:00
2020-04-20 16:56:46 -07:00
```js
2020-04-20 17:17:10 -07:00
// 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>');
2020-04-20 16:56:46 -07:00
```
#### API reference
- [Page]
2021-01-01 15:17:27 -08:00
- [page.on('pageerror')](api/class-page.md#pageonpageerror)
2020-04-20 16:56:46 -07:00
<br/>
## Page events
#### `"requestfailed"`
```js
page.on('requestfailed', request => {
console.log(request.url() + ' ' + request.failure().errorText);
});
```
#### `"dialog"` - handle alert, confirm, prompt
```js
page.on('dialog', dialog => {
dialog.accept();
});
```
#### `"popup"` - handle popup windows
```js
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.click('#open')
]);
```
#### API reference
- [Page]
2021-01-01 15:17:27 -08:00
- [page.on('requestfailed')](api/class-page.md#pageonrequestfailed)
- [page.on('dialog')](api/class-page.md#pageondialog)
- [page.on('popup')](api/class-page.md#pageonpopup)
2021-01-01 15:17:27 -08:00
[Playwright]: api/class-playwright.md "Playwright"
[Browser]: api/class-browser.md "Browser"
[BrowserContext]: api/class-browsercontext.md "BrowserContext"
[Page]: api/class-page.md "Page"
[Frame]: api/class-frame.md "Frame"
[ElementHandle]: api/class-elementhandle.md "ElementHandle"
[JSHandle]: api/class-jshandle.md "JSHandle"
[ConsoleMessage]: api/class-consolemessage.md "ConsoleMessage"
[Dialog]: api/class-dialog.md "Dialog"
[Download]: api/class-download.md "Download"
[Video]: api/class-video.md "Video"
[FileChooser]: api/class-filechooser.md "FileChooser"
[Keyboard]: api/class-keyboard.md "Keyboard"
[Mouse]: api/class-mouse.md "Mouse"
[Touchscreen]: api/class-touchscreen.md "Touchscreen"
[Request]: api/class-request.md "Request"
[Response]: api/class-response.md "Response"
[Selectors]: api/class-selectors.md "Selectors"
[Route]: api/class-route.md "Route"
[WebSocket]: api/class-websocket.md "WebSocket"
[TimeoutError]: api/class-timeouterror.md "TimeoutError"
[Accessibility]: api/class-accessibility.md "Accessibility"
[Worker]: api/class-worker.md "Worker"
[BrowserServer]: api/class-browserserver.md "BrowserServer"
[BrowserType]: api/class-browsertype.md "BrowserType"
[Logger]: api/class-logger.md "Logger"
[ChromiumBrowser]: api/class-chromiumbrowser.md "ChromiumBrowser"
[ChromiumBrowserContext]: api/class-chromiumbrowsercontext.md "ChromiumBrowserContext"
[ChromiumCoverage]: api/class-chromiumcoverage.md "ChromiumCoverage"
[CDPSession]: api/class-cdpsession.md "CDPSession"
[FirefoxBrowser]: api/class-firefoxbrowser.md "FirefoxBrowser"
[WebKitBrowser]: api/class-webkitbrowser.md "WebKitBrowser"
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array"
[Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer "Buffer"
[ChildProcess]: https://nodejs.org/api/child_process.html "ChildProcess"
[Element]: https://developer.mozilla.org/en-US/docs/Web/API/element "Element"
[Error]: https://nodejs.org/api/errors.html#errors_class_error "Error"
2021-01-01 15:17:27 -08:00
[Evaluation Argument]: ./core-concepts.md#evaluationargument "Evaluation Argument"
[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map "Map"
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object"
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"
[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp "RegExp"
[Serializable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable"
[UIEvent.detail]: https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail "UIEvent.detail"
[URL]: https://nodejs.org/api/url.html "URL"
[USKeyboardLayout]: ../src/usKeyboardLayout.ts "USKeyboardLayout"
[UnixTime]: https://en.wikipedia.org/wiki/Unix_time "Unix Time"
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean"
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function"
[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator"
[null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null "null"
[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number"
[origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin "Origin"
[selector]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors "selector"
[Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "Readable"
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string"
[xpath]: https://developer.mozilla.org/en-US/docs/Web/XPath "xpath"