diff --git a/docs/api.md b/docs/api.md index eccc2e85f2e..46f9718305e 100644 --- a/docs/api.md +++ b/docs/api.md @@ -13,7 +13,7 @@ * [browser.stdout](#browserstdout) * [browser.version()](#browserversion) - [class: Page](#class-page) - * [event: 'consolemessage'](#event-consolemessage) + * [event: 'console'](#event-console) * [event: 'dialog'](#event-dialog) * [event: 'frameattached'](#event-frameattached) * [event: 'framedetached'](#event-framedetached) @@ -205,10 +205,21 @@ browser.newPage().then(async page => }); ``` -#### event: 'consolemessage' +#### event: 'console' - <[string]> -Emitted when a page calls one of console API methods, e.g. `console.log`. +Emitted when a page calls one of console API methods, e.g. `console.log` or `console.dir`. + +If multiple arguments are passed over to the console API call, these arguments are dispatched in an event. + +An example of handling `console` event: +```js +page.on('console', (...args) => { + for (let i =0; i < args.length; ++i) + console.log(`${i}: ${args[i]}`); +}); +page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})); +``` #### event: 'dialog' - <[Dialog]> diff --git a/examples/features.js b/examples/features.js index 6c28f811560..035dfc23453 100644 --- a/examples/features.js +++ b/examples/features.js @@ -21,7 +21,7 @@ var browser = new Browser(); browser.newPage().then(async page => { var modernizrPath = path.join(__dirname, '../third_party/phantomjs/examples/modernizr.js'); await page.injectFile(modernizrPath); - page.on('consolemessage', console.log); + page.on('console', console.log); await page.evaluate(detectFeatures); browser.close(); }); diff --git a/examples/pagecallback.js b/examples/pagecallback.js index 15daa316f5b..b4df51ed0a1 100644 --- a/examples/pagecallback.js +++ b/examples/pagecallback.js @@ -18,7 +18,7 @@ var Browser = require('../lib/Browser'); var browser = new Browser(); browser.newPage().then(async page => { - page.on('consolemessage', console.log); + page.on('console', console.log); await page.setInPageCallback('callPhantom', msg => { diff --git a/lib/Page.js b/lib/Page.js index b42953dcb07..766520307ed 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -224,7 +224,7 @@ class Page extends EventEmitter { return; } let values = await Promise.all(event.args.map(arg => helper.serializeRemoteObject(this._client, arg))); - this.emit(Page.Events.ConsoleMessage, ...values); + this.emit(Page.Events.Console, ...values); } _onDialog(event) { @@ -668,7 +668,7 @@ function convertPrintParameterToInches(parameter) { } Page.Events = { - ConsoleMessage: 'consolemessage', + Console: 'console', Dialog: 'dialog', // Can'e use just 'error' due to node.js special treatment of error events. // @see https://nodejs.org/api/events.html#events_error_events diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index def5ba168e5..4fcd28cfa6e 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -62,7 +62,7 @@ class WebPage { this._pageEvents.on(PageEvents.Response, response => this._onResponseReceived(response)); this._pageEvents.on(PageEvents.RequestFinished, request => this._onRequestFinished(request)); this._pageEvents.on(PageEvents.RequestFailed, event => (this.onResourceError || noop).call(null, event)); - this._pageEvents.on(PageEvents.ConsoleMessage, (...args) => this._onConsoleMessage(...args)); + this._pageEvents.on(PageEvents.Console, (...args) => this._onConsole(...args)); this._pageEvents.on(PageEvents.Confirm, message => this._onConfirm(message)); this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message)); this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog)); @@ -84,7 +84,7 @@ class WebPage { /** * @param {!Array} args */ - _onConsoleMessage(...args) { + _onConsole(...args) { if (!this.onConsoleMessage) return; const text = args.join(' '); diff --git a/test/test.js b/test/test.js index b4b3dc3e33d..1d23274af02 100644 --- a/test/test.js +++ b/test/test.js @@ -226,17 +226,17 @@ describe('Puppeteer', function() { })); }); - describe('Page.Events.ConsoleMessage', function() { + describe('Page.Events.Console', function() { it('should work', SX(async function() { let commandArgs = []; - page.once('consolemessage', (...args) => commandArgs = args); + page.once('console', (...args) => commandArgs = args); page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})); - await waitForEvents(page, 'consolemessage'); + await waitForEvents(page, 'console'); expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]); })); it('should work for different console API calls', SX(async function() { let messages = []; - page.on('consolemessage', msg => messages.push(msg)); + page.on('console', msg => messages.push(msg)); page.evaluate(() => { // A pair of time/timeEnd generates only one Console API call. console.time('calling console.time'); @@ -247,7 +247,7 @@ describe('Puppeteer', function() { console.error('calling console.error'); }); // Wait for 5 events to hit. - await waitForEvents(page, 'consolemessage', 5); + await waitForEvents(page, 'console', 5); expect(messages[0]).toContain('calling console.time'); expect(messages.slice(1)).toEqual([ 'calling console.trace',