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

fix: takeScreenshotsOfFailed #2175

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Changes from all 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
63 changes: 30 additions & 33 deletions packages/test-kit/src/with-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,16 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) {
const afterEachDisposables = new Set<() => Promise<void>>();

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);
Expand Down Expand Up @@ -393,37 +392,35 @@ export function withFeature(withFeatureOptions: IWithFeatureOptions = {}) {
});

const featurePage = await dedicatedBrowserContext.newPage();
afterEachDisposables.add(() => featurePage.close());

afterEachDisposables.add(async () => {
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}`),
);
}
}
await featurePage.close();
});

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,
Expand Down