docs: improve snippets for console logs (#2684)
This commit is contained in:
parent
07dbd0baae
commit
4af8c6831e
|
|
@ -36,17 +36,30 @@ await elementHandle.screenshot({ path: 'screenshot.png' });
|
||||||
|
|
||||||
## Console logs
|
## 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 messages logged in the page can be brought into the Node.js context.
|
||||||
|
|
||||||
#### `"console"` - get all console messages from the page
|
|
||||||
|
|
||||||
```js
|
```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 => {
|
page.on('console', msg => {
|
||||||
// Handle only errors.
|
if (msg.type() === 'error')
|
||||||
if (msg.type() !== 'error')
|
console.log(`Error text: "${msg.text()}"`);
|
||||||
return;
|
|
||||||
console.log(`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
|
#### API reference
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue