feat(Page): page 'close' event (#2232)

This patch adds 'page.close' event.

References #952.
This commit is contained in:
JoelEinbinder 2018-03-28 13:11:51 -07:00 committed by Andrey Lushnikov
parent 47481967c5
commit d46e157f19
3 changed files with 24 additions and 0 deletions

View File

@ -39,6 +39,7 @@
* [browser.version()](#browserversion)
* [browser.wsEndpoint()](#browserwsendpoint)
- [class: Page](#class-page)
* [event: 'close'](#event-close)
* [event: 'console'](#event-console)
* [event: 'dialog'](#event-dialog)
* [event: 'domcontentloaded'](#event-domcontentloaded)
@ -520,6 +521,10 @@ page.on('request', logRequest);
page.removeListener('request', logRequest);
```
#### event: 'close'
Emitted when the page closes.
#### event: 'console'
- <[ConsoleMessage]>

View File

@ -103,6 +103,8 @@ 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));
this._target._isClosedPromise.then(() => this.emit(Page.Events.Close));
}
/**
@ -968,6 +970,7 @@ function convertPrintParameterToInches(parameter) {
}
Page.Events = {
Close: 'close',
Console: 'console',
Dialog: 'dialog',
DOMContentLoaded: 'domcontentloaded',

View File

@ -2397,5 +2397,21 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
});
});
});
describe('Page.Events.Close', function() {
it('should work with window.close', async function({ page, browser, server }) {
const newPagePromise = new Promise(fulfill => browser.once('targetcreated', target => fulfill(target.page())));
await page.evaluate(() => window['newPage'] = window.open('about:blank'));
const newPage = await newPagePromise;
const closedPromise = new Promise(x => newPage.on('close', x));
await page.evaluate(() => window['newPage'].close());
await closedPromise;
});
it('should work with page.close', async function({ page, browser, server }) {
const newPage = await browser.newPage();
const closedPromise = new Promise(x => newPage.on('close', x));
await newPage.close();
await closedPromise;
});
});
});
};