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): add artifactsDir option to electron.launch() (#39880)

Yury Semikhatskyd67f31e9570f
diff --git a/docs/src/api/class-electron.md b/docs/src/api/class-electron.md
index 90c41de84e..1df55a214a 100644
--- a/docs/src/api/class-electron.md
+++ b/docs/src/api/class-electron.md
@@ -140,5 +140,8 @@ Maximum time in milliseconds to wait for the application to start. Defaults to `
### option: Electron.launch.tracesDir = %%-browser-option-tracesdir-%%
* since: v1.36
+### option: Electron.launch.artifactsDir = %%-browser-option-artifactsdir-%%
+* since: v1.59
+
### option: Electron.launch.chromiumSandbox = %%-browser-option-chromiumsandbox-%%
* since: v1.59
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index f565b8a04f..8fc4de177c 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -19817,6 +19817,13 @@ export interface Electron {
*/
args?: Array<string>;
+ /**
+ * If specified, artifacts (traces, videos, downloads, HAR files, etc.) are saved into this directory. The directory
+ * is not cleaned up when the browser closes. If not specified, a temporary directory is used and cleaned up when the
+ * browser closes.
+ */
+ artifactsDir?: string;
+
/**
* Toggles bypassing page's Content-Security-Policy. Defaults to `false`.
*/
diff --git a/packages/playwright-core/src/client/electron.ts b/packages/playwright-core/src/client/electron.ts
index ed6505e24d..2399aa61ff 100644
--- a/packages/playwright-core/src/client/electron.ts
+++ b/packages/playwright-core/src/client/electron.ts
@@ -61,6 +61,7 @@ export class Electron extends ChannelOwner<channels.ElectronChannel> implements
...await prepareBrowserContextParams(this._platform, options),
env: envObjectToArray(options.env ? options.env : this._platform.env),
tracesDir: options.tracesDir,
+ artifactsDir: options.artifactsDir,
timeout: new TimeoutSettings(this._platform).launchTimeout(options),
};
const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.ts
index c940c7ed6e..032aa64018 100644
--- a/packages/playwright-core/src/protocol/validator.ts
+++ b/packages/playwright-core/src/protocol/validator.ts
@@ -2689,6 +2689,7 @@ scheme.ElectronLaunchParams = tObject({
strictSelectors: tOptional(tBoolean),
timezoneId: tOptional(tString),
tracesDir: tOptional(tString),
+ artifactsDir: tOptional(tString),
selectorEngines: tOptional(tArray(tType('SelectorEngine'))),
testIdAttributeName: tOptional(tString),
});
diff --git a/packages/playwright-core/src/server/electron/electron.ts b/packages/playwright-core/src/server/electron/electron.ts
index 9df97f097a..173ca898d0 100644
--- a/packages/playwright-core/src/server/electron/electron.ts
+++ b/packages/playwright-core/src/server/electron/electron.ts
@@ -164,7 +164,14 @@ export class Electron extends SdkObject {
electronArguments.unshift('--no-sandbox');
}
- const artifactsDir = await progress.race(fs.promises.mkdtemp(ARTIFACTS_FOLDER));
+ let artifactsDir: string;
+ const tempDirectories: string[] = [];
+ if (options.artifactsDir) {
+ artifactsDir = options.artifactsDir;
+ } else {
+ artifactsDir = await progress.race(fs.promises.mkdtemp(ARTIFACTS_FOLDER));
+ tempDirectories.push(artifactsDir);
+ }
const browserLogsCollector = new RecentLogsCollector();
const env = options.env ? envArrayToObject(options.env) : process.env;
@@ -216,7 +223,7 @@ export class Electron extends SdkObject {
shell,
stdio: 'pipe',
cwd: options.cwd,
- tempDirectories: [artifactsDir],
+ tempDirectories,
attemptToGracefullyClose: () => app!.close(),
handleSIGINT: true,
handleSIGTERM: true,
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index f565b8a04f..8fc4de177c 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -19817,6 +19817,13 @@ export interface Electron {
*/
args?: Array<string>;
+ /**
+ * If specified, artifacts (traces, videos, downloads, HAR files, etc.) are saved into this directory. The directory
+ * is not cleaned up when the browser closes. If not specified, a temporary directory is used and cleaned up when the
+ * browser closes.
+ */
+ artifactsDir?: string;
+
/**
* Toggles bypassing page's Content-Security-Policy. Defaults to `false`.
*/
diff --git a/packages/protocol/src/channels.d.ts b/packages/protocol/src/channels.d.ts
index 4c4ee1e205..4bae179f31 100644
--- a/packages/protocol/src/channels.d.ts
+++ b/packages/protocol/src/channels.d.ts
@@ -4686,6 +4686,7 @@ export type ElectronLaunchParams = {
strictSelectors?: boolean,
timezoneId?: string,
tracesDir?: string,
+ artifactsDir?: string,
selectorEngines?: SelectorEngine[],
testIdAttributeName?: string,
};
@@ -4723,6 +4724,7 @@ export type ElectronLaunchOptions = {
strictSelectors?: boolean,
timezoneId?: string,
tracesDir?: string,
+ artifactsDir?: string,
selectorEngines?: SelectorEngine[],
testIdAttributeName?: string,
};
diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.yml
index f603e63edb..642b0d0e8e 100644
--- a/packages/protocol/src/protocol.yml
+++ b/packages/protocol/src/protocol.yml
@@ -4184,6 +4184,7 @@ Electron:
strictSelectors: boolean?
timezoneId: string?
tracesDir: string?
+ artifactsDir: string?
selectorEngines:
type: array?
items: SelectorEngine
diff --git a/tests/electron/electron-app.spec.ts b/tests/electron/electron-app.spec.ts
index a4393b53d1..7fa8f8950a 100644
--- a/tests/electron/electron-app.spec.ts
+++ b/tests/electron/electron-app.spec.ts
@@ -339,3 +339,29 @@ test('should report downloads', async ({ launchElectronApp, electronMajorVersion
expect(fs.readFileSync(path).toString()).toBe('Hello world');
await app.close();
});
+
+test('should save downloads to artifactsDir', async ({ launchElectronApp, electronMajorVersion, server }, testInfo) => {
+ server.setRoute('/download', (req, res) => {
+ res.setHeader('Content-Type', 'application/octet-stream');
+ res.setHeader('Content-Disposition', 'attachment');
+ res.end(`Hello world`);
+ });
+
+ const artifactsDir = testInfo.outputPath('artifacts');
+ const app = await launchElectronApp('electron-window-app.js', [], {
+ acceptDownloads: true,
+ artifactsDir,
+ });
+ const window = await app.firstWindow();
+ await window.setContent(`<a href="${server.PREFIX}/download">download</a>`);
+ const [download] = await Promise.all([
+ window.waitForEvent('download'),
+ window.click('a')
+ ]);
+ const downloadPath = await download.path();
+ expect(downloadPath.startsWith(artifactsDir)).toBeTruthy();
+ expect(fs.existsSync(downloadPath)).toBeTruthy();
+ await app.close();
+ // User-provided artifactsDir should not be cleaned up.
+ expect(fs.existsSync(artifactsDir)).toBeTruthy();
+});