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

This commit is contained in:
Max Schmitt 2025-03-17 19:41:44 +01:00 committed by GitHub
parent 0baa973207
commit 16468e65bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 3 deletions

View File

@ -1,6 +1,10 @@
const assert = require('node:assert/strict');
const { app, protocol } = require('electron'); const { app, protocol } = require('electron');
const path = require('path'); 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.on('window-all-closed', e => e.preventDefault());
app.whenReady().then(() => { app.whenReady().then(() => {

View File

@ -15,17 +15,21 @@
*/ */
import { baseTest } from '../config/baseTest'; 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 { ElectronApplication, Page, Electron } from '@playwright/test';
import type { PageTestFixtures, PageWorkerFixtures } from '../page/pageTestApi'; import type { PageTestFixtures, PageWorkerFixtures } from '../page/pageTestApi';
import type { TraceViewerFixtures } from '../config/traceViewerFixtures'; import type { TraceViewerFixtures } from '../config/traceViewerFixtures';
import { traceViewerFixtures } from '../config/traceViewerFixtures'; import { traceViewerFixtures } from '../config/traceViewerFixtures';
import { removeFolders } from '../../packages/playwright-core/lib/server/utils/fileUtils';
export { expect } from '@playwright/test'; export { expect } from '@playwright/test';
type ElectronTestFixtures = PageTestFixtures & { type ElectronTestFixtures = PageTestFixtures & {
electronApp: ElectronApplication; electronApp: ElectronApplication;
launchElectronApp: (appFile: string, args?: string[], options?: Parameters<Electron['launch']>[0]) => Promise<ElectronApplication>; launchElectronApp: (appFile: string, args?: string[], options?: Parameters<Electron['launch']>[0]) => Promise<ElectronApplication>;
newWindow: () => Promise<Page>; newWindow: () => Promise<Page>;
createUserDataDir: () => Promise<string>;
}; };
export const electronTest = baseTest.extend<TraceViewerFixtures>(traceViewerFixtures).extend<ElectronTestFixtures, PageWorkerFixtures>({ 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' }], isWebView2: [false, { scope: 'worker' }],
isHeadlessShell: [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. // This env prevents 'Electron Security Policy' console message.
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'; process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
const apps: ElectronApplication[] = []; const apps: ElectronApplication[] = [];
await use(async (appFile: string, args: string[] = [], options?: Parameters<Electron['launch']>[0]) => { 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); apps.push(app);
return app; return app;
}); });