mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs: update console event snippet (#9800)
This avoids the confusion where arguments are printed asynchronously, interleaved with other console messages.
This commit is contained in:
parent
22e4a0d580
commit
fac5da9517
@ -169,8 +169,10 @@ An example of handling `console` event:
|
||||
|
||||
```js
|
||||
page.on('console', async msg => {
|
||||
for (let i = 0; i < msg.args().length; ++i)
|
||||
console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
|
||||
const values = [];
|
||||
for (const arg of msg.args())
|
||||
values.push(await arg.jsonValue());
|
||||
console.log(...values);
|
||||
});
|
||||
await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
|
||||
```
|
||||
@ -185,8 +187,10 @@ page.evaluate("() => console.log('hello', 5, {foo: 'bar'})");
|
||||
|
||||
```python async
|
||||
async def print_args(msg):
|
||||
values = []
|
||||
for arg in msg.args:
|
||||
print(await arg.json_value())
|
||||
values.append(await arg.json_value())
|
||||
print(values)
|
||||
|
||||
page.on("console", print_args)
|
||||
await page.evaluate("console.log('hello', 5, {foo: 'bar'})")
|
||||
|
||||
18
packages/playwright-core/types/types.d.ts
vendored
18
packages/playwright-core/types/types.d.ts
vendored
@ -794,8 +794,10 @@ export interface Page {
|
||||
*
|
||||
* ```js
|
||||
* page.on('console', async msg => {
|
||||
* for (let i = 0; i < msg.args().length; ++i)
|
||||
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
|
||||
* const values = [];
|
||||
* for (const arg of msg.args())
|
||||
* values.push(await arg.jsonValue());
|
||||
* console.log(...values);
|
||||
* });
|
||||
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
|
||||
* ```
|
||||
@ -1065,8 +1067,10 @@ export interface Page {
|
||||
*
|
||||
* ```js
|
||||
* page.on('console', async msg => {
|
||||
* for (let i = 0; i < msg.args().length; ++i)
|
||||
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
|
||||
* const values = [];
|
||||
* for (const arg of msg.args())
|
||||
* values.push(await arg.jsonValue());
|
||||
* console.log(...values);
|
||||
* });
|
||||
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
|
||||
* ```
|
||||
@ -3394,8 +3398,10 @@ export interface Page {
|
||||
*
|
||||
* ```js
|
||||
* page.on('console', async msg => {
|
||||
* for (let i = 0; i < msg.args().length; ++i)
|
||||
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
|
||||
* const values = [];
|
||||
* for (const arg of msg.args())
|
||||
* values.push(await arg.jsonValue());
|
||||
* console.log(...values);
|
||||
* });
|
||||
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
|
||||
* ```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user