Emit 'error' event when page crashes (#266)

This patch starts emitting 'error' event when page crashes.
'error' events have special treatment in node, so page crashes
become observable for users.

Fixes #262.
This commit is contained in:
Andrey Lushnikov 2017-08-15 11:13:05 -07:00 committed by GitHub
parent cd54e5fb11
commit 24764df4c4
3 changed files with 25 additions and 1 deletions

View File

@ -14,6 +14,7 @@
* [class: Page](#class-page) * [class: Page](#class-page)
+ [event: 'console'](#event-console) + [event: 'console'](#event-console)
+ [event: 'dialog'](#event-dialog) + [event: 'dialog'](#event-dialog)
+ [event: 'error'](#event-error)
+ [event: 'frameattached'](#event-frameattached) + [event: 'frameattached'](#event-frameattached)
+ [event: 'framedetached'](#event-framedetached) + [event: 'framedetached'](#event-framedetached)
+ [event: 'framenavigated'](#event-framenavigated) + [event: 'framenavigated'](#event-framenavigated)
@ -216,6 +217,13 @@ page.evaluate(() => console.log(5, 'hello', {foo: 'bar'}));
Emitted when a JavaScript dialog, such as `alert`, `prompt`, `confirm` or `beforeunload`, gets opened on the page. Puppeteer can take action to the dialog via dialog's [accept](#dialogacceptprompttext) or [dismiss](#dialogdismiss) methods. Emitted when a JavaScript dialog, such as `alert`, `prompt`, `confirm` or `beforeunload`, gets opened on the page. Puppeteer can take action to the dialog via dialog's [accept](#dialogacceptprompttext) or [dismiss](#dialogdismiss) methods.
#### event: 'error'
- <[Error]>
Emitted when the page crashes.
> **Note** `error` event has a special meaning in Node, see [error events](https://nodejs.org/api/events.html#events_error_events) for details.
#### event: 'frameattached' #### event: 'frameattached'
- <[Frame]> - <[Frame]>
@ -1153,6 +1161,7 @@ Contains the URL of the response.
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise" [Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String" [string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String"
[stream.Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "stream.Readable" [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]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-frame "Frame" [Frame]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-frame "Frame"
[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator" [iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator"
[Response]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-response "Response" [Response]: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-response "Response"

View File

@ -80,11 +80,15 @@ class Page extends EventEmitter {
this._networkManager.on(NetworkManager.Events.RequestFinished, event => this.emit(Page.Events.RequestFinished, event)); this._networkManager.on(NetworkManager.Events.RequestFinished, event => this.emit(Page.Events.RequestFinished, event));
client.on('Page.loadEventFired', event => this.emit(Page.Events.Load)); client.on('Page.loadEventFired', event => this.emit(Page.Events.Load));
client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event)); client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event));
client.on('Page.javascriptDialogOpening', event => this._onDialog(event)); client.on('Page.javascriptDialogOpening', event => this._onDialog(event));
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)); client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
client.on('Security.certificateError', event => this._onCertificateError(event)); client.on('Security.certificateError', event => this._onCertificateError(event));
client.on('Inspector.targetCrashed', event => this._onTargetCrashed());
}
_onTargetCrashed() {
this.emit('error', new Error('Page crashed!'));
} }
/** /**
@ -694,6 +698,7 @@ function convertPrintParameterToInches(parameter) {
Page.Events = { Page.Events = {
Console: 'console', Console: 'console',
Dialog: 'dialog', Dialog: 'dialog',
Error: 'error',
// 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
PageError: 'pageerror', PageError: 'pageerror',

View File

@ -155,6 +155,16 @@ describe('Page', function() {
})); }));
}); });
describe('Page.Events.error', function() {
it('should throw when page crashes', SX(async function() {
let error = null;
page.on('error', err => error = err);
page.goto('chrome://crash').catch(e => {});
await waitForEvents(page, 'error');
expect(error.message).toBe('Page crashed!');
}));
});
describe('Page.evaluate', function() { describe('Page.evaluate', function() {
it('should work', SX(async function() { it('should work', SX(async function() {
let result = await page.evaluate(() => 7 * 3); let result = await page.evaluate(() => 7 * 3);