Skip to content

Commit

Permalink
E2E: Try to take a screenshot if the app fails to start
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Yen <mark.yen@suse.com>
  • Loading branch information
mook-as committed Oct 10, 2024
1 parent 9c4941f commit 9e3c531
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions e2e/utils/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ export async function teardownApp(app: ElectronApplication) {
}
}

export async function teardown(app: ElectronApplication, testInfo: TestInfo) {
export async function teardown(app: ElectronApplication | undefined, testInfo: TestInfo) {
if (!app) {
// This can happen if we failed to start up.
return;
}

const context = app.context();
const { file: filename } = testInfo;

Expand Down Expand Up @@ -384,11 +389,17 @@ export async function startRancherDesktop(testInfo: TestInfo, options: startRanc
if (options?.timeout) {
launchOptions.timeout = options?.timeout;
}
const electronApp = await _electron.launch(launchOptions);

await electronApp.context().tracing.start({ screenshots: true, snapshots: true });
try {
const electronApp = await _electron.launch(launchOptions);

await electronApp.context().tracing.start({ screenshots: true, snapshots: true });

return electronApp;
return electronApp;
} catch (ex) {
await takeScreenShot(path.join(reportAsset(testInfo, 'log'), 'screeshot.png'));
throw ex;
}
}

export async function startSlowerDesktop(testInfo: TestInfo, defaultSettings: RecursivePartial<Settings> = {}): Promise<[ElectronApplication, Page]> {
Expand All @@ -403,3 +414,24 @@ export async function startSlowerDesktop(testInfo: TestInfo, defaultSettings: Re

return [electronApp, page];
}

async function takeScreenShot(outPath: string) {
try {
switch (process.platform) {
case 'darwin':
await childProcess.spawnFile('screencapture', ['-m', outPath]);
break;
case 'win32': {
const script = path.resolve(__dirname, '..', '..', 'screenshots', 'screenshot.ps1');

await childProcess.spawnFile('powershell.exe', [script, '-FilePath', outPath]);
break;
}
default:
await childProcess.spawnFile('import', [outPath]);
break;
}
} catch (ex) {
console.error(`Error taking screenshot: ${ ex }`);
}
}

0 comments on commit 9e3c531

Please sign in to comment.