Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Console): Introduce ConsoleMessage type #909

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
+ [dialog.dismiss()](#dialogdismiss)
+ [dialog.message()](#dialogmessage)
+ [dialog.type](#dialogtype)
* [class: ConsoleMessage](#class-consolemessage)
+ [consoleMessage.args](#consolemessageargs)
+ [consoleMessage.text](#consolemessagetext)
+ [consoleMessage.type](#consolemessagetype)
* [class: Frame](#class-frame)
+ [frame.$(selector)](#frameselector)
+ [frame.$$(selector)](#frameselector)
Expand Down Expand Up @@ -249,19 +253,19 @@ puppeteer.launch().then(async browser => {
```

#### event: 'console'
- <[string]>
- <[ConsoleMessage]>

Emitted when JavaScript within the page calls one of console API methods, e.g. `console.log` or `console.dir`. Also emitted if the page throws an error or a warning.

The arguments passed into `console.log` appear as arguments on the event handler.

An example of handling `console` event:
```js
page.on('console', (...args) => {
for (let i = 0; i < args.length; ++i)
page.on('console', msg => {
for (let i = 0; i < msg.args.length; ++i)
console.log(`${i}: ${args[i]}`);
});
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
```

#### event: 'dialog'
Expand Down Expand Up @@ -1093,6 +1097,21 @@ puppeteer.launch().then(async browser => {

Dialog's type, could be one of the `alert`, `beforeunload`, `confirm` and `prompt`.

### class: ConsoleMessage
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline this into the console event?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine like this


[ConsoleMessage] objects are dispatched by page via the ['console'](#event-console) event.

#### consoleMessage.args
- <[Array]<[string]>>

#### consoleMessage.text
- <[string]>

#### consoleMessage.type
- <[string]>

One of the following values: `'log'`, `'debug'`, `'info'`, `'error'`, `'warning'`, `'dir'`, `'dirxml'`, `'table'`, `'trace'`, `'clear'`, `'startGroup'`, `'startGroupCollapsed'`, `'endGroup'`, `'assert'`, `'profile'`, `'profileEnd'`, `'count'`, `'timeEnd'`.

### class: Frame

At every point of time, page exposes its current frame tree via the [page.mainFrame()](#pagemainframe) and [frame.childFrames()](#framechildframes) methods.
Expand Down Expand Up @@ -1432,6 +1451,7 @@ Contains the URL of the response.
[stream.Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "stream.Readable"
[Error]: https://nodejs.org/api/errors.html#errors_class_error "Error"
[Frame]: #class-frame "Frame"
[ConsoleMessage]: #class-consolemessage "ConsoleMessage"
[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator"
[Response]: #class-response "Response"
[Request]: #class-request "Request"
Expand Down
18 changes: 17 additions & 1 deletion lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ class Page extends EventEmitter {
return;
}
const values = await Promise.all(event.args.map(arg => helper.serializeRemoteObject(this._client, arg)));
this.emit(Page.Events.Console, ...values);
const text = values.length && typeof values[0] === 'string' ? values[0] : null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make text all the values.join(" ")


const message = new ConsoleMessage(event.type, text, values);
this.emit(Page.Events.Console, message);
}

_onDialog(event) {
Expand Down Expand Up @@ -875,6 +878,19 @@ Page.Viewport;
* @property {("Strict"|"Lax")=} sameSite
*/

class ConsoleMessage {
/**
* @param {string} type
* @param {string} text
* @param {!Array<*>} args
*/
constructor(type, text, args) {
this.type = type;
this.text = text;
this.args = args;
}
}


module.exports = Page;
helper.tracePublicAPI(Page);
27 changes: 16 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,17 @@ describe('Page', function() {
}));
});

describe('Page.Events.Console', function() {
fdescribe('Page.Events.Console', function() {
it('should work', SX(async function() {
let commandArgs = [];
page.once('console', (...args) => commandArgs = args);
let message = null;
page.once('console', m => message = m);
await Promise.all([
page.evaluate(() => console.log(5, 'hello', {foo: 'bar'})),
page.evaluate(() => console.log('hello', 5, {foo: 'bar'})),
waitForEvents(page, 'console')
]);
expect(commandArgs).toEqual([5, 'hello', {foo: 'bar'}]);
expect(message.text).toEqual('hello');
expect(message.type).toEqual('log');
expect(message.args).toEqual(['hello', 5, {foo: 'bar'}]);
}));
it('should work for different console API calls', SX(async function() {
const messages = [];
Expand All @@ -559,11 +561,14 @@ describe('Page', function() {
console.error('calling console.error');
console.log(Promise.resolve('should not wait until resolved!'));
}),
// Wait for 5 events to hit.
// Wait for 5 events to hit - console.time is not reported
waitForEvents(page, 'console', 5)
]);
expect(messages[0]).toContain('calling console.time');
expect(messages.slice(1)).toEqual([
expect(messages.map(msg => msg.type)).toEqual([
'timeEnd', 'trace', 'dir', 'warning', 'error', 'log'
]);
expect(messages[0].text).toContain('calling console.time');
expect(messages.slice(1).map(msg => msg.text)).toEqual([
'calling console.trace',
'calling console.dir',
'calling console.warn',
Expand All @@ -572,13 +577,13 @@ describe('Page', function() {
]);
}));
it('should not fail for window object', SX(async function() {
let windowObj = null;
page.once('console', arg => windowObj = arg);
let message = null;
page.once('console', msg => message = msg);
await Promise.all([
page.evaluate(() => console.error(window)),
waitForEvents(page, 'console')
]);
expect(windowObj).toBe('Window');
expect(message.text).toBe('Window');
}));
});

Expand Down
1 change: 1 addition & 0 deletions utils/doclint/check_public_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const EXCLUDE_CLASSES = new Set([
const EXCLUDE_METHODS = new Set([
'Body.constructor',
'Browser.constructor',
'ConsoleMessage.constructor',
'Dialog.constructor',
'ElementHandle.constructor',
'Frame.constructor',
Expand Down