Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test-kit): save browser screenshots of failed tests #2144

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions packages/test-kit/src/with-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -110,6 +112,10 @@ export interface IWithFeatureOptions extends Omit<IFeatureExecutionOptions, 'tra
* add tracing for the entire suite, the name of the test will be used as the zip name
*/
tracing?: boolean | Omit<Tracing, 'name'>;
/**
zetmate marked this conversation as resolved.
Show resolved Hide resolved
* 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<Tracing, 'outPath'>;
/**
* Keeps the page open and the feature running for the all the tests in the suite
*/
Expand Down Expand Up @@ -191,6 +197,7 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) {
devtools = envDebugMode ? debugMode : undefined,
slowMo,
persist,
takeScreenshotsOfFailed = true,
} = withFeatureOptions;

if (
Expand Down Expand Up @@ -331,14 +338,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) => {
Expand Down Expand Up @@ -366,6 +373,28 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) {
},
);
}
if (takeScreenshotsOfFailed) {
const outPath =
(typeof takeScreenshotsOfFailed !== 'boolean' && takeScreenshotsOfFailed.outPath) ||
`${combinedTrancingOptions.outPath}/screenshots-of-failed`;
zetmate marked this conversation as resolved.
Show resolved Hide resolved

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 },
);
}

function onPageError(e: Error) {
if (
Expand Down