Skip to content

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

feat(console): allow page.on('console', console.log) (#2145)

Pavel Feldman7a8dd2c361e9
diff --git a/src/console.ts b/src/console.ts
index a5c660e1a5..0e40887ed4 100644
--- a/src/console.ts
+++ b/src/console.ts
@@ -15,6 +15,7 @@
*/
import * as js from './javascript';
+import * as util from 'util';
export type ConsoleMessageLocation = {
url?: string,
@@ -52,4 +53,8 @@ export class ConsoleMessage {
location(): ConsoleMessageLocation {
return this._location;
}
+
+ [util.inspect.custom]() {
+ return this.text();
+ }
}
diff --git a/test/page.spec.js b/test/page.spec.js
index eff81d10e9..e09259b1f4 100644
--- a/test/page.spec.js
+++ b/test/page.spec.js
@@ -16,6 +16,7 @@
*/
const path = require('path');
+const util = require('util');
const vm = require('vm');
const {FFOX, CHROMIUM, WEBKIT} = require('./utils').testOptions(browserType);
@@ -169,6 +170,15 @@ describe('Page.Events.Console', function() {
await page.evaluate(() => { for (let i = 0; i < 2; ++i ) console.log('hello'); } );
expect(messages).toEqual(['hello', 'hello']);
});
+ it('should use text() for inspection', async({page}) => {
+ let text;
+ const inspect = value => {
+ text = util.inspect(value);
+ }
+ page.on('console', inspect);
+ await page.evaluate(() => console.log('Hello world'));
+ expect(text).toEqual('Hello world');
+ });
it('should work for different console API calls', async({page, server}) => {
const messages = [];
page.on('console', msg => messages.push(msg));