Skip to content

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

chore: refactor browser creation from PlaywrightConnection into PlaywrightServer (#36369)

Simon Knott71088f6e9dce
diff --git a/packages/playwright-core/src/remote/playwrightConnection.ts b/packages/playwright-core/src/remote/playwrightConnection.ts
index 1a3dcb2a32..d92caeee78 100644
--- a/packages/playwright-core/src/remote/playwrightConnection.ts
+++ b/packages/playwright-core/src/remote/playwrightConnection.ts
@@ -14,63 +14,35 @@
* limitations under the License.
*/
-import { SocksProxy } from '../server/utils/socksProxy';
import { DispatcherConnection, PlaywrightDispatcher, RootDispatcher } from '../server';
import { AndroidDevice } from '../server/android/android';
import { Browser } from '../server/browser';
import { DebugControllerDispatcher } from '../server/dispatchers/debugControllerDispatcher';
-import { serverSideCallMetadata } from '../server/instrumentation';
-import { assert } from '../utils/isomorphic/assert';
-import { isUnderTest } from '../server/utils/debug';
import { startProfiling, stopProfiling } from '../server/utils/profiler';
-import { monotonicTime } from '../utils';
+import { monotonicTime, Semaphore } from '../utils';
import { debugLogger } from '../server/utils/debugLogger';
+import { PlaywrightDispatcherOptions } from '../server/dispatchers/playwrightDispatcher';
import type { DispatcherScope, Playwright } from '../server';
-import type { LaunchOptions } from '../server/types';
import type { WebSocket } from '../utilsBundle';
-import type * as channels from '@protocol/channels';
-
-export type ClientType = 'controller' | 'launch-browser' | 'reuse-browser' | 'pre-launched-browser-or-android';
-
-type Options = {
- allowFSPaths: boolean,
- socksProxyPattern: string | undefined,
- browserName: string | null,
- launchOptions: LaunchOptions,
- sharedBrowser?: boolean,
-};
-
-type PreLaunched = {
- browser?: Browser | undefined;
- androidDevice?: AndroidDevice | undefined;
- socksProxy?: SocksProxy | undefined;
-};
export class PlaywrightConnection {
private _ws: WebSocket;
- private _onClose: () => void;
+ private _semaphore: Semaphore;
private _dispatcherConnection: DispatcherConnection;
private _cleanups: (() => Promise<void>)[] = [];
private _id: string;
private _disconnected = false;
- private _playwright: Playwright;
- private _preLaunched: PreLaunched;
- private _options: Options;
private _root: DispatcherScope;
private _profileName: string;
- constructor(lock: Promise<void>, clientType: ClientType, ws: WebSocket, options: Options, playwright: Playwright, preLaunched: PreLaunched, id: string, onClose: () => void) {
+ constructor(semaphore: Semaphore, ws: WebSocket, controller: boolean, playwright: Playwright, initialize: () => Promise<PlaywrightDispatcherOptions & { dispose?(): Promise<void> }>, id: string) {
this._ws = ws;
- this._playwright = playwright;
- this._preLaunched = preLaunched;
- this._options = options;
- options.launchOptions = filterLaunchOptions(options.launchOptions, options.allowFSPaths);
- if (clientType === 'pre-launched-browser-or-android')
- assert(preLaunched.browser || preLaunched.androidDevice);
- this._onClose = onClose;
+ this._semaphore = semaphore;
this._id = id;
- this._profileName = `${new Date().toISOString()}-${clientType}`;
+ this._profileName = new Date().toISOString();
+
+ const lock = this._semaphore.acquire();
this._dispatcherConnection = new DispatcherConnection();
this._dispatcherConnection.onmessage = async message => {
@@ -98,148 +70,39 @@ export class PlaywrightConnection {
ws.on('close', () => this._onDisconnect());
ws.on('error', (error: Error) => this._onDisconnect(error));
- if (clientType === 'controller') {
- this._root = this._initDebugControllerMode();
+ if (controller) {
+ debugLogger.log('server', `[${this._id}] engaged reuse controller mode`);
+ this._root = new DebugControllerDispatcher(this._dispatcherConnection, playwright.debugController);
return;
}
- this._root = new RootDispatcher(this._dispatcherConnection, async (scope, options) => {
+ this._root = new RootDispatcher(this._dispatcherConnection, async (scope, params) => {
await startProfiling();
- if (clientType === 'reuse-browser')
- return await this._initReuseBrowsersMode(scope, options);
- if (clientType === 'pre-launched-browser-or-android')
- return this._preLaunched.browser ? await this._initPreLaunchedBrowserMode(scope, options) : await this._initPreLaunchedAndroidMode(scope);
- if (clientType === 'launch-browser')
- return await this._initLaunchBrowserMode(scope, options);
- throw new Error('Unsupported client type: ' + clientType);
- });
- }
-
- private async _initLaunchBrowserMode(scope: RootDispatcher, options: channels.RootInitializeParams) {
- debugLogger.log('server', `[${this._id}] engaged launch mode for "${this._options.browserName}"`);
- const ownedSocksProxy = await this._createOwnedSocksProxy();
- const browser = await this._playwright[this._options.browserName as 'chromium'].launch(serverSideCallMetadata(), this._options.launchOptions);
- browser.options.sdkLanguage = options.sdkLanguage;
-
- this._cleanups.push(() => browser.close({ reason: 'Connection terminated' }));
- browser.on(Browser.Events.Disconnected, () => {
- // Underlying browser did close for some reason - force disconnect the client.
- this.close({ code: 1001, reason: 'Browser closed' });
- });
-
- return new PlaywrightDispatcher(scope, this._playwright, { socksProxy: ownedSocksProxy, preLaunchedBrowser: browser, denyLaunch: true, });
- }
-
- private async _initPreLaunchedBrowserMode(scope: RootDispatcher, options: channels.RootInitializeParams) {
- debugLogger.log('server', `[${this._id}] engaged pre-launched (browser) mode`);
- // Note: connected client owns the socks proxy and configures the pattern.
- this._preLaunched.socksProxy?.setPattern(this._options.socksProxyPattern);
-
- const browser = this._preLaunched.browser!;
- browser.options.sdkLanguage = options.sdkLanguage;
- browser.on(Browser.Events.Disconnected, () => {
- // Underlying browser did close for some reason - force disconnect the client.
- this.close({ code: 1001, reason: 'Browser closed' });
- });
-
- const playwrightDispatcher = new PlaywrightDispatcher(scope, this._playwright, {
- socksProxy: this._preLaunched.socksProxy,
- preLaunchedBrowser: browser,
- sharedBrowser: this._options.sharedBrowser,
- denyLaunch: true,
- });
- // In pre-launched mode, keep only the pre-launched browser.
- for (const b of this._playwright.allBrowsers()) {
- if (b !== browser)
- await b.close({ reason: 'Connection terminated' });
- }
- this._cleanups.push(() => playwrightDispatcher.cleanup());
- return playwrightDispatcher;
- }
-
- private async _initPreLaunchedAndroidMode(scope: RootDispatcher) {
- debugLogger.log('server', `[${this._id}] engaged pre-launched (Android) mode`);
- const androidDevice = this._preLaunched.androidDevice!;
- androidDevice.on(AndroidDevice.Events.Close, () => {
- // Underlying browser did close for some reason - force disconnect the client.
- this.close({ code: 1001, reason: 'Android device disconnected' });
- });
- const playwrightDispatcher = new PlaywrightDispatcher(scope, this._playwright, { preLaunchedAndroidDevice: androidDevice, denyLaunch: true });
- this._cleanups.push(() => playwrightDispatcher.cleanup());
- return playwrightDispatcher;
- }
-
- private _initDebugControllerMode(): DebugControllerDispatcher {
- debugLogger.log('server', `[${this._id}] engaged reuse controller mode`);
- // Always create new instance based on the reused Playwright instance.
- return new DebugControllerDispatcher(this._dispatcherConnection, this._playwright.debugController);
- }
-
- private async _initReuseBrowsersMode(scope: RootDispatcher, options: channels.RootInitializeParams) {
- // Note: reuse browser mode does not support socks proxy, because
- // clients come and go, while the browser stays the same.
-
- debugLogger.log('server', `[${this._id}] engaged reuse browsers mode for ${this._options.browserName}`);
-
- const requestedOptions = launchOptionsHash(this._options.launchOptions);
- let browser = this._playwright.allBrowsers().find(b => {
- if (b.options.name !== this._options.browserName)
- return false;
- const existingOptions = launchOptionsHash(b.options.originalLaunchOptions);
- return existingOptions === requestedOptions;
- });
-
- // Close remaining browsers of this type+channel. Keep different browser types for the speed.
- for (const b of this._playwright.allBrowsers()) {
- if (b === browser)
- continue;
- if (b.options.name === this._options.browserName && b.options.channel === this._options.launchOptions.channel)
- await b.close({ reason: 'Connection terminated' });
- }
-
- if (!browser) {
- browser = await this._playwright[(this._options.browserName || 'chromium') as 'chromium'].launch(serverSideCallMetadata(), {
- ...this._options.launchOptions,
- headless: !!process.env.PW_DEBUG_CONTROLLER_HEADLESS,
- });
- browser.on(Browser.Events.Disconnected, () => {
- // Underlying browser did close for some reason - force disconnect the client.
- this.close({ code: 1001, reason: 'Browser closed' });
- });
- }
- browser.options.sdkLanguage = options.sdkLanguage;
-
- this._cleanups.push(async () => {
- // Don't close the pages so that user could debug them,
- // but close all the empty browsers and contexts to clean up.
- for (const browser of this._playwright.allBrowsers()) {
- for (const context of browser.contexts()) {
- if (!context.pages().length)
- await context.close({ reason: 'Connection terminated' });
- else
- await context.stopPendingOperations('Connection closed');
- }
- if (!browser.contexts())
- await browser.close({ reason: 'Connection terminated' });
+ const options = await initialize();
+ if (options.preLaunchedBrowser) {
+ const browser = options.preLaunchedBrowser;
+ browser.options.sdkLanguage = params.sdkLanguage;
+ browser.on(Browser.Events.Disconnected, () => {
+ // Underlying browser did close for some reason - force disconnect the client.
+ this.close({ code: 1001, reason: 'Browser closed' });
+ });
}
- });
+ if (options.preLaunchedAndroidDevice) {
+ const androidDevice = options.preLaunchedAndroidDevice;
+ androidDevice.on(AndroidDevice.Events.Close, () => {
+ // Underlying android device did close for some reason - force disconnect the client.
+ this.close({ code: 1001, reason: 'Android device disconnected' });
+ });
+ }
+ if (options.dispose)
+ this._cleanups.push(options.dispose);
- const playwrightDispatcher = new PlaywrightDispatcher(scope, this._playwright, { preLaunchedBrowser: browser, denyLaunch: true });
- return playwrightDispatcher;
- }
+ const dispatcher = new PlaywrightDispatcher(scope, playwright, options);
+ this._cleanups.push(() => dispatcher.cleanup());
- private async _createOwnedSocksProxy(): Promise<SocksProxy | undefined> {
- if (!this._options.socksProxyPattern) {
- this._options.launchOptions.socksProxyPort = undefined;
- return;
- }
- const socksProxy = new SocksProxy();
- socksProxy.setPattern(this._options.socksProxyPattern);
- this._options.launchOptions.socksProxyPort = await socksProxy.listen(0);
- debugLogger.log('server', `[${this._id}] started socks proxy on port ${this._options.launchOptions.socksProxyPort}`);
- this._cleanups.push(() => socksProxy.close());
- return socksProxy;
+ return dispatcher;
+ });
}
private async _onDisconnect(error?: Error) {
@@ -250,7 +113,7 @@ export class PlaywrightConnection {
for (const cleanup of this._cleanups)
await cleanup().catch(() => {});
await stopProfiling(this._profileName);
- this._onClose();
+ this._semaphore.release();
debugLogger.log('server', `[${this._id}] finished cleanup`);
}
@@ -275,47 +138,3 @@ export class PlaywrightConnection {
}
}
}
-
-function launchOptionsHash(options: LaunchOptions) {
- const copy = { ...options };
- for (const k of Object.keys(copy)) {
- const key = k as keyof LaunchOptions;
- if (copy[key] === defaultLaunchOptions[key])
- delete copy[key];
- }
- for (const key of optionsThatAllowBrowserReuse)
- delete copy[key];
- return JSON.stringify(copy);
-}
-
-function filterLaunchOptions(options: LaunchOptions, allowFSPaths: boolean): LaunchOptions {
- return {
- channel: options.channel,
- args: options.args,
- ignoreAllDefaultArgs: options.ignoreAllDefaultArgs,
- ignoreDefaultArgs: options.ignoreDefaultArgs,
- timeout: options.timeout,
- headless: options.headless,
- proxy: options.proxy,
- chromiumSandbox: options.chromiumSandbox,
- firefoxUserPrefs: options.firefoxUserPrefs,
- slowMo: options.slowMo,
- executablePath: (isUnderTest() || allowFSPaths) ? options.executablePath : undefined,
- downloadsPath: allowFSPaths ? options.downloadsPath : undefined,
- };
-}
-
-const defaultLaunchOptions: Partial<LaunchOptions> = {
- ignoreAllDefaultArgs: false,
- handleSIGINT: false,
- handleSIGTERM: false,
- handleSIGHUP: false,
- headless: true,
- devtools: false,
-};
-
-const optionsThatAllowBrowserReuse: (keyof LaunchOptions)[] = [
- 'headless',
- 'timeout',
- 'tracesDir',
-];
diff --git a/packages/playwright-core/src/remote/playwrightServer.ts b/packages/playwright-core/src/remote/playwrightServer.ts
index a6add53eb4..ced787c64b 100644
--- a/packages/playwright-core/src/remote/playwrightServer.ts
+++ b/packages/playwright-core/src/remote/playwrightServer.ts
@@ -21,9 +21,10 @@ import { DEFAULT_PLAYWRIGHT_LAUNCH_TIMEOUT } from '../utils/isomorphic/time';
import { WSServer } from '../server/utils/wsServer';
import { wrapInASCIIBox } from '../server/utils/ascii';
import { getPlaywrightVersion } from '../server/utils/userAgent';
+import { debugLogger, isUnderTest } from '../utils';
+import { serverSideCallMetadata } from '../server';
+import { SocksProxy } from '../server/utils/socksProxy';
-import type { ClientType } from './playwrightConnection';
-import type { SocksProxy } from '../server/utils/socksProxy';
import type { AndroidDevice } from '../server/android/android';
import type { Browser } from '../server/browser';
import type { Playwright } from '../server/playwright';
@@ -94,42 +95,166 @@ export class PlaywrightServer {
} catch (e) {
}
- // Instantiate playwright for the extension modes.
const isExtension = this._options.mode === 'extension';
- let clientType: ClientType = 'launch-browser';
- let semaphore: Semaphore = browserSemaphore;
- if (isExtension && url.searchParams.has('debug-controller')) {
- clientType = 'controller';
- semaphore = controllerSemaphore;
- } else if (isExtension) {
- clientType = 'reuse-browser';
- semaphore = reuseBrowserSemaphore;
- } else if (this._options.mode === 'launchServer' || this._options.mode === 'launchServerShared') {
- clientType = 'pre-launched-browser-or-android';
- semaphore = browserSemaphore;
+ const allowFSPaths = isExtension;
+ launchOptions = filterLaunchOptions(launchOptions, allowFSPaths);
+
+ if (isExtension) {
+ if (url.searchParams.has('debug-controller')) {
+ return new PlaywrightConnection(
+ controllerSemaphore,
+ ws,
+ true,
+ this._playwright,
+ async () => { throw new Error('shouldnt be used'); },
+ id,
+ );
+ }
+ return new PlaywrightConnection(
+ reuseBrowserSemaphore,
+ ws,
+ false,
+ this._playwright,
+ () => this._initReuseBrowsersMode(browserName, launchOptions, id),
+ id,
+ );
+ }
+
+ if (this._options.mode === 'launchServer' || this._options.mode === 'launchServerShared') {
+ if (this._options.preLaunchedBrowser) {
+ return new PlaywrightConnection(
+ browserSemaphore,
+ ws,
+ false,
+ this._playwright,
+ () => this._initPreLaunchedBrowserMode(id),
+ id,
+ );
+ }
+
+ return new PlaywrightConnection(
+ browserSemaphore,
+ ws,
+ false,
+ this._playwright,
+ () => this._initPreLaunchedAndroidMode(id),
+ id,
+ );
}
return new PlaywrightConnection(
- semaphore.acquire(),
- clientType, ws,
- {
- socksProxyPattern: proxyValue,
- browserName,
- launchOptions,
- allowFSPaths: this._options.mode === 'extension',
- sharedBrowser: this._options.mode === 'launchServerShared',
- },
+ browserSemaphore,
+ ws,
+ false,
this._playwright,
- {
- browser: this._options.preLaunchedBrowser,
- androidDevice: this._options.preLaunchedAndroidDevice,
- socksProxy: this._options.preLaunchedSocksProxy,
- },
- id, () => semaphore.release());
+ () => this._initLaunchBrowserMode(browserName, proxyValue, launchOptions, id),
+ id,
+ );
},
});
}
+ private async _initReuseBrowsersMode(browserName: string | null, launchOptions: LaunchOptions, id: string) {
+ // Note: reuse browser mode does not support socks proxy, because
+ // clients come and go, while the browser stays the same.
+
+ debugLogger.log('server', `[${id}] engaged reuse browsers mode for ${browserName}`);
+
+ const requestedOptions = launchOptionsHash(launchOptions);
+ let browser = this._playwright.allBrowsers().find(b => {
+ if (b.options.name !== browserName)
+ return false;
+ const existingOptions = launchOptionsHash(b.options.originalLaunchOptions);
+ return existingOptions === requestedOptions;
+ });
+
+ // Close remaining browsers of this type+channel. Keep different browser types for the speed.
+ for (const b of this._playwright.allBrowsers()) {
+ if (b === browser)
+ continue;
+ if (b.options.name === browserName && b.options.channel === launchOptions.channel)
+ await b.close({ reason: 'Connection terminated' });
+ }
+
+ if (!browser) {
+ browser = await this._playwright[(browserName || 'chromium') as 'chromium'].launch(serverSideCallMetadata(), {
+ ...launchOptions,
+ headless: !!process.env.PW_DEBUG_CONTROLLER_HEADLESS,
+ });
+ }
+
+ return {
+ preLaunchedBrowser: browser,
+ denyLaunch: true,
+ dispose: async () => {
+ // Don't close the pages so that user could debug them,
+ // but close all the empty browsers and contexts to clean up.
+ for (const browser of this._playwright.allBrowsers()) {
+ for (const context of browser.contexts()) {
+ if (!context.pages().length)
+ await context.close({ reason: 'Connection terminated' });
+ else
+ await context.stopPendingOperations('Connection closed');
+ }
+ if (!browser.contexts())
+ await browser.close({ reason: 'Connection terminated' });
+ }
+ }
+ };
+ }
+
+ private async _initPreLaunchedBrowserMode(id: string) {
+ debugLogger.log('server', `[${id}] engaged pre-launched (browser) mode`);
+
+ const browser = this._options.preLaunchedBrowser!;
+
+ // In pre-launched mode, keep only the pre-launched browser.
+ for (const b of this._playwright.allBrowsers()) {
+ if (b !== browser)
+ await b.close({ reason: 'Connection terminated' });
+ }
+
+ return {
+ preLaunchedBrowser: browser,
+ socksProxy: this._options.preLaunchedSocksProxy,
+ sharedBrowser: this._options.mode === 'launchServerShared',
+ denyLaunch: true,
+ };
+ }
+
+ private async _initPreLaunchedAndroidMode(id: string) {
+ debugLogger.log('server', `[${id}] engaged pre-launched (Android) mode`);
+ const androidDevice = this._options.preLaunchedAndroidDevice!;
+ return {
+ preLaunchedAndroidDevice: androidDevice,
+ denyLaunch: true,
+ };
+ }
+
+ private async _initLaunchBrowserMode(browserName: string | null, proxyValue: string | undefined, launchOptions: LaunchOptions, id: string) {
+ debugLogger.log('server', `[${id}] engaged launch mode for "${browserName}"`);
+ let socksProxy: SocksProxy | undefined;
+ if (proxyValue) {
+ socksProxy = new SocksProxy();
+ socksProxy.setPattern(proxyValue);
+ launchOptions.socksProxyPort = await socksProxy.listen(0);
+ debugLogger.log('server', `[${id}] started socks proxy on port ${launchOptions.socksProxyPort}`);
+ } else {
+ launchOptions.socksProxyPort = undefined;
+ }
+ const browser = await this._playwright[browserName as 'chromium'].launch(serverSideCallMetadata(), launchOptions);
+ return {
+ preLaunchedBrowser: browser,
+ socksProxy,
+ sharedBrowser: true,
+ denyLaunch: true,
+ dispose: async () => {
+ await browser.close({ reason: 'Connection terminated' });
+ socksProxy?.close();
+ },
+ };
+ }
+
async listen(port: number = 0, hostname?: string): Promise<string> {
return this._wsServer.listen(port, hostname, this._options.path);
}
@@ -163,3 +288,47 @@ function userAgentVersionMatchesErrorMessage(userAgent: string) {
].join('\n'), 1);
}
}
+
+function launchOptionsHash(options: LaunchOptions) {
+ const copy = { ...options };
+ for (const k of Object.keys(copy)) {
+ const key = k as keyof LaunchOptions;
+ if (copy[key] === defaultLaunchOptions[key])
+ delete copy[key];
+ }
+ for (const key of optionsThatAllowBrowserReuse)
+ delete copy[key];
+ return JSON.stringify(copy);
+}
+
+function filterLaunchOptions(options: LaunchOptions, allowFSPaths: boolean): LaunchOptions {
+ return {
+ channel: options.channel,
+ args: options.args,
+ ignoreAllDefaultArgs: options.ignoreAllDefaultArgs,
+ ignoreDefaultArgs: options.ignoreDefaultArgs,
+ timeout: options.timeout,
+ headless: options.headless,
+ proxy: options.proxy,
+ chromiumSandbox: options.chromiumSandbox,
+ firefoxUserPrefs: options.firefoxUserPrefs,
+ slowMo: options.slowMo,
+ executablePath: (isUnderTest() || allowFSPaths) ? options.executablePath : undefined,
+ downloadsPath: allowFSPaths ? options.downloadsPath : undefined,
+ };
+}
+
+const defaultLaunchOptions: Partial<LaunchOptions> = {
+ ignoreAllDefaultArgs: false,
+ handleSIGINT: false,
+ handleSIGTERM: false,
+ handleSIGHUP: false,
+ headless: true,
+ devtools: false,
+};
+
+const optionsThatAllowBrowserReuse: (keyof LaunchOptions)[] = [
+ 'headless',
+ 'timeout',
+ 'tracesDir',
+];
diff --git a/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts b/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts
index 04bb144d11..a80aa49456 100644
--- a/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts
+++ b/packages/playwright-core/src/server/dispatchers/playwrightDispatcher.ts
@@ -35,7 +35,7 @@ import type { Browser } from '../browser';
import type { Playwright } from '../playwright';
import type * as channels from '@protocol/channels';
-type PlaywrightDispatcherOptions = {
+export type PlaywrightDispatcherOptions = {
socksProxy?: SocksProxy;
denyLaunch?: boolean;
preLaunchedBrowser?: Browser;