Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs: improve snippets for console logs (#2684)

Arjun Attam4af8c6831e5c
diff --git a/docs/verification.md b/docs/verification.md
index f03b2de1ee..d772c9fda3 100644
--- a/docs/verification.md
+++ b/docs/verification.md
@@ -36,17 +36,30 @@ await elementHandle.screenshot({ path: 'screenshot.png' });
## 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
+Console messages logged in the page can be brought into the Node.js context.
```js
+// Listen for all console logs
+page.on('console', msg => console.log(msg.text()))
+
+// Listen for all console events and handle errors
page.on('console', msg => {
- // Handle only errors.
- if (msg.type() !== 'error')
- return;
- console.log(`text: "${msg.text()}"`);
+ if (msg.type() === 'error')
+ console.log(`Error text: "${msg.text()}"`);
});
+
+// 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
```
#### API reference