|
| 1 | +// Adapted from paranext-core/e2e-tests/fixtures/app.fixture.ts |
| 2 | +import { |
| 3 | + test as base, |
| 4 | + ElectronApplication, |
| 5 | + Page, |
| 6 | + TestInfo, |
| 7 | + ConsoleMessage, |
| 8 | +} from '@playwright/test'; |
| 9 | +import { |
| 10 | + launchElectronWithExtension, |
| 11 | + teardownElectronApp, |
| 12 | + ElectronAppContext, |
| 13 | + PROCESS_READY_TIMEOUT, |
| 14 | +} from './helpers'; |
| 15 | + |
| 16 | +export { expect } from '@playwright/test'; |
| 17 | + |
| 18 | +/** Worker-scoped fixtures — one instance shared across all tests in a worker. */ |
| 19 | +export interface WorkerAppFixtures { |
| 20 | + electronApp: ElectronApplication; |
| 21 | +} |
| 22 | + |
| 23 | +/** Test-scoped fixtures — re-created for every test. */ |
| 24 | +export interface TestAppFixtures { |
| 25 | + mainPage: Page; |
| 26 | +} |
| 27 | + |
| 28 | +export const test = base.extend<TestAppFixtures, WorkerAppFixtures>({ |
| 29 | + // Worker-scoped: the Electron process is launched once per worker and shared across all tests, |
| 30 | + // avoiding the process startup/teardown cost per test. |
| 31 | + electronApp: [ |
| 32 | + // eslint-disable-next-line no-empty-pattern |
| 33 | + async ({}, use) => { |
| 34 | + const ctx: ElectronAppContext = await launchElectronWithExtension(); |
| 35 | + |
| 36 | + await use(ctx.electronApp); |
| 37 | + |
| 38 | + console.log('[teardown] Worker-scoped app teardown starting...'); |
| 39 | + await teardownElectronApp(ctx); |
| 40 | + console.log('[teardown] Worker-scoped app teardown complete — worker will exit now'); |
| 41 | + }, |
| 42 | + { scope: 'worker' }, |
| 43 | + ], |
| 44 | + |
| 45 | + mainPage: async ({ electronApp }, use, testInfo: TestInfo) => { |
| 46 | + const page = await electronApp.firstWindow({ timeout: PROCESS_READY_TIMEOUT }); |
| 47 | + |
| 48 | + console.log(`Window URL: ${page.url()}`); |
| 49 | + const onPageError = (err: Error) => console.error(`Page error: ${err.message}`); |
| 50 | + const onConsoleMsg = (msg: ConsoleMessage) => { |
| 51 | + if (msg.type() === 'error') console.error(`Console error: ${msg.text()}`); |
| 52 | + }; |
| 53 | + page.on('pageerror', onPageError); |
| 54 | + page.on('console', onConsoleMsg); |
| 55 | + |
| 56 | + await page.waitForLoadState('domcontentloaded'); |
| 57 | + await page.waitForSelector('#root', { state: 'attached', timeout: PROCESS_READY_TIMEOUT }); |
| 58 | + |
| 59 | + await use(page); |
| 60 | + |
| 61 | + page.off('pageerror', onPageError); |
| 62 | + page.off('console', onConsoleMsg); |
| 63 | + |
| 64 | + if (testInfo.status !== testInfo.expectedStatus) { |
| 65 | + const screenshotPath = testInfo.outputPath('failure.png'); |
| 66 | + try { |
| 67 | + await page.screenshot({ path: screenshotPath, fullPage: true }); |
| 68 | + await testInfo.attach('failure-screenshot', { |
| 69 | + path: screenshotPath, |
| 70 | + contentType: 'image/png', |
| 71 | + }); |
| 72 | + console.log(`Failure screenshot saved to ${screenshotPath}`); |
| 73 | + } catch { |
| 74 | + console.warn('Could not capture failure screenshot (window may already be closed)'); |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | +}); |
0 commit comments