From f5b31455246549162e9e73a38212dbd0e5c44043 Mon Sep 17 00:00:00 2001 From: Barak Date: Wed, 29 Nov 2023 16:40:19 +0200 Subject: [PATCH 1/2] fix: takeScreenshotsOfFailed --- packages/test-kit/src/with-feature.ts | 64 +++++++++++++-------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index 322640263..84d16e657 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -281,17 +281,16 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { const afterEachDisposables = new Set<() => Promise>(); if (persist) { - after('dispose suite level page', async function () { - this.timeout(disposables.list().totalTimeout); - await disposables.dispose(); - }); const disposables = new Disposables(); disposables.registerGroup(DISPOSE_OF_TEMP_DIRS, { after: 'default' }); disposables.registerGroup(WITH_FEATURE_DISPOSABLES, { after: 'default', before: DISPOSE_OF_TEMP_DIRS }); disposables.registerGroup(PAGE_DISPOSABLES, { before: WITH_FEATURE_DISPOSABLES }); disposables.registerGroup(TRACING_DISPOSABLES, { before: PAGE_DISPOSABLES }); - dispose = (disposable: DisposableItem, options?: DisposableOptions) => disposables.add(disposable, options); + after('dispose suite level page', async function () { + this.timeout(disposables.list().totalTimeout); + await disposables.dispose(); + }); } else { afterEach('dispose all', async function () { this.timeout(20_000); @@ -393,37 +392,36 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { }); const featurePage = await dedicatedBrowserContext.newPage(); - afterEachDisposables.add(() => featurePage.close()); + + afterEachDisposables.add(async () => { + await featurePage.close(); + + if (takeScreenshotsOfFailed) { + const suiteTracingOptions = typeof suiteTracing === 'boolean' ? {} : suiteTracing; + const testTracingOptions = typeof tracing === 'boolean' ? {} : tracing; + const outPath = + (typeof takeScreenshotsOfFailed !== 'boolean' && takeScreenshotsOfFailed.outPath) || + `${ + suiteTracingOptions?.outPath ?? testTracingOptions?.outPath ?? process.cwd() + }/screenshots-of-failed-tests`; + + const ctx = mochaCtx(); + + if (ctx?.currentTest?.state === 'failed') { + 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}`), + ); + } + } + }); + const fullFeatureUrl = (buildFlow ? runningFeature.url : featureUrl) + search; const response = await featurePage.goto(fullFeatureUrl, navigationOptions); - if (takeScreenshotsOfFailed) { - const suiteTracingOptions = typeof suiteTracing === 'boolean' ? {} : suiteTracing; - const testTracingOptions = typeof tracing === 'boolean' ? {} : tracing; - const outPath = - (typeof takeScreenshotsOfFailed !== 'boolean' && takeScreenshotsOfFailed.outPath) || - `${ - suiteTracingOptions?.outPath ?? testTracingOptions?.outPath ?? process.cwd() - }/screenshots-of-failed-tests`; - - dispose( - async () => { - const ctx = mochaCtx(); - - if (ctx?.currentTest?.state === 'failed') { - 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 }, - ); - } - return { page: featurePage, response, From ac475aaa6eec650b76a117cda3505b2b3982688b Mon Sep 17 00:00:00 2001 From: Barak Date: Wed, 29 Nov 2023 16:47:02 +0200 Subject: [PATCH 2/2] fix: dispose page after screen shot --- packages/test-kit/src/with-feature.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/test-kit/src/with-feature.ts b/packages/test-kit/src/with-feature.ts index 84d16e657..b464c26a1 100644 --- a/packages/test-kit/src/with-feature.ts +++ b/packages/test-kit/src/with-feature.ts @@ -394,8 +394,6 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { const featurePage = await dedicatedBrowserContext.newPage(); afterEachDisposables.add(async () => { - await featurePage.close(); - if (takeScreenshotsOfFailed) { const suiteTracingOptions = typeof suiteTracing === 'boolean' ? {} : suiteTracing; const testTracingOptions = typeof tracing === 'boolean' ? {} : tracing; @@ -417,6 +415,7 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) { ); } } + await featurePage.close(); }); const fullFeatureUrl = (buildFlow ? runningFeature.url : featureUrl) + search;