Skip to content

Commit

Permalink
feat(Page): report 'Log' domain entries as 'console' events (#2400)
Browse files Browse the repository at this point in the history
Fixes #1939
  • Loading branch information
yanivefraim authored and aslushnikov committed Apr 28, 2018
1 parent 58c4668 commit e236887
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -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));
}

Expand All @@ -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}
*/
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e236887

Please sign in to comment.