Skip to content

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

fix: separate Selectors instance per Playwright object (#9529)

Dmitry Gozmanb383c6bf3c16
diff --git a/packages/playwright-core/src/client/browserType.ts b/packages/playwright-core/src/client/browserType.ts
index 389db307ec..187eb6276e 100644
--- a/packages/playwright-core/src/client/browserType.ts
+++ b/packages/playwright-core/src/client/browserType.ts
@@ -27,6 +27,7 @@ import { assert, headersObjectToArray, getUserAgent, monotonicTime } from '../ut
import * as api from '../../types/types';
import { kBrowserClosedError } from '../utils/errors';
import { raceAgainstDeadline } from '../utils/async';
+import type { Playwright } from './playwright';
export interface BrowserServerLauncher {
launchServer(options?: LaunchServerOptions): Promise<api.BrowserServer>;
@@ -43,6 +44,7 @@ export interface BrowserServer extends api.BrowserServer {
export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, channels.BrowserTypeInitializer> implements api.BrowserType {
_serverLauncher?: BrowserServerLauncher;
_contexts = new Set<BrowserContext>();
+ _playwright!: Playwright;
// Instrumentation.
_defaultContextOptions: BrowserContextOptions = {};
@@ -54,10 +56,6 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
return (browserType as any)._object;
}
- constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.BrowserTypeInitializer) {
- super(parent, type, guid, initializer);
- }
-
executablePath(): string {
if (!this._initializer.executablePath)
throw new Error('Browser is not supported on current platform');
@@ -172,6 +170,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
closePipe();
return;
}
+ playwright._setSelectors(this._playwright.selectors);
browser = Browser.from(playwright._initializer.preLaunchedBrowser!);
browser._logger = logger;
browser._shouldCloseConnectionOnClose = true;
diff --git a/packages/playwright-core/src/client/playwright.ts b/packages/playwright-core/src/client/playwright.ts
index 2c499a2d97..55389e0877 100644
--- a/packages/playwright-core/src/client/playwright.ts
+++ b/packages/playwright-core/src/client/playwright.ts
@@ -25,7 +25,7 @@ import { BrowserType } from './browserType';
import { ChannelOwner } from './channelOwner';
import { Electron } from './electron';
import { Fetch } from './fetch';
-import { Selectors, SelectorsOwner, sharedSelectors } from './selectors';
+import { Selectors, SelectorsOwner } from './selectors';
import { Size } from './types';
const dnsLookupAsync = util.promisify(dns.lookup);
@@ -46,7 +46,7 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel, channel
readonly firefox: BrowserType;
readonly webkit: BrowserType;
readonly devices: Devices;
- readonly selectors: Selectors;
+ selectors: Selectors;
readonly request: Fetch;
readonly errors: { TimeoutError: typeof TimeoutError };
private _sockets = new Map<string, net.Socket>();
@@ -56,14 +56,17 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel, channel
super(parent, type, guid, initializer);
this.request = new Fetch(this);
this.chromium = BrowserType.from(initializer.chromium);
+ this.chromium._playwright = this;
this.firefox = BrowserType.from(initializer.firefox);
+ this.firefox._playwright = this;
this.webkit = BrowserType.from(initializer.webkit);
+ this.webkit._playwright = this;
this._android = Android.from(initializer.android);
this._electron = Electron.from(initializer.electron);
this.devices = {};
for (const { name, descriptor } of initializer.deviceDescriptors)
this.devices[name] = descriptor;
- this.selectors = sharedSelectors;
+ this.selectors = new Selectors();
this.errors = { TimeoutError };
const selectorsOwner = SelectorsOwner.from(initializer.selectors);
@@ -75,6 +78,13 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel, channel
});
}
+ _setSelectors(selectors: Selectors) {
+ const selectorsOwner = SelectorsOwner.from(this._initializer.selectors);
+ this.selectors._removeChannel(selectorsOwner);
+ this.selectors = selectors;
+ this.selectors._addChannel(selectorsOwner);
+ }
+
_enablePortForwarding(redirectPortForTest?: number) {
this._redirectPortForTest = redirectPortForTest;
this._channel.on('socksRequested', ({ uid, host, port }) => this._onSocksRequested(uid, host, port));
diff --git a/packages/playwright-core/src/client/selectors.ts b/packages/playwright-core/src/client/selectors.ts
index 65b5086fa1..4ad07e8c34 100644
--- a/packages/playwright-core/src/client/selectors.ts
+++ b/packages/playwright-core/src/client/selectors.ts
@@ -50,5 +50,3 @@ export class SelectorsOwner extends ChannelOwner<channels.SelectorsChannel, chan
return (browser as any)._object;
}
}
-
-export const sharedSelectors = new Selectors();