Skip to content

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

chore: fix utility world name clash over cdp (#36024)

Dmitry Gozman42ea95e1c104
diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.ts
index 263999c3f4..d94c13e298 100644
--- a/packages/playwright-core/src/server/chromium/crPage.ts
+++ b/packages/playwright-core/src/server/chromium/crPage.ts
@@ -53,10 +53,10 @@ import type * as types from '../types';
import type * as channels from '@protocol/channels';
-const UTILITY_WORLD_NAME = '__playwright_utility_world__';
export type WindowBounds = { top?: number, left?: number, width?: number, height?: number };
export class CRPage implements PageDelegate {
+ readonly utilityWorldName: string;
readonly _mainFrameSession: FrameSession;
readonly _sessions = new Map<Protocol.Target.TargetID, FrameSession>();
readonly _page: Page;
@@ -95,6 +95,9 @@ export class CRPage implements PageDelegate {
this._coverage = new CRCoverage(client);
this._browserContext = browserContext;
this._page = new Page(this, browserContext);
+ // Create a unique utility world for this Playwright instance, just in case there
+ // are multiple instances of Playwright connected to the same browser page.
+ this.utilityWorldName = `__playwright_utility_world_${this._page.guid}`;
this._networkManager = new CRNetworkManager(this._page, null);
// Sync any browser context state to the network manager. This does not talk over CDP because
// we have not connected any sessions to the network manager yet.
@@ -490,7 +493,7 @@ class FrameSession {
this._client._sendMayFail('Page.createIsolatedWorld', {
frameId: frame._id,
grantUniveralAccess: true,
- worldName: UTILITY_WORLD_NAME,
+ worldName: this._crPage.utilityWorldName,
});
}
@@ -512,7 +515,7 @@ class FrameSession {
this._client.send('Runtime.enable', {}),
this._client.send('Page.addScriptToEvaluateOnNewDocument', {
source: '',
- worldName: UTILITY_WORLD_NAME,
+ worldName: this._crPage.utilityWorldName,
}),
this._crPage._networkManager.addSession(this._client, undefined, this._isMainFrame()),
this._client.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }),
@@ -675,7 +678,7 @@ class FrameSession {
let worldName: types.World|null = null;
if (contextPayload.auxData && !!contextPayload.auxData.isDefault)
worldName = 'main';
- else if (contextPayload.name === UTILITY_WORLD_NAME)
+ else if (contextPayload.name === this._crPage.utilityWorldName)
worldName = 'utility';
const context = new dom.FrameExecutionContext(delegate, frame, worldName);
if (worldName)
@@ -1055,7 +1058,7 @@ class FrameSession {
}
async _evaluateOnNewDocument(initScript: InitScript, world: types.World, runImmediately?: boolean): Promise<void> {
- const worldName = world === 'utility' ? UTILITY_WORLD_NAME : undefined;
+ const worldName = world === 'utility' ? this._crPage.utilityWorldName : undefined;
const { identifier } = await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: initScript.source, worldName, runImmediately });
if (!initScript.internal)
this._evaluateOnNewDocumentIdentifiers.push(identifier);
diff --git a/tests/library/chromium/connect-over-cdp.spec.ts b/tests/library/chromium/connect-over-cdp.spec.ts
index 3191403be8..d5ff791567 100644
--- a/tests/library/chromium/connect-over-cdp.spec.ts
+++ b/tests/library/chromium/connect-over-cdp.spec.ts
@@ -20,6 +20,7 @@ import http from 'http';
import fs from 'fs';
import { getUserAgent } from '../../../packages/playwright-core/lib/server/utils/userAgent';
import { suppressCertificateWarning } from '../../config/utils';
+import type { Frame } from '../../../packages/playwright-core/lib/server/frames';
test.skip(({ mode }) => mode === 'service2');
@@ -537,3 +538,31 @@ test('should print custom ws close error', async ({ browserType, server }) => {
const error = await browserType.connectOverCDP(`ws://localhost:${server.PORT}/ws`).catch(e => e);
expect(error.message).toContain(`Browser logs:\n\nOh my!\n`);
});
+
+test('should not reuse utility worlds between two clients', async ({ browserType, toImpl }, testInfo) => {
+ const port = 9339 + testInfo.workerIndex;
+ const browserServer = await browserType.launch({
+ args: ['--remote-debugging-port=' + port]
+ });
+ try {
+ const browser1 = await browserType.connectOverCDP(`http://127.0.0.1:${port}/`);
+ const context1 = browser1.contexts()[0];
+ expect(context1.pages().length).toBe(0);
+ const page1 = await context1.newPage();
+ const frameImpl1 = toImpl(page1.mainFrame()) as Frame;
+ await frameImpl1.evaluateExpression('window.foo = 42', { world: 'utility' });
+
+ const browser2 = await browserType.connectOverCDP(`http://127.0.0.1:${port}/`);
+ const context2 = browser2.contexts()[0];
+ const page2 = context2.pages()[0];
+ const frameImpl2 = toImpl(page2.mainFrame()) as Frame;
+ const result = await frameImpl2.evaluateExpression('window.foo', { world: 'utility' });
+
+ await browser1.close();
+ await browser2.close();
+
+ expect(result).toBeUndefined();
+ } finally {
+ await browserServer.close();
+ }
+});