From fb859115c0e2829bae1d1b32edbf642988e2ef76 Mon Sep 17 00:00:00 2001 From: dmitrysteblyuk Date: Thu, 26 Nov 2020 12:43:42 +0100 Subject: [PATCH] fix: do not use old utility world (#6528) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t use the old utility world, as it is being destroyed later when browser reconnects to the page. Issue: #6527 --- src/common/FrameManager.ts | 27 +++++++++++++-------------- test/launcher.spec.ts | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/common/FrameManager.ts b/src/common/FrameManager.ts index e1017487d35c3..df993c9bbba8f 100644 --- a/src/common/FrameManager.ts +++ b/src/common/FrameManager.ts @@ -322,18 +322,19 @@ export class FrameManager extends EventEmitter { await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: `//# sourceURL=${EVALUATION_SCRIPT_URL}`, worldName: name, - }), - await Promise.all( - this.frames().map((frame) => - this._client - .send('Page.createIsolatedWorld', { - frameId: frame._id, - grantUniveralAccess: true, - worldName: name, - }) - .catch(debugError) - ) - ); // frames might be removed before we send this + }); + // Frames might be removed before we send this. + await Promise.all( + this.frames().map((frame) => + this._client + .send('Page.createIsolatedWorld', { + frameId: frame._id, + worldName: name, + grantUniveralAccess: true, + }) + .catch(debugError) + ) + ); } _onFrameNavigatedWithinDocument(frameId: string, url: string): void { @@ -369,8 +370,6 @@ export class FrameManager extends EventEmitter { world = frame._secondaryWorld; } } - if (contextPayload.auxData && contextPayload.auxData['type'] === 'isolated') - this._isolatedWorlds.add(contextPayload.name); const context = new ExecutionContext(this._client, contextPayload, world); if (world) world._setContext(context); this._contextIdToContext.set(contextPayload.id, context); diff --git a/test/launcher.spec.ts b/test/launcher.spec.ts index a352b116e68bc..41b5cb1465998 100644 --- a/test/launcher.spec.ts +++ b/test/launcher.spec.ts @@ -589,6 +589,25 @@ describe('Launcher specs', function () { await browserOne.close(); } ); + // @see https://github.com/puppeteer/puppeteer/issues/6527 + it('should be able to reconnect', async () => { + const { puppeteer, server } = getTestState(); + const browserOne = await puppeteer.launch(); + const browserWSEndpoint = browserOne.wsEndpoint(); + const pageOne = await browserOne.newPage(); + await pageOne.goto(server.EMPTY_PAGE); + browserOne.disconnect(); + + const browserTwo = await puppeteer.connect({ browserWSEndpoint }); + const pages = await browserTwo.pages(); + const pageTwo = pages.find((page) => page.url() === server.EMPTY_PAGE); + await pageTwo.reload(); + const bodyHandle = await pageTwo.waitForSelector('body', { + timeout: 10000, + }); + await bodyHandle.dispose(); + await browserTwo.close(); + }); }); describe('Puppeteer.executablePath', function () { itOnlyRegularInstall('should work', async () => {