Skip to content

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

feat(electron): support tracesDir option (#23947)

Dmitry Gozman1ab99fe1b8fc
diff --git a/docs/src/api/class-electron.md b/docs/src/api/class-electron.md
index cf1fa06fd6..b21d8573b0 100644
--- a/docs/src/api/class-electron.md
+++ b/docs/src/api/class-electron.md
@@ -142,3 +142,6 @@ Maximum time in milliseconds to wait for the application to start. Defaults to `
### option: Electron.launch.timezoneId = %%-context-option-timezoneid-%%
* since: v1.12
+
+### option: Electron.launch.tracesDir = %%-browser-option-tracesdir-%%
+* since: v1.36
diff --git a/packages/playwright-core/src/client/electron.ts b/packages/playwright-core/src/client/electron.ts
index 2a4d2d7f3e..9442d61576 100644
--- a/packages/playwright-core/src/client/electron.ts
+++ b/packages/playwright-core/src/client/electron.ts
@@ -51,6 +51,7 @@ export class Electron extends ChannelOwner<channels.ElectronChannel> implements
const params: channels.ElectronLaunchParams = {
...await prepareBrowserContextParams(options),
env: envObjectToArray(options.env ? options.env : process.env),
+ tracesDir: options.tracesDir,
};
const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
app._context._options = params;
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index c7aa380f7f..575784da91 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -2240,6 +2240,7 @@ scheme.ElectronLaunchParams = tObject({
})),
strictSelectors: tOptional(tBoolean),
timezoneId: tOptional(tString),
+ tracesDir: tOptional(tString),
});
scheme.ElectronLaunchResult = tObject({
electronApplication: tChannel(['ElectronApplication']),
diff --git a/packages/playwright-core/src/server/electron/electron.ts b/packages/playwright-core/src/server/electron/electron.ts
index 9bc6353b04..07369d5991 100644
--- a/packages/playwright-core/src/server/electron/electron.ts
+++ b/packages/playwright-core/src/server/electron/electron.ts
@@ -231,7 +231,7 @@ export class Electron extends SdkObject {
browserLogsCollector,
artifactsDir,
downloadsPath: artifactsDir,
- tracesDir: artifactsDir,
+ tracesDir: options.tracesDir || artifactsDir,
originalLaunchOptions: {},
};
validateBrowserContextOptions(contextOptions, browserOptions);
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 6aefe158a9..bcfbeaac7c 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -16995,6 +16995,11 @@ export interface Electron {
* for a list of supported timezone IDs. Defaults to the system timezone.
*/
timezoneId?: string;
+
+ /**
+ * If specified, traces are saved into this directory.
+ */
+ tracesDir?: string;
}): Promise<ElectronApplication>;
}
diff --git a/packages/protocol/src/channels.ts b/packages/protocol/src/channels.ts
index d008c2c005..1dbc4e19fb 100644
--- a/packages/protocol/src/channels.ts
+++ b/packages/protocol/src/channels.ts
@@ -4015,6 +4015,7 @@ export type ElectronLaunchParams = {
},
strictSelectors?: boolean,
timezoneId?: string,
+ tracesDir?: string,
};
export type ElectronLaunchOptions = {
executablePath?: string,
@@ -4049,6 +4050,7 @@ export type ElectronLaunchOptions = {
},
strictSelectors?: boolean,
timezoneId?: string,
+ tracesDir?: string,
};
export type ElectronLaunchResult = {
electronApplication: ElectronApplicationChannel,
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index 12eed0c925..b6178f6050 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -3176,6 +3176,7 @@ Electron:
height: number
strictSelectors: boolean?
timezoneId: string?
+ tracesDir: string?
returns:
electronApplication: ElectronApplication
diff --git a/tests/electron/electron-tracing.spec.ts b/tests/electron/electron-tracing.spec.ts
index 59dc2f840c..91ebab78c9 100644
--- a/tests/electron/electron-tracing.spec.ts
+++ b/tests/electron/electron-tracing.spec.ts
@@ -15,9 +15,10 @@
*/
import { electronTest as test, expect } from './electronTest';
+import fs from 'fs';
+import path from 'path';
test.skip(({ trace }) => trace === 'on');
-// test.slow();
test('should record trace', async ({ newWindow, server, runAndTrace }) => {
const traceViewer = await runAndTrace(async () => {
@@ -45,3 +46,17 @@ test('should support custom protocol', async ({ electronApp, newWindow, server,
await expect(frame.locator('button')).toHaveCSS('color', 'rgb(255, 0, 0)');
await expect(frame.locator('button')).toHaveCSS('font-weight', '700');
});
+
+test('should respect tracesDir and name', async ({ launchElectronApp, server }, testInfo) => {
+ const tracesDir = testInfo.outputPath('traces');
+ const electronApp = await launchElectronApp('electron-window-app.js', [], { tracesDir });
+
+ await electronApp.context().tracing.start({ name: 'name1', snapshots: true });
+ const page = await electronApp.firstWindow();
+ await page.goto(server.PREFIX + '/one-style.html');
+ await electronApp.context().tracing.stopChunk({ path: testInfo.outputPath('trace1.zip') });
+ expect(fs.existsSync(path.join(tracesDir, 'name1.trace'))).toBe(true);
+ expect(fs.existsSync(path.join(tracesDir, 'name1.network'))).toBe(true);
+
+ await electronApp.close();
+});