Skip to content

Commit 27ca863

Browse files
--wip-- [skip ci] test
1 parent 072b977 commit 27ca863

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

packages/playwright/src/index.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
type Benchmark,
1010
type BenchmarkStats,
1111
} from "@codspeed/core";
12+
import { createRequire } from "node:module";
13+
import { pathToFileURL } from "node:url";
1214
import { _electron as electron } from "playwright";
1315
import type { ElectronApplication, Page } from "playwright-core";
1416

@@ -52,20 +54,18 @@ export interface BenchOptions {
5254
*/
5355
appPath: string;
5456
/**
55-
* Additional CLI flags forwarded to Electron.
57+
* CLI flags forwarded to Electron.
5658
*/
5759
electronArgs?: string[];
5860
/**
5961
* Working directory for the Electron process. Defaults to `process.cwd()`.
6062
*/
6163
cwd?: string;
6264
/**
63-
* Absolute path to the Electron executable. When omitted, Playwright resolves
64-
* it via `require("electron")`. Set this when running under package managers
65-
* (e.g. pnpm) where Playwright cannot resolve `electron` from its own
66-
* directory.
65+
* Absolute path to the Electron executable. When omitted, it is resolved from
66+
* the `electron` package in `cwd`. Set this only to override that default.
6767
*/
68-
executablePath?: string;
68+
electronExecutablePath?: string;
6969
/**
7070
* Run before each round, after the window opens. Use it to bring the app to
7171
* a steady state (initial render done, data loaded, …). Not measured.
@@ -106,15 +106,32 @@ function resolveRounds(optionRounds: number | undefined): number {
106106
return n;
107107
}
108108

109+
/**
110+
* Resolve the path to the Electron binary.
111+
*
112+
* Playwright resolves Electron via `require("electron/index.js")` from inside
113+
* its own package directory. Under isolated installs (e.g. pnpm), Playwright
114+
* cannot see the project's `electron` dependency and bails out with
115+
* "Electron executablePath not found!". We resolve it ourselves from the
116+
* benchmark's working directory, where `electron` is a real dependency.
117+
*/
118+
function resolveElectronExecutable(cwd: string): string {
119+
const require = createRequire(pathToFileURL(`${cwd}/`));
120+
// `electron`'s main module exports the absolute path to its binary.
121+
return require("electron") as string;
122+
}
123+
109124
async function launchApp(options: BenchOptions): Promise<ElectronApplication> {
125+
const cwd = options.cwd ?? process.cwd();
110126
return electron.launch({
111127
args: [
112128
options.appPath,
113129
...(options.electronArgs ?? []),
114130
`--js-flags=${DEFAULT_PROFILING_JS_FLAGS}`,
115131
],
116-
cwd: options.cwd ?? process.cwd(),
117-
executablePath: options.executablePath,
132+
cwd,
133+
executablePath:
134+
options.electronExecutablePath ?? resolveElectronExecutable(cwd),
118135
});
119136
}
120137

0 commit comments

Comments
 (0)