From 191de117665bec16d1eb03e2452b5e20f853ec4f Mon Sep 17 00:00:00 2001 From: Katerina Zakimatova Date: Wed, 15 Nov 2023 17:23:08 +0100 Subject: [PATCH 1/4] feat(test-kit): save browser screenshots of failed tests --- packages/test-kit/src/with-feature.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index d1d9ecb1f..a976a3086 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -366,6 +366,18 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { }, ); } + dispose( + async () => { + const ctx = mochaCtx(); + + if (ctx?.currentTest?.state === 'failed') { + const screenshotDirPath = `${process.cwd()}/screenshots-of-failed`; + const testPath = ctx.currentTest.titlePath().join('/'); + await featurePage.screenshot({ path: `${screenshotDirPath}/${testPath}.png` }); + } + }, + { timeout: 3_000 }, + ); function onPageError(e: Error) { if ( From 9b9bb459bbe94d2b9f49061f4dc836a70160d79f Mon Sep 17 00:00:00 2001 From: Katerina Zakimatova Date: Thu, 16 Nov 2023 15:17:59 +0100 Subject: [PATCH 2/4] improve screenshots output --- packages/test-kit/src/with-feature.ts | 46 ++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index a976a3086..b0f75d497 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -110,6 +110,10 @@ export interface IWithFeatureOptions extends Omit; + /** + * Take screenshots of failed tests, file name is the test title and the file path is the output folder + the test's titlePath + */ + takeScreenshotsOfFailed?: boolean | Pick; /** * Keeps the page open and the feature running for the all the tests in the suite */ @@ -191,6 +195,7 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { devtools = envDebugMode ? debugMode : undefined, slowMo, persist, + takeScreenshotsOfFailed = true, } = withFeatureOptions; if ( @@ -331,14 +336,14 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { const suiteTracingOptions = typeof suiteTracing === 'boolean' ? {} : suiteTracing; const testTracingOptions = typeof tracing === 'boolean' ? {} : tracing; + const combinedTrancingOptions = { + screenshots: true, + snapshots: true, + outPath: process.cwd(), + ...suiteTracingOptions, + ...testTracingOptions, + }; if (testTracingOptions) { - const combinedTrancingOptions = { - screenshots: true, - snapshots: true, - outPath: process.cwd(), - ...suiteTracingOptions, - ...testTracingOptions, - }; const { screenshots, snapshots, name, outPath } = combinedTrancingOptions; await browserContext.tracing.start({ screenshots, snapshots }); tracingDisposables.add((testName) => { @@ -366,18 +371,23 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { }, ); } - dispose( - async () => { - const ctx = mochaCtx(); + if (takeScreenshotsOfFailed) { + const outPath = + (typeof takeScreenshotsOfFailed !== 'boolean' && takeScreenshotsOfFailed.outPath) || + `${combinedTrancingOptions.outPath}/screenshots-of-failed`; - if (ctx?.currentTest?.state === 'failed') { - const screenshotDirPath = `${process.cwd()}/screenshots-of-failed`; - const testPath = ctx.currentTest.titlePath().join('/'); - await featurePage.screenshot({ path: `${screenshotDirPath}/${testPath}.png` }); - } - }, - { timeout: 3_000 }, - ); + dispose( + async () => { + const ctx = mochaCtx(); + + if (ctx?.currentTest?.state === 'failed') { + const testPath = ctx.currentTest.titlePath().join('/'); + await featurePage.screenshot({ path: `${outPath}/${testPath}.png` }); + } + }, + { timeout: 3_000 }, + ); + } function onPageError(e: Error) { if ( From 09b788c69bd6da0f7b516e6abb0ef171458953bc Mon Sep 17 00:00:00 2001 From: Katerina Zakimatova Date: Thu, 23 Nov 2023 11:24:59 +0100 Subject: [PATCH 3/4] fix: generate hash to ensure unique test screenshot path --- packages/test-kit/src/with-feature.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index b0f75d497..817b3734d 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -4,8 +4,10 @@ import type { PerformanceMetrics } from '@wixc3/engine-runtime-node'; import { Disposables, type DisposableItem, type DisposableOptions } from '@wixc3/patterns'; import { createDisposalGroup, disposeAfter, mochaCtx } from '@wixc3/testing'; import { DISPOSE_OF_TEMP_DIRS } from '@wixc3/testing-node'; +import { uniqueHash } from '@wixc3/engine-scripts'; import isCI from 'is-ci'; import playwright from 'playwright-core'; +import { reporters } from 'mocha'; import { ForkedProcessApplication } from './forked-process-application.js'; import { hookPageConsole } from './hook-page-console.js'; import { normalizeTestName } from './normalize-test-name.js'; @@ -381,8 +383,13 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { const ctx = mochaCtx(); if (ctx?.currentTest?.state === 'failed') { - const testPath = ctx.currentTest.titlePath().join('/'); - await featurePage.screenshot({ path: `${outPath}/${testPath}.png` }); + const testPath = ctx.currentTest.titlePath().join('/').replace(/\s/g, '-'); + const filePath = `${outPath}/${testPath}__${uniqueHash()}.png`; + await featurePage.screenshot({ path: filePath }); + + console.log( + reporters.Base.color('bright yellow', `The screenshot has been saved at ${filePath}`), + ); } }, { timeout: 3_000 }, From 5a803c7fb17b49441153aa0242c732dbd6021453 Mon Sep 17 00:00:00 2001 From: Katerina Zakimatova Date: Mon, 27 Nov 2023 13:13:35 +0100 Subject: [PATCH 4/4] improve naming and docs --- packages/test-kit/src/with-feature.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index af897eeb2..adc2d79c7 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -114,7 +114,7 @@ export interface IWithFeatureOptions extends Omit; /** - * Take screenshots of failed tests, file name is the test title and the file path is the output folder + the test's titlePath + * Take screenshots of failed tests, file name is the test title + hash, the file path is the output folder + the test's titlePath */ takeScreenshotsOfFailed?: boolean | Pick; /** @@ -388,7 +388,7 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { if (takeScreenshotsOfFailed) { const outPath = (typeof takeScreenshotsOfFailed !== 'boolean' && takeScreenshotsOfFailed.outPath) || - `${combinedTrancingOptions.outPath}/screenshots-of-failed`; + `${combinedTrancingOptions.outPath}/screenshots-of-failed-tests`; dispose( async () => {