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): insert oopifs into the frame hierarchy when connecting over CDP (#37359)

Dmitry Gozmanca0e7c8c61b6
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index c019cac28b..6f11f5f79e 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -432,13 +432,13 @@ class FrameSession {
eventsHelper.addEventListener(this._client, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)),
eventsHelper.addEventListener(this._client, 'Runtime.executionContextDestroyed', event => this._onExecutionContextDestroyed(event.executionContextId)),
eventsHelper.addEventListener(this._client, 'Runtime.executionContextsCleared', event => this._onExecutionContextsCleared()),
- eventsHelper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)),
- eventsHelper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event)),
]);
}
private _addBrowserListeners() {
this._eventListeners.push(...[
+ eventsHelper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)),
+ eventsHelper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event)),
eventsHelper.addEventListener(this._client, 'Inspector.targetCrashed', event => this._onTargetCrashed()),
eventsHelper.addEventListener(this._client, 'Page.screencastFrame', event => this._onScreencastFrame(event)),
eventsHelper.addEventListener(this._client, 'Page.windowOpen', event => this._onWindowOpen(event)),
@@ -713,7 +713,12 @@ class FrameSession {
if (event.targetInfo.type === 'iframe') {
// Frame id equals target id.
const targetId = event.targetInfo.targetId;
- const frame = this._page.frameManager.frame(targetId);
+ let frame = this._page.frameManager.frame(targetId);
+ if (!frame && event.targetInfo.parentFrameId) {
+ // When connecting to an existing page with an iframe, there is an "iframe" target,
+ // but no local frame is reported in getFrameTree. We can create a remote frame here.
+ frame = this._page.frameManager.frameAttached(targetId, event.targetInfo.parentFrameId);
+ }
if (!frame)
return; // Subtree may be already gone due to renderer/browser race.
this._page.frameManager.removeChildFramesRecursively(frame);
diff --git a/tests/library/chromium/oopif.spec.ts b/tests/library/chromium/oopif.spec.ts
index c062239a60..3a0a0558ad 100644
--- a/tests/library/chromium/oopif.spec.ts
+++ b/tests/library/chromium/oopif.spec.ts
@@ -371,8 +371,10 @@ it('should intercept response body from oopif', async function({ page, browser,
expect(await response.text()).toBeTruthy();
});
-it.fail('should allow to re-connect to OOPIFs with CDP when iframes were there already', async ({ browserType, server }) => {
+it('should allow to re-connect to OOPIFs with CDP when iframes were there already', async ({ browserType, server, browserMajorVersion }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36095' });
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/17656' });
+ it.skip(browserMajorVersion < 141, 'old chromium does not have TargetInfo.parentFrameId');
const cdpPort = 10123 + it.info().parallelIndex * 4;
const hostBrowser = await browserType.launch({ cdpPort } as any);
@@ -386,6 +388,8 @@ it.fail('should allow to re-connect to OOPIFs with CDP when iframes were there a
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');
+ await browser.close();
+ await hostBrowser.close();
});
async function assertOOPIFCount(browser: Browser, count: number) {