|
1 |
| -import { test as baseTest, ElectronApplication, Page } from '@playwright/test'; |
| 1 | +import { test as baseTest, BrowserContext, ElectronApplication, Page } from '@playwright/test'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as fs from 'fs'; |
2 | 5 |
|
3 |
| -const { startApp } = require('./electron.ts'); |
| 6 | +const electronAppPath = path.join(__dirname, '../packages/bruno-electron'); |
4 | 7 |
|
5 |
| -export const test = baseTest.extend<{ page: Page }, { electronApp: ElectronApplication }>({ |
6 |
| - electronApp: [ |
| 8 | +export const test = baseTest.extend< |
| 9 | + { |
| 10 | + context: BrowserContext; |
| 11 | + page: Page; |
| 12 | + newPage: Page; |
| 13 | + pageWithUserData: Page; |
| 14 | + }, |
| 15 | + { |
| 16 | + createTmpDir: (tag?: string) => Promise<string>; |
| 17 | + launchElectronApp: (options?: { initUserDataPath?: string }) => Promise<ElectronApplication>; |
| 18 | + electronApp: ElectronApplication; |
| 19 | + reuseOrLaunchElectronApp: (options?: { initUserDataPath?: string }) => Promise<ElectronApplication>; |
| 20 | + } |
| 21 | +>({ |
| 22 | + createTmpDir: [ |
7 | 23 | async ({}, use) => {
|
8 |
| - const { app: electronApp, context } = await startApp(); |
| 24 | + const dirs: string[] = []; |
| 25 | + await use(async (tag?: string) => { |
| 26 | + const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), `pw-${tag || ''}-`)); |
| 27 | + dirs.push(dir); |
| 28 | + return dir; |
| 29 | + }); |
| 30 | + await Promise.all( |
| 31 | + dirs.map((dir) => fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 10 }).catch((e) => e)) |
| 32 | + ); |
| 33 | + }, |
| 34 | + { scope: 'worker' } |
| 35 | + ], |
| 36 | + |
| 37 | + launchElectronApp: [ |
| 38 | + async ({ playwright, createTmpDir }, use, workerInfo) => { |
| 39 | + const apps: ElectronApplication[] = []; |
| 40 | + await use(async ({ initUserDataPath } = {}) => { |
| 41 | + const userDataPath = await createTmpDir('electron-userdata'); |
| 42 | + |
| 43 | + if (initUserDataPath) { |
| 44 | + const replacements = { |
| 45 | + projectRoot: path.join(__dirname, '..') |
| 46 | + }; |
9 | 47 |
|
10 |
| - await use(electronApp); |
11 |
| - await context.close(); |
12 |
| - await electronApp.close(); |
| 48 | + for (const file of await fs.promises.readdir(initUserDataPath)) { |
| 49 | + let content = await fs.promises.readFile(path.join(initUserDataPath, file), 'utf-8'); |
| 50 | + content = content.replace(/{{(\w+)}}/g, (_, key) => { |
| 51 | + if (replacements[key]) { |
| 52 | + return replacements[key]; |
| 53 | + } else { |
| 54 | + throw new Error(`\tNo replacement for {{${key}}} in ${path.join(initUserDataPath, file)}`); |
| 55 | + } |
| 56 | + }); |
| 57 | + await fs.promises.writeFile(path.join(userDataPath, file), content, 'utf-8'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const app = await playwright._electron.launch({ |
| 62 | + args: [electronAppPath], |
| 63 | + env: { |
| 64 | + ELECTRON_USER_DATA_PATH: userDataPath |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + const { workerIndex } = workerInfo; |
| 69 | + app.process().stdout.on('data', (data) => { |
| 70 | + process.stdout.write(data.toString().replace(/^(?=.)/gm, `[Electron #${workerIndex}] |`)); |
| 71 | + }); |
| 72 | + app.process().stderr.on('data', (error) => { |
| 73 | + process.stderr.write(error.toString().replace(/^(?=.)/gm, `[Electron #${workerIndex}] |`)); |
| 74 | + }); |
| 75 | + |
| 76 | + apps.push(app); |
| 77 | + return app; |
| 78 | + }); |
| 79 | + for (const app of apps) { |
| 80 | + await app.context().close(); |
| 81 | + await app.close(); |
| 82 | + } |
13 | 83 | },
|
14 | 84 | { scope: 'worker' }
|
15 | 85 | ],
|
16 |
| - page: async ({ electronApp }, use) => { |
| 86 | + |
| 87 | + electronApp: [ |
| 88 | + async ({ launchElectronApp }, use) => { |
| 89 | + const app = await launchElectronApp(); |
| 90 | + await use(app); |
| 91 | + }, |
| 92 | + { scope: 'worker' } |
| 93 | + ], |
| 94 | + |
| 95 | + context: async ({ electronApp }, use, testInfo) => { |
| 96 | + const context = await electronApp.context(); |
| 97 | + const tracingOptions = (testInfo as any)._tracing.traceOptions(); |
| 98 | + if (tracingOptions) { |
| 99 | + try { |
| 100 | + await context.tracing.start({ screenshots: true, snapshots: true, sources: true }); |
| 101 | + } catch (e) {} |
| 102 | + } |
| 103 | + await use(context); |
| 104 | + }, |
| 105 | + |
| 106 | + page: async ({ electronApp, context }, use, testInfo) => { |
17 | 107 | const page = await electronApp.firstWindow();
|
18 |
| - await use(page); |
19 |
| - await page.reload(); |
| 108 | + const tracingOptions = (testInfo as any)._tracing.traceOptions(); |
| 109 | + if (tracingOptions) { |
| 110 | + const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`); |
| 111 | + await context.tracing.startChunk(); |
| 112 | + await use(page); |
| 113 | + await context.tracing.stopChunk({ path: tracePath }); |
| 114 | + await testInfo.attach('trace', { path: tracePath }); |
| 115 | + } else { |
| 116 | + await use(page); |
| 117 | + } |
| 118 | + }, |
| 119 | + |
| 120 | + newPage: async ({ launchElectronApp }, use, testInfo) => { |
| 121 | + const app = await launchElectronApp(); |
| 122 | + const context = await app.context(); |
| 123 | + const page = await app.firstWindow(); |
| 124 | + const tracingOptions = (testInfo as any)._tracing.traceOptions(); |
| 125 | + if (tracingOptions) { |
| 126 | + const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`); |
| 127 | + await context.tracing.start({ screenshots: true, snapshots: true, sources: true }); |
| 128 | + await use(page); |
| 129 | + await context.tracing.stop({ path: tracePath }); |
| 130 | + await testInfo.attach('trace', { path: tracePath }); |
| 131 | + } else { |
| 132 | + await use(page); |
| 133 | + } |
| 134 | + }, |
| 135 | + |
| 136 | + reuseOrLaunchElectronApp: [ |
| 137 | + async ({ launchElectronApp }, use, testInfo) => { |
| 138 | + const apps: Record<string, ElectronApplication> = {}; |
| 139 | + await use(async ({ initUserDataPath } = {}) => { |
| 140 | + const key = initUserDataPath; |
| 141 | + if (key && apps[key]) { |
| 142 | + return apps[key]; |
| 143 | + } |
| 144 | + const app = await launchElectronApp({ initUserDataPath }); |
| 145 | + apps[key] = app; |
| 146 | + return app; |
| 147 | + }); |
| 148 | + }, |
| 149 | + { scope: 'worker' } |
| 150 | + ], |
| 151 | + |
| 152 | + pageWithUserData: async ({ reuseOrLaunchElectronApp }, use, testInfo) => { |
| 153 | + const testDir = path.dirname(testInfo.file); |
| 154 | + const initUserDataPath = path.join(testDir, 'init-user-data'); |
| 155 | + |
| 156 | + const app = await reuseOrLaunchElectronApp( |
| 157 | + (await fs.promises.stat(initUserDataPath).catch(() => false)) ? { initUserDataPath } : {} |
| 158 | + ); |
| 159 | + |
| 160 | + const context = await app.context(); |
| 161 | + const page = await app.firstWindow(); |
| 162 | + const tracingOptions = (testInfo as any)._tracing.traceOptions(); |
| 163 | + if (tracingOptions) { |
| 164 | + const tracePath = testInfo.outputPath(`trace-${testInfo.testId}.zip`); |
| 165 | + try { |
| 166 | + await context.tracing.start({ screenshots: true, snapshots: true, sources: true }); |
| 167 | + } catch (e) {} |
| 168 | + await context.tracing.startChunk(); |
| 169 | + await use(page); |
| 170 | + await context.tracing.stopChunk({ path: tracePath }); |
| 171 | + await testInfo.attach('trace', { path: tracePath }); |
| 172 | + } else { |
| 173 | + await use(page); |
| 174 | + } |
20 | 175 | }
|
21 | 176 | });
|
22 | 177 |
|
23 |
| -export * from '@playwright/test' |
| 178 | +export * from '@playwright/test'; |
0 commit comments