From 0955225b51421663288523a3dfb63103b51775b4 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 7 Apr 2022 15:05:36 +0200 Subject: [PATCH] fix: consider existing frames when waiting for a frame (#8200) --- src/common/Page.ts | 14 +++++++++++++- test/oopif.spec.ts | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/common/Page.ts b/src/common/Page.ts index 43974162237..169c8432cc5 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -2039,7 +2039,7 @@ export class Page extends EventEmitter { return false; } - return Promise.race([ + const eventRace = Promise.race([ helper.waitForEvent( this._frameManager, FrameManagerEmittedEvents.FrameAttached, @@ -2055,6 +2055,18 @@ export class Page extends EventEmitter { this._sessionClosePromise() ), ]); + + return Promise.race([ + eventRace, + (async () => { + for (const frame of this.frames()) { + if (await predicate(frame)) { + return frame; + } + } + await eventRace; + })(), + ]); } /** diff --git a/test/oopif.spec.ts b/test/oopif.spec.ts index e9999a3cfa5..b8e39b6973c 100644 --- a/test/oopif.spec.ts +++ b/test/oopif.spec.ts @@ -371,6 +371,21 @@ describeChromeOnly('OOPIF', function () { await target.page(); browser1.disconnect(); }); + + describe('waitForFrame', () => { + it('should resolve immediately if the frame already exists', async () => { + const { server } = getTestState(); + + await page.goto(server.EMPTY_PAGE); + await utils.attachFrame( + page, + 'frame2', + server.CROSS_PROCESS_PREFIX + '/empty.html' + ); + + await page.waitForFrame((frame) => frame.url().endsWith('/empty.html')); + }); + }); }); /**