From d75fdb7098732cb9e5888729f77b90b40caf96f1 Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Wed, 21 Feb 2018 16:08:29 -0800 Subject: [PATCH] fix(Browser): wait for the page to close in the browser in page.close (#2064) Fixes #1491 --- lib/Browser.js | 2 ++ lib/Page.js | 3 ++- test/test.js | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Browser.js b/lib/Browser.js index 79d1d338..e0bdfe88 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 b79c0eee..3ede4406 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 bc0b9fb0..ae8fd19f 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() {