Skip to content

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

feat(webkit): support executablePath for the webkit-wsl channel (#41325)

Yury Semikhatskyb77f5d79e130
diff --git a/packages/playwright-core/src/server/browserType.ts b/packages/playwright-core/src/server/browserType.ts
index 3dd0d51969..0e0119c45c 100644
--- a/packages/playwright-core/src/server/browserType.ts
+++ b/packages/playwright-core/src/server/browserType.ts
@@ -152,7 +152,6 @@ export abstract class BrowserType extends SdkObject {
ignoreDefaultArgs,
ignoreAllDefaultArgs,
args = [],
- executablePath = null,
} = options;
const tempDirectories: string[] = [];
let artifactsDir: string;
@@ -183,10 +182,9 @@ export abstract class BrowserType extends SdkObject {
browserArguments.push(...await this.defaultArgs(options, isPersistent, userDataDir));
let executable: string;
- if (executablePath) {
- if (!(await existsAsync(executablePath)))
- throw new Error(`Failed to launch ${this._name} because executable doesn't exist at ${executablePath}`);
- executable = executablePath;
+ const customExecutablePath = await this.resolveExecutablePath(options);
+ if (customExecutablePath) {
+ executable = customExecutablePath;
} else {
const registryExecutable = registry.findExecutable(this.getExecutableName(options));
if (!registryExecutable || registryExecutable.browserName !== this._name)
@@ -345,6 +343,15 @@ export abstract class BrowserType extends SdkObject {
return options.channel || this._name;
}
+ protected async resolveExecutablePath(options: types.LaunchOptions): Promise<string | undefined> {
+ const { executablePath } = options;
+ if (!executablePath)
+ return undefined;
+ if (!(await existsAsync(executablePath)))
+ throw new Error(`Failed to launch ${this._name} because executable doesn't exist at ${executablePath}`);
+ return executablePath;
+ }
+
abstract defaultArgs(options: types.LaunchOptions, isPersistent: boolean, userDataDir: string): Promise<string[]>;
abstract connectToTransport(transport: ConnectionTransport, options: BrowserOptions, browserLogsCollector: RecentLogsCollector): Promise<Browser>;
abstract amendEnvironment(env: NodeJS.ProcessEnv, userDataDir: string, isPersistent: boolean, options: types.LaunchOptions): NodeJS.ProcessEnv;
diff --git a/packages/playwright-core/src/server/webkit/webkit.ts b/packages/playwright-core/src/server/webkit/webkit.ts
index 1b90404a29..62b4bcca4b 100644
--- a/packages/playwright-core/src/server/webkit/webkit.ts
+++ b/packages/playwright-core/src/server/webkit/webkit.ts
@@ -65,6 +65,16 @@ export class WebKit extends BrowserType {
return options.channel !== 'webkit-wsl';
}
+ override async resolveExecutablePath(options: types.LaunchOptions): Promise<string | undefined> {
+ if (options.channel !== 'webkit-wsl')
+ return super.resolveExecutablePath(options);
+ // executablePath points inside the WSL distribution and is consumed in defaultArgs; the
+ // host command is wsl.exe from the registry.
+ if (options.executablePath && !await wslPathExists(options.executablePath))
+ throw new Error(`Failed to launch webkit because executable doesn't exist at ${options.executablePath}`);
+ return undefined;
+ }
+
override async waitForReadyState(options: types.LaunchOptions, browserLogsCollector: RecentLogsCollector): Promise<{ wsEndpoint?: string }> {
if (options.channel !== 'webkit-wsl')
return {};
@@ -99,15 +109,13 @@ export class WebKit extends BrowserType {
const webkitArguments = [isWSL ? '--remote-debugging-port=0' : '--inspector-pipe'];
if (isWSL) {
- if (options.executablePath)
- throw new Error('Cannot specify executablePath when using the "webkit-wsl" channel.');
- // The actual command is `wsl.exe -- <linux executable> <browser args>`.
+ const wslExecutablePath = options.executablePath || registry.findExecutable('webkit-wsl')!.wslExecutablePath!;
webkitArguments.unshift(
'-d', kWSLDistribution,
'-u', kWSLUser,
'--cd', kWSLHome,
'--',
- registry.findExecutable('webkit-wsl')!.wslExecutablePath!,
+ wslExecutablePath,
);
}
@@ -148,3 +156,8 @@ export async function translatePathToWSL(path: string): Promise<string> {
const { stdout } = await spawnAsync('wsl.exe', ['-d', kWSLDistribution, '--cd', kWSLHome, 'wslpath', path.replace(/\\/g, '\\\\')]);
return stdout.toString().trim();
}
+
+async function wslPathExists(wslPath: string): Promise<boolean> {
+ const { code } = await spawnAsync('wsl.exe', ['-d', kWSLDistribution, '-u', kWSLUser, '--', 'test', '-e', wslPath]);
+ return code === 0;
+}