From e236887bbbd015e46fe036ef1caf957b52d70251 Mon Sep 17 00:00:00 2001 From: Yaniv Efraim Date: Sat, 28 Apr 2018 07:40:09 +0300 Subject: [PATCH] feat(Page): report 'Log' domain entries as 'console' events (#2400) Fixes #1939 --- lib/Page.js | 13 +++++++++++-- test/page.spec.js | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index c9303465..c1cfe935 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -50,6 +50,7 @@ class Page extends EventEmitter { client.send('Runtime.enable', {}), client.send('Security.enable', {}), client.send('Performance.enable', {}), + client.send('Log.enable', {}), ]); if (ignoreHTTPSErrors) await client.send('Security.setOverrideCertificateErrors', {override: true}); @@ -103,7 +104,7 @@ class Page extends EventEmitter { client.on('Security.certificateError', event => this._onCertificateError(event)); client.on('Inspector.targetCrashed', event => this._onTargetCrashed()); client.on('Performance.metrics', event => this._emitMetrics(event)); - + client.on('Log.entryAdded', event => this._onLogEntryAdded(event)); this._target._isClosedPromise.then(() => this.emit(Page.Events.Close)); } @@ -125,6 +126,14 @@ class Page extends EventEmitter { this.emit('error', new Error('Page crashed!')); } + _onLogEntryAdded(event) { + const {level, text, args} = event.entry; + if (args) + args.map(arg => helper.releaseObject(this._client, arg)); + + this.emit(Page.Events.Console, new ConsoleMessage(level, text)); + } + /** * @return {!Puppeteer.Frame} */ @@ -1048,7 +1057,7 @@ class ConsoleMessage { * @param {string} text * @param {!Array<*>} args */ - constructor(type, text, args) { + constructor(type, text, args = []) { this._type = type; this._text = text; this._args = args; diff --git a/test/page.spec.js b/test/page.spec.js index aff7215b..efabb2c8 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -331,6 +331,15 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip ]); expect(message.text()).toBe('JSHandle@object'); }); + it('should trigger correct Log', async({page, server}) => { + await page.goto('about:blank'); + let message; + page.on('console', event => message = event); + page.evaluate(async url => fetch(url).catch(e => {}), server.EMPTY_PAGE); + await waitEvent(page, 'console'); + expect(message.text()).toContain('No \'Access-Control-Allow-Origin\''); + expect(message.type()).toEqual('error'); + }); }); describe('Page.Events.DOMContentLoaded', function() {