From 463c038c1c9a30622c5891dc7f80b77db5195302 Mon Sep 17 00:00:00 2001 From: Martin Vere Cihlar Date: Wed, 11 Dec 2024 13:44:20 +0100 Subject: [PATCH] feat(e2e): Electron test videos are not added to reports --- .../suite-desktop-core/e2e/support/common.ts | 25 ++++++++++++++---- .../e2e/support/fixtures.ts | 26 ++++++++----------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/suite-desktop-core/e2e/support/common.ts b/packages/suite-desktop-core/e2e/support/common.ts index e3b6637415e..e02d2d80958 100644 --- a/packages/suite-desktop-core/e2e/support/common.ts +++ b/packages/suite-desktop-core/e2e/support/common.ts @@ -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'; @@ -19,6 +19,7 @@ type LaunchSuiteParams = { bridgeDaemon?: boolean; locale?: string; colorScheme?: 'light' | 'dark' | 'no-preference' | null | undefined; + videoFolder?: string; }; const formatErrorLogMessage = (data: string) => { @@ -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. } @@ -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]); +}; diff --git a/packages/suite-desktop-core/e2e/support/fixtures.ts b/packages/suite-desktop-core/e2e/support/fixtures.ts index c155b156069..ec935a7af32 100644 --- a/packages/suite-desktop-core/e2e/support/fixtures.ts +++ b/packages/suite-desktop-core/e2e/support/fixtures.ts @@ -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, @@ -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'; @@ -67,7 +66,11 @@ const test = base.extend({ } 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 { @@ -90,18 +93,11 @@ const test = base.extend({ 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