Skip to content

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

test: isolate Electron userDataDir per-test (#35235)

Max Schmitt16468e65bd71
diff --git a/tests/electron/electron-app.js b/tests/electron/electron-app.js
index 0e68871199..da709b60a1 100644
--- a/tests/electron/electron-app.js
+++ b/tests/electron/electron-app.js
@@ -1,6 +1,10 @@
+const assert = require('node:assert/strict');
const { app, protocol } = require('electron');
const path = require('path');
+assert(process.env.PWTEST_ELECTRON_USER_DATA_DIR, 'PWTEST_ELECTRON_USER_DATA_DIR env var is not set');
+app.setPath('appData', process.env.PWTEST_ELECTRON_USER_DATA_DIR);
+
app.on('window-all-closed', e => e.preventDefault());
app.whenReady().then(() => {
diff --git a/tests/electron/electronTest.ts b/tests/electron/electronTest.ts
index c418236f55..b2105017a0 100644
--- a/tests/electron/electronTest.ts
+++ b/tests/electron/electronTest.ts
@@ -15,17 +15,21 @@
*/
import { baseTest } from '../config/baseTest';
-import * as path from 'path';
+import path from 'path';
+import fs from 'fs';
+import os from 'os';
import type { ElectronApplication, Page, Electron } from '@playwright/test';
import type { PageTestFixtures, PageWorkerFixtures } from '../page/pageTestApi';
import type { TraceViewerFixtures } from '../config/traceViewerFixtures';
import { traceViewerFixtures } from '../config/traceViewerFixtures';
+import { removeFolders } from '../../packages/playwright-core/lib/server/utils/fileUtils';
export { expect } from '@playwright/test';
type ElectronTestFixtures = PageTestFixtures & {
electronApp: ElectronApplication;
launchElectronApp: (appFile: string, args?: string[], options?: Parameters<Electron['launch']>[0]) => Promise<ElectronApplication>;
newWindow: () => Promise<Page>;
+ createUserDataDir: () => Promise<string>;
};
export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixtures).extend<ElectronTestFixtures, PageWorkerFixtures>({
@@ -37,12 +41,32 @@ export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixt
isWebView2: [false, { scope: 'worker' }],
isHeadlessShell: [false, { scope: 'worker' }],
- launchElectronApp: async ({ playwright }, use) => {
+ createUserDataDir: async ({ mode }, run) => {
+ const dirs: string[] = [];
+ // We do not put user data dir in testOutputPath,
+ // because we do not want to upload them as test result artifacts.
+ await run(async () => {
+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-test-'));
+ dirs.push(dir);
+ return dir;
+ });
+ await removeFolders(dirs);
+ },
+
+ launchElectronApp: async ({ playwright, createUserDataDir }, use) => {
// This env prevents 'Electron Security Policy' console message.
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
const apps: ElectronApplication[] = [];
await use(async (appFile: string, args: string[] = [], options?: Parameters<Electron['launch']>[0]) => {
- const app = await playwright._electron.launch({ ...options, args: [path.join(__dirname, appFile), ...args] });
+ const userDataDir = await createUserDataDir();
+ const app = await playwright._electron.launch({
+ ...options,
+ args: [path.join(__dirname, appFile), ...args],
+ env: {
+ ...process.env,
+ PWTEST_ELECTRON_USER_DATA_DIR: userDataDir,
+ }
+ });
apps.push(app);
return app;
});