mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Page): page 'close' event (#2232)
This patch adds 'page.close' event. References #952.
This commit is contained in:
parent
47481967c5
commit
d46e157f19
@ -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]>
|
||||
|
||||
|
@ -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',
|
||||
|
@ -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;
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user