Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(chromium): unflake oopifs upon connect (#37407)

Dmitry Gozmaned7b48efab8c
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index 6f11f5f79e..33167497ba 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -394,6 +394,7 @@ class FrameSession {
private _metricsOverride: Protocol.Emulation.setDeviceMetricsOverrideParameters | undefined;
private _workerSessions = new Map<string, CRSession>();
private _initScriptIds = new Map<InitScript, string>();
+ private _bufferedAttachedToTargetEvents: Protocol.Target.attachedToTargetPayload[] | undefined;
constructor(crPage: CRPage, client: CRSession, targetId: string, parentSession: FrameSession | null) {
this._client = client;
@@ -476,6 +477,13 @@ class FrameSession {
if (!this._isMainFrame())
this._addRendererListeners();
this._addBrowserListeners();
+
+ // Buffer attachedToTarget events until we receive the frame tree.
+ // This way we'll know where to insert oopif targets in the frame hierarchy.
+ // Note that we cannot send Target.setAutoAttach after Runtime.runIfWaitingForDebugger,
+ // so we have to buffer events instead.
+ this._bufferedAttachedToTargetEvents = [];
+
const promises: Promise<any>[] = [
this._client.send('Page.enable'),
this._client.send('Page.getFrameTree').then(({ frameTree }) => {
@@ -484,6 +492,12 @@ class FrameSession {
this._addRendererListeners();
}
+ // Now that we have the frame tree, it is possible to insert oopif targets at the right place.
+ const attachedToTargetEvents = this._bufferedAttachedToTargetEvents || [];
+ this._bufferedAttachedToTargetEvents = undefined;
+ for (const event of attachedToTargetEvents)
+ this._onAttachedToTarget(event);
+
const localFrames = this._isMainFrame() ? this._page.frames() : [this._page.frameManager.frame(this._targetId)!];
for (const frame of localFrames) {
// Note: frames might be removed before we send these.
@@ -708,6 +722,11 @@ class FrameSession {
}
_onAttachedToTarget(event: Protocol.Target.attachedToTargetPayload) {
+ if (this._bufferedAttachedToTargetEvents) {
+ this._bufferedAttachedToTargetEvents.push(event);
+ return;
+ }
+
const session = this._client.createChildSession(event.sessionId);
if (event.targetInfo.type === 'iframe') {
diff --git a/tests/library/chromium/oopif.spec.ts b/tests/library/chromium/oopif.spec.ts
index 3a0a0558ad..058bce4a0c 100644
--- a/tests/library/chromium/oopif.spec.ts
+++ b/tests/library/chromium/oopif.spec.ts
@@ -383,8 +383,6 @@ it('should allow to re-connect to OOPIFs with CDP when iframes were there alread
const browser = await browserType.connectOverCDP(`http://localhost:${cdpPort}`);
const page = browser.contexts()[0].pages()[0];
- // can be fixed by reloading first
- // await page.reload();
expect(page.frames().length).toBe(2);
await assertOOPIFCount(browser, 1);
expect(await page.frames()[1].evaluate(() => '' + location.href)).toBe(server.CROSS_PROCESS_PREFIX + '/grid.html');