Skip to content

Commit

Permalink
feat(e2e): Electron test videos are not added to reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Vere-Grey committed Dec 11, 2024
1 parent a319202 commit 463c038
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
25 changes: 20 additions & 5 deletions packages/suite-desktop-core/e2e/support/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { _electron as electron, TestInfo } from '@playwright/test';
import path from 'path';
import fse from 'fs-extra';
import { readdirSync, removeSync } from 'fs-extra';

import { TrezorUserEnvLink } from '@trezor/trezor-user-env-link';

Expand All @@ -19,6 +19,7 @@ type LaunchSuiteParams = {
bridgeDaemon?: boolean;
locale?: string;
colorScheme?: 'light' | 'dark' | 'no-preference' | null | undefined;
videoFolder?: string;
};

const formatErrorLogMessage = (data: string) => {
Expand Down Expand Up @@ -54,19 +55,20 @@ export const launchSuiteElectronApp = async (params: LaunchSuiteParams = {}) =>
],
colorScheme: params.colorScheme,
locale: params.locale,
// when testing electron, video needs to be setup like this. it works locally but not in docker
// recordVideo: { dir: 'test-results' },
...(params.videoFolder && {
recordVideo: { dir: params.videoFolder, size: { width: 1280, height: 720 } },
}),
});

const localDataDir = await electronApp.evaluate(({ app }) => app.getPath('userData'));

if (options.rmUserData) {
const filesToDelete = fse.readdirSync(localDataDir);
const filesToDelete = readdirSync(localDataDir);
filesToDelete.forEach(file => {
// omitting Cache folder it sometimes prevents the deletion and is not necessary to delete for test idempotency
if (file !== 'Cache') {
try {
fse.removeSync(`${localDataDir}/${file}`);
removeSync(`${localDataDir}/${file}`);
} catch {
// If files does not exist do nothing.
}
Expand Down Expand Up @@ -113,3 +115,16 @@ export const getApiUrl = (webBaseUrl: string | undefined, testInfo: TestInfo) =>

return apiURL;
};

export const getElectronVideoPath = (videoFolder: string) => {
const videoFilenames = readdirSync(videoFolder).filter(file => file.endsWith('.webm'));
if (videoFilenames.length > 1) {
console.error(
formatErrorLogMessage(
`Warning: More than one electron video file found in the output directory: ${videoFolder}\nAttaching only the first one: ${videoFilenames[0]}`,
),
);
}

return path.join(videoFolder, videoFilenames[0]);
};
26 changes: 11 additions & 15 deletions packages/suite-desktop-core/e2e/support/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { test as base, ElectronApplication, Page } from '@playwright/test';
import { existsSync } from 'node:fs';

import {
SetupEmu,
Expand All @@ -10,7 +9,7 @@ import {
} from '@trezor/trezor-user-env-link';

import { DashboardActions } from './pageActions/dashboardActions';
import { getApiUrl, launchSuite } from './common';
import { getApiUrl, getElectronVideoPath, launchSuite } from './common';
import { SettingsActions } from './pageActions/settingsActions';
import { SuiteGuide } from './pageActions/suiteGuideActions';
import { WalletActions } from './pageActions/walletActions';
Expand Down Expand Up @@ -67,7 +66,11 @@ const test = base.extend<Fixtures>({
}

if (testInfo.project.name === PlaywrightProjects.Desktop) {
const suite = await launchSuite({ locale, colorScheme });
const suite = await launchSuite({
locale,
colorScheme,
videoFolder: testInfo.outputDir,
});
await use(suite.electronApp);
await suite.electronApp.close(); // Ensure cleanup after tests
} else {
Expand All @@ -90,18 +93,11 @@ const test = base.extend<Fixtures>({
path: tracePath,
contentType: 'application/zip',
});

// Attach video if it exists
const videoPath = `${testInfo.outputDir}/${testInfo.title}`;
console.error('videoPath', videoPath);
console.error('exists?', existsSync(videoPath));
if (existsSync(videoPath)) {
testInfo.attachments.push({
name: 'video',
path: videoPath,
contentType: 'video/webm',
});
}
testInfo.attachments.push({
name: 'video',
path: getElectronVideoPath(testInfo.outputDir),
contentType: 'video/webm',
});
} else {
await page.context().addInitScript(() => {
// Tells the app to attach Redux Store to window object. packages/suite-web/src/support/useCypress.ts
Expand Down

0 comments on commit 463c038

Please sign in to comment.