feat(page): add page.isClosed method (#2588)

Fixes #2501.
This commit is contained in:
Alex Veligura 2018-05-26 02:53:57 +03:00 committed by Andrey Lushnikov
parent b522ecaa65
commit 32f4c173c8
3 changed files with 25 additions and 1 deletions

View File

@ -97,6 +97,7 @@
* [page.goForward(options)](#pagegoforwardoptions)
* [page.goto(url, options)](#pagegotourl-options)
* [page.hover(selector)](#pagehoverselector)
* [page.isClosed()](#pageisclosed)
* [page.keyboard](#pagekeyboard)
* [page.mainFrame()](#pagemainframe)
* [page.metrics()](#pagemetrics)
@ -1180,6 +1181,12 @@ If there's no element matching `selector`, the method throws an error.
Shortcut for [page.mainFrame().hover(selector)](#framehoverselector).
#### page.isClosed()
- returns: boolean
Indicates that the page has been closed.
#### page.keyboard
- returns: <[Keyboard]>

View File

@ -72,6 +72,7 @@ class Page extends EventEmitter {
*/
constructor(client, target, frameTree, ignoreHTTPSErrors, screenshotTaskQueue) {
super();
this._closed = false;
this._client = client;
this._target = target;
this._keyboard = new Keyboard(client);
@ -131,7 +132,10 @@ class Page extends EventEmitter {
client.on('Inspector.targetCrashed', event => this._onTargetCrashed());
client.on('Performance.metrics', event => this._emitMetrics(event));
client.on('Log.entryAdded', event => this._onLogEntryAdded(this._client, event));
this._target._isClosedPromise.then(() => this.emit(Page.Events.Close));
this._target._isClosedPromise.then(() => {
this.emit(Page.Events.Close);
this._closed = true;
});
}
/**
@ -876,6 +880,13 @@ class Page extends EventEmitter {
}
}
/**
* @return {boolean}
*/
isClosed() {
return this._closed;
}
/**
* @return {!Mouse}
*/

View File

@ -54,6 +54,12 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
dialog.accept();
await waitEvent(newPage, 'close');
});
it('should set the page close state', async({ browser }) => {
const newPage = await browser.newPage();
expect(newPage.isClosed()).toBe(false);
await newPage.close();
expect(newPage.isClosed()).toBe(true);
});
});
describe('Page.Events.error', function() {