Skip to content

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

chore: lift up playwright prelaunch (#36330)

Simon Knott66e903021290
diff --git a/packages/playwright-core/src/remote/playwrightConnection.ts b/packages/playwright-core/src/remote/playwrightConnection.ts
index 1b56d45698..f96eded914 100644
--- a/packages/playwright-core/src/remote/playwrightConnection.ts
+++ b/packages/playwright-core/src/remote/playwrightConnection.ts
@@ -15,7 +15,7 @@
*/
import { SocksProxy } from '../server/utils/socksProxy';
-import { DispatcherConnection, PlaywrightDispatcher, RootDispatcher, createPlaywright } from '../server';
+import { DispatcherConnection, PlaywrightDispatcher, RootDispatcher } from '../server';
import { AndroidDevice } from '../server/android/android';
import { Browser } from '../server/browser';
import { DebugControllerDispatcher } from '../server/dispatchers/debugControllerDispatcher';
@@ -42,7 +42,6 @@ type Options = {
};
type PreLaunched = {
- playwright?: Playwright | undefined;
browser?: Browser | undefined;
androidDevice?: AndroidDevice | undefined;
socksProxy?: SocksProxy | undefined;
@@ -55,18 +54,18 @@ export class PlaywrightConnection {
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, preLaunched: PreLaunched, id: string, onClose: () => void) {
+ constructor(lock: Promise<void>, clientType: ClientType, ws: WebSocket, options: Options, playwright: Playwright, preLaunched: PreLaunched, id: string, onClose: () => void) {
this._ws = ws;
+ this._playwright = playwright;
this._preLaunched = preLaunched;
this._options = options;
options.launchOptions = filterLaunchOptions(options.launchOptions, options.allowFSPaths);
- if (clientType === 'reuse-browser' || clientType === 'pre-launched-browser-or-android')
- assert(preLaunched.playwright);
if (clientType === 'pre-launched-browser-or-android')
assert(preLaunched.browser || preLaunched.androidDevice);
this._onClose = onClose;
@@ -118,8 +117,6 @@ export class PlaywrightConnection {
private async _initLaunchBrowserMode(scope: RootDispatcher, options: channels.RootInitializeParams) {
debugLogger.log('server', `[${this._id}] engaged launch mode for "${this._options.browserName}"`);
- const playwright = createPlaywright({ sdkLanguage: options.sdkLanguage, isServer: true });
-
const ownedSocksProxy = await this._createOwnedSocksProxy();
let browserName = this._options.browserName;
if ('bidi' === browserName) {
@@ -128,7 +125,7 @@ export class PlaywrightConnection {
else
browserName = 'bidiChromium';
}
- const browser = await playwright[browserName as 'chromium'].launch(serverSideCallMetadata(), this._options.launchOptions);
+ const browser = await this._playwright[browserName as 'chromium'].launch(serverSideCallMetadata(), this._options.launchOptions);
browser.options.sdkLanguage = options.sdkLanguage;
this._cleanups.push(() => browser.close({ reason: 'Connection terminated' }));
@@ -137,12 +134,11 @@ export class PlaywrightConnection {
this.close({ code: 1001, reason: 'Browser closed' });
});
- return new PlaywrightDispatcher(scope, playwright, { socksProxy: ownedSocksProxy, preLaunchedBrowser: browser, denyLaunch: true, });
+ 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`);
- const playwright = this._preLaunched.playwright!;
// Note: connected client owns the socks proxy and configures the pattern.
this._preLaunched.socksProxy?.setPattern(this._options.socksProxyPattern);
@@ -154,14 +150,14 @@ export class PlaywrightConnection {
this.close({ code: 1001, reason: 'Browser closed' });
});
- const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, {
+ 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 playwright.allBrowsers()) {
+ for (const b of this._playwright.allBrowsers()) {
if (b !== browser)
await b.close({ reason: 'Connection terminated' });
}
@@ -171,22 +167,20 @@ export class PlaywrightConnection {
private async _initPreLaunchedAndroidMode(scope: RootDispatcher) {
debugLogger.log('server', `[${this._id}] engaged pre-launched (Android) mode`);
- const playwright = this._preLaunched.playwright!;
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, playwright, { preLaunchedAndroidDevice: androidDevice, denyLaunch: true });
+ 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`);
- const playwright = this._preLaunched.playwright!;
// Always create new instance based on the reused Playwright instance.
- return new DebugControllerDispatcher(this._dispatcherConnection, playwright.debugController);
+ return new DebugControllerDispatcher(this._dispatcherConnection, this._playwright.debugController);
}
private async _initReuseBrowsersMode(scope: RootDispatcher, options: channels.RootInitializeParams) {
@@ -194,10 +188,9 @@ export class PlaywrightConnection {
// clients come and go, while the browser stays the same.
debugLogger.log('server', `[${this._id}] engaged reuse browsers mode for ${this._options.browserName}`);
- const playwright = this._preLaunched.playwright!;
const requestedOptions = launchOptionsHash(this._options.launchOptions);
- let browser = playwright.allBrowsers().find(b => {
+ let browser = this._playwright.allBrowsers().find(b => {
if (b.options.name !== this._options.browserName)
return false;
const existingOptions = launchOptionsHash(b.options.originalLaunchOptions);
@@ -205,7 +198,7 @@ export class PlaywrightConnection {
});
// Close remaining browsers of this type+channel. Keep different browser types for the speed.
- for (const b of playwright.allBrowsers()) {
+ 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)
@@ -213,7 +206,7 @@ export class PlaywrightConnection {
}
if (!browser) {
- browser = await playwright[(this._options.browserName || 'chromium') as 'chromium'].launch(serverSideCallMetadata(), {
+ browser = await this._playwright[(this._options.browserName || 'chromium') as 'chromium'].launch(serverSideCallMetadata(), {
...this._options.launchOptions,
headless: !!process.env.PW_DEBUG_CONTROLLER_HEADLESS,
});
@@ -227,7 +220,7 @@ export class PlaywrightConnection {
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 playwright.allBrowsers()) {
+ for (const browser of this._playwright.allBrowsers()) {
for (const context of browser.contexts()) {
if (!context.pages().length)
await context.close({ reason: 'Connection terminated' });
@@ -239,7 +232,7 @@ export class PlaywrightConnection {
}
});
- const playwrightDispatcher = new PlaywrightDispatcher(scope, playwright, { preLaunchedBrowser: browser, denyLaunch: true });
+ const playwrightDispatcher = new PlaywrightDispatcher(scope, this._playwright, { preLaunchedBrowser: browser, denyLaunch: true });
return playwrightDispatcher;
}
diff --git a/packages/playwright-core/src/remote/playwrightServer.ts b/packages/playwright-core/src/remote/playwrightServer.ts
index 44b204a962..a6add53eb4 100644
--- a/packages/playwright-core/src/remote/playwrightServer.ts
+++ b/packages/playwright-core/src/remote/playwrightServer.ts
@@ -40,16 +40,17 @@ type ServerOptions = {
};
export class PlaywrightServer {
- private _preLaunchedPlaywright: Playwright | undefined;
+ private _playwright: Playwright;
private _options: ServerOptions;
private _wsServer: WSServer;
constructor(options: ServerOptions) {
this._options = options;
if (options.preLaunchedBrowser)
- this._preLaunchedPlaywright = options.preLaunchedBrowser.attribution.playwright;
+ this._playwright = options.preLaunchedBrowser.attribution.playwright;
if (options.preLaunchedAndroidDevice)
- this._preLaunchedPlaywright = options.preLaunchedAndroidDevice._android.attribution.playwright;
+ this._playwright = options.preLaunchedAndroidDevice._android.attribution.playwright;
+ this._playwright ??= createPlaywright({ sdkLanguage: 'javascript', isServer: true });
const browserSemaphore = new Semaphore(this._options.maxConnections);
const controllerSemaphore = new Semaphore(1);
@@ -95,11 +96,6 @@ export class PlaywrightServer {
// Instantiate playwright for the extension modes.
const isExtension = this._options.mode === 'extension';
- if (isExtension) {
- if (!this._preLaunchedPlaywright)
- this._preLaunchedPlaywright = createPlaywright({ sdkLanguage: 'javascript', isServer: true });
- }
-
let clientType: ClientType = 'launch-browser';
let semaphore: Semaphore = browserSemaphore;
if (isExtension && url.searchParams.has('debug-controller')) {
@@ -123,8 +119,8 @@ export class PlaywrightServer {
allowFSPaths: this._options.mode === 'extension',
sharedBrowser: this._options.mode === 'launchServerShared',
},
+ this._playwright,
{
- playwright: this._preLaunchedPlaywright,
browser: this._options.preLaunchedBrowser,
androidDevice: this._options.preLaunchedAndroidDevice,
socksProxy: this._options.preLaunchedSocksProxy,