Skip to content

Commit 7a2f7aa

Browse files
committed
tests: improve puppeteer's console logging
1 parent 5d5caa9 commit 7a2f7aa

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

test/resources/puppeteer.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@ const serverOptions = {root: path};
1414

1515
const server = httpServer.createServer(serverOptions);
1616

17+
// see https://github.com/puppeteer/puppeteer/issues/3397#issuecomment-434970058
18+
async function logMsg(msg) {
19+
// serialize my args the way I want
20+
const args = await Promise.all(msg.args().map(arg => arg.executionContext().evaluate(arg => {
21+
// I'm in a page context now. If my arg is an error - get me its message.
22+
if (arg instanceof Error)
23+
return arg.message;
24+
// return arg right away. since we use `executionContext.evaluate`, it'll return JSON value of
25+
// the argument if possible, or `undefined` if it fails to stringify it.
26+
return arg;
27+
}, arg)));
28+
console.log(...args);
29+
}
30+
1731
server.listen(port, host, function () {
18-
console.log(`Server running at http://${host}:${port}/`);
32+
console.log(`[puppeteer] Server running at http://${host}:${port}/`);
1933

2034
(async () => {
2135
const browser = await puppeteer.launch();
2236
const page = await browser.newPage();
23-
page.on('console', msg => console.log(msg.text()));
24-
console.log("navigating to: ", url);
37+
page.on('console', logMsg);
38+
console.log("[puppeteer] Navigating to: ", url);
2539
await page.goto(url);
2640

2741
const failures = await page.evaluate(() => {

0 commit comments

Comments
 (0)