diff --git a/docs/api.md b/docs/api.md index 75ee60fe7a5..4d06575ddc6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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]> diff --git a/lib/Page.js b/lib/Page.js index eb881dd2d1e..26c61d35eaf 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -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} */ diff --git a/test/page.spec.js b/test/page.spec.js index 950b82f2de0..71c0c732d0f 100644 --- a/test/page.spec.js +++ b/test/page.spec.js @@ -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() {