mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Rename Page's 'consolemessage' event into 'console'
This patch: - renames 'consolemessage' event into 'console' - improves on 'console' event documentation References #39.
This commit is contained in:
parent
1a97d8b3c2
commit
2ca08b032b
17
docs/api.md
17
docs/api.md
@ -13,7 +13,7 @@
|
|||||||
* [browser.stdout](#browserstdout)
|
* [browser.stdout](#browserstdout)
|
||||||
* [browser.version()](#browserversion)
|
* [browser.version()](#browserversion)
|
||||||
- [class: Page](#class-page)
|
- [class: Page](#class-page)
|
||||||
* [event: 'consolemessage'](#event-consolemessage)
|
* [event: 'console'](#event-console)
|
||||||
* [event: 'dialog'](#event-dialog)
|
* [event: 'dialog'](#event-dialog)
|
||||||
* [event: 'frameattached'](#event-frameattached)
|
* [event: 'frameattached'](#event-frameattached)
|
||||||
* [event: 'framedetached'](#event-framedetached)
|
* [event: 'framedetached'](#event-framedetached)
|
||||||
@ -205,10 +205,21 @@ browser.newPage().then(async page =>
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
#### event: 'consolemessage'
|
#### event: 'console'
|
||||||
- <[string]>
|
- <[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'
|
#### event: 'dialog'
|
||||||
- <[Dialog]>
|
- <[Dialog]>
|
||||||
|
@ -21,7 +21,7 @@ var browser = new Browser();
|
|||||||
browser.newPage().then(async page => {
|
browser.newPage().then(async page => {
|
||||||
var modernizrPath = path.join(__dirname, '../third_party/phantomjs/examples/modernizr.js');
|
var modernizrPath = path.join(__dirname, '../third_party/phantomjs/examples/modernizr.js');
|
||||||
await page.injectFile(modernizrPath);
|
await page.injectFile(modernizrPath);
|
||||||
page.on('consolemessage', console.log);
|
page.on('console', console.log);
|
||||||
await page.evaluate(detectFeatures);
|
await page.evaluate(detectFeatures);
|
||||||
browser.close();
|
browser.close();
|
||||||
});
|
});
|
||||||
|
@ -18,7 +18,7 @@ var Browser = require('../lib/Browser');
|
|||||||
var browser = new Browser();
|
var browser = new Browser();
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
browser.newPage().then(async page => {
|
||||||
page.on('consolemessage', console.log);
|
page.on('console', console.log);
|
||||||
|
|
||||||
|
|
||||||
await page.setInPageCallback('callPhantom', msg => {
|
await page.setInPageCallback('callPhantom', msg => {
|
||||||
|
@ -224,7 +224,7 @@ class Page extends EventEmitter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let values = await Promise.all(event.args.map(arg => helper.serializeRemoteObject(this._client, arg)));
|
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) {
|
_onDialog(event) {
|
||||||
@ -668,7 +668,7 @@ function convertPrintParameterToInches(parameter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Page.Events = {
|
Page.Events = {
|
||||||
ConsoleMessage: 'consolemessage',
|
Console: 'console',
|
||||||
Dialog: 'dialog',
|
Dialog: 'dialog',
|
||||||
// Can'e use just 'error' due to node.js special treatment of error events.
|
// Can'e use just 'error' due to node.js special treatment of error events.
|
||||||
// @see https://nodejs.org/api/events.html#events_error_events
|
// @see https://nodejs.org/api/events.html#events_error_events
|
||||||
|
@ -62,7 +62,7 @@ class WebPage {
|
|||||||
this._pageEvents.on(PageEvents.Response, response => this._onResponseReceived(response));
|
this._pageEvents.on(PageEvents.Response, response => this._onResponseReceived(response));
|
||||||
this._pageEvents.on(PageEvents.RequestFinished, request => this._onRequestFinished(request));
|
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.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.Confirm, message => this._onConfirm(message));
|
||||||
this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message));
|
this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message));
|
||||||
this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog));
|
this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog));
|
||||||
@ -84,7 +84,7 @@ class WebPage {
|
|||||||
/**
|
/**
|
||||||
* @param {!Array<!Object>} args
|
* @param {!Array<!Object>} args
|
||||||
*/
|
*/
|
||||||
_onConsoleMessage(...args) {
|
_onConsole(...args) {
|
||||||
if (!this.onConsoleMessage)
|
if (!this.onConsoleMessage)
|
||||||
return;
|
return;
|
||||||
const text = args.join(' ');
|
const text = args.join(' ');
|
||||||
|
10
test/test.js
10
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() {
|
it('should work', SX(async function() {
|
||||||
let commandArgs = [];
|
let commandArgs = [];
|
||||||
page.once('consolemessage', (...args) => commandArgs = args);
|
page.once('console', (...args) => commandArgs = args);
|
||||||
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
|
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
|
||||||
await waitForEvents(page, 'consolemessage');
|
await waitForEvents(page, 'console');
|
||||||
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
|
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
|
||||||
}));
|
}));
|
||||||
it('should work for different console API calls', SX(async function() {
|
it('should work for different console API calls', SX(async function() {
|
||||||
let messages = [];
|
let messages = [];
|
||||||
page.on('consolemessage', msg => messages.push(msg));
|
page.on('console', msg => messages.push(msg));
|
||||||
page.evaluate(() => {
|
page.evaluate(() => {
|
||||||
// A pair of time/timeEnd generates only one Console API call.
|
// A pair of time/timeEnd generates only one Console API call.
|
||||||
console.time('calling console.time');
|
console.time('calling console.time');
|
||||||
@ -247,7 +247,7 @@ describe('Puppeteer', function() {
|
|||||||
console.error('calling console.error');
|
console.error('calling console.error');
|
||||||
});
|
});
|
||||||
// Wait for 5 events to hit.
|
// 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[0]).toContain('calling console.time');
|
||||||
expect(messages.slice(1)).toEqual([
|
expect(messages.slice(1)).toEqual([
|
||||||
'calling console.trace',
|
'calling console.trace',
|
||||||
|
Loading…
Reference in New Issue
Block a user