Skip to content

Commit

Permalink
feat: enable traces for non-failure screenshots
Browse files Browse the repository at this point in the history
Traces are now attached to all screenshots not only failure screenshots.

It allows to have enhanced visual testing debugging for visual
differences.
  • Loading branch information
gregberge committed Jun 7, 2024
1 parent 0b8d5b4 commit fcad44c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/playwright/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class ArgosReporter implements Reporter {
this.createUploadDirPromise = null;
}

/**
* Write a file to the temporary directory.
*/
async writeFile(path: string, body: Buffer | string) {
const uploadDir = await this.getUploadDir();
const dir = dirname(path);
Expand All @@ -79,6 +82,9 @@ class ArgosReporter implements Reporter {
debug(`File written to ${path}`);
}

/**
* Copy a file to the temporary directory.
*/
async copyFile(from: string, to: string) {
const uploadDir = await this.getUploadDir();
const dir = dirname(to);
Expand All @@ -90,6 +96,16 @@ class ArgosReporter implements Reporter {
debug(`File copied from ${from} to ${to}`);
}

/**
* Copy the trace file if found in the result.
*/
async copyTraceIfFound(result: TestResult, path: string) {
const trace = result.attachments.find(checkIsTrace) ?? null;
if (trace) {
await this.copyFile(trace.path, path + ".pw-trace.zip");
}
}

getAutomaticScreenshotName(test: TestCase, result: TestResult) {
let name = test.titlePath().join(" ");
name += result.retry > 0 ? ` #${result.retry + 1}` : "";
Expand Down Expand Up @@ -122,20 +138,22 @@ class ArgosReporter implements Reporter {
checkIsArgosScreenshotMetadata(attachment)
) {
const path = join(uploadDir, getAttachmentFilename(attachment.name));
await this.copyFile(attachment.path, path);
await Promise.all([
this.copyFile(attachment.path, path),
this.copyTraceIfFound(result, path),
]);
return;
}

// Error screenshots are sent to Argos
if (checkIsAutomaticScreenshot(attachment)) {
const trace = result.attachments.find(checkIsTrace) ?? null;
const metadata = await getMetadataFromTestCase(test, result);
const name = this.getAutomaticScreenshotName(test, result);
const path = join(uploadDir, `${name}.png`);
await Promise.all([
this.writeFile(path + ".argos.json", JSON.stringify(metadata)),
this.copyFile(attachment.path, path),
trace ? this.copyFile(trace.path, path + ".pw-trace.zip") : null,
this.copyTraceIfFound(result, path),
]);
return;
}
Expand Down

0 comments on commit fcad44c

Please sign in to comment.