diff --git a/lib/Browser.js b/lib/Browser.js index 79d1d3382f1..e0bdfe88b3f 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -81,6 +81,7 @@ class Browser extends EventEmitter { const target = this._targets.get(event.targetId); target._initializedCallback(false); this._targets.delete(event.targetId); + target._closedCallback(); if (await target._initializedPromise) this.emit(Browser.Events.TargetDestroyed, target); } @@ -198,6 +199,7 @@ class Target { /** @type {?Promise} */ this._pagePromise = null; this._initializedPromise = new Promise(fulfill => this._initializedCallback = fulfill); + this._isClosedPromise = new Promise(fulfill => this._closedCallback = fulfill); this._isInitialized = this._targetInfo.type !== 'page' || this._targetInfo.url !== ''; if (this._isInitialized) this._initializedCallback(true); diff --git a/lib/Page.js b/lib/Page.js index b79c0eeea44..3ede44063ef 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -798,7 +798,8 @@ class Page extends EventEmitter { async close() { console.assert(!!this._client._connection, 'Protocol error: Connection closed. Most likely the page has been closed.'); - await this._client._connection.send('Target.closeTarget', {targetId: this._target._targetId}); + await this._client._connection.send('Target.closeTarget', { targetId: this._target._targetId }); + await this._target._isClosedPromise; } /** diff --git a/test/test.js b/test/test.js index bc0b9fb017b..ae8fd19f5e7 100644 --- a/test/test.js +++ b/test/test.js @@ -390,6 +390,12 @@ describe('Page', function() { await neverResolves.catch(e => error = e); expect(error.message).toContain('Protocol error'); }); + it('should not be visible in browser.pages', async({browser}) => { + const newPage = await browser.newPage(); + expect(await browser.pages()).toContain(newPage); + await newPage.close(); + expect(await browser.pages()).not.toContain(newPage); + }); }); describe('Page.Events.error', function() {