From 1aa13b94a03a82e5b5049d969146a626d8cc50cd Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 1 Oct 2024 10:37:34 +0200 Subject: [PATCH] feat(tracing): tag trace attachments with content type parameter --- packages/playwright/src/reporters/base.ts | 2 +- packages/playwright/src/worker/testTracing.ts | 2 +- .../trace-viewer/src/ui/workbenchLoader.tsx | 2 +- .../reporter-attachment.spec.ts | 24 ++++++++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/playwright/src/reporters/base.ts b/packages/playwright/src/reporters/base.ts index 6776ce606f..2fc2ee9b85 100644 --- a/packages/playwright/src/reporters/base.ts +++ b/packages/playwright/src/reporters/base.ts @@ -312,7 +312,7 @@ export function formatFailure(config: FullConfig, test: TestCase, options: {inde const relativePath = path.relative(process.cwd(), attachment.path); resultLines.push(colors.cyan(` ${relativePath}`)); // Make this extensible - if (attachment.name === 'trace') { + if (attachment.name === 'trace' || attachment.contentType === 'application/zip;content=playwright-trace') { const packageManagerCommand = getPackageManagerExecCommand(); resultLines.push(colors.cyan(` Usage:`)); resultLines.push(''); diff --git a/packages/playwright/src/worker/testTracing.ts b/packages/playwright/src/worker/testTracing.ts index fed7fdde7e..243b65a330 100644 --- a/packages/playwright/src/worker/testTracing.ts +++ b/packages/playwright/src/worker/testTracing.ts @@ -216,7 +216,7 @@ export class TestTracing { const tracePath = this._testInfo.outputPath('trace.zip'); await mergeTraceFiles(tracePath, this._temporaryTraceFiles); - this._testInfo.attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip' }); + this._testInfo.attachments.push({ name: 'trace', path: tracePath, contentType: 'application/zip;content=playwright-trace' }); } appendForError(error: TestInfoError) { diff --git a/packages/trace-viewer/src/ui/workbenchLoader.tsx b/packages/trace-viewer/src/ui/workbenchLoader.tsx index 19793d4b57..1fc0afc44a 100644 --- a/packages/trace-viewer/src/ui/workbenchLoader.tsx +++ b/packages/trace-viewer/src/ui/workbenchLoader.tsx @@ -63,7 +63,7 @@ export const WorkbenchLoader: React.FunctionComponent<{ if (!e.clipboardData?.files.length) return; for (const file of e.clipboardData.files) { - if (file.type !== 'application/zip') + if (file.type !== 'application/zip' && file.type !== 'application/zip;content=playwright-trace') return; } e.preventDefault(); diff --git a/tests/playwright-test/reporter-attachment.spec.ts b/tests/playwright-test/reporter-attachment.spec.ts index 50d6d1ee0c..4715d4c56e 100644 --- a/tests/playwright-test/reporter-attachment.spec.ts +++ b/tests/playwright-test/reporter-attachment.spec.ts @@ -59,7 +59,7 @@ test('render screenshot attachment', async ({ runInlineTest }) => { expect(result.exitCode).toBe(1); }); -test('render trace attachment', async ({ runInlineTest }) => { +test('render trace attachment by name', async ({ runInlineTest }) => { const result = await runInlineTest({ 'a.test.ts': ` import { test, expect } from '@playwright/test'; @@ -81,6 +81,28 @@ test('render trace attachment', async ({ runInlineTest }) => { expect(result.exitCode).toBe(1); }); +test('render trace attachment by content type', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.ts': ` + import { test, expect } from '@playwright/test'; + test('one', async ({}, testInfo) => { + testInfo.attachments.push({ + name: 'foo-trace', + path: testInfo.outputPath('my dir with space', 'foo.zip'), + contentType: 'application/zip;content=playwright-trace' + }); + expect(1).toBe(0); + }); + `, + }, { reporter: 'line' }); + const text = result.output.replace(/\\/g, '/'); + expect(text).toContain(' attachment #1: foo-trace (application/zip;content=playwright-trace) ────────────────────────────'); + expect(text).toContain(' test-results/a-one/my dir with space/foo.zip'); + expect(text).toContain('npx playwright show-trace "test-results/a-one/my dir with space/foo.zip"'); + expect(text).toContain(' ────────────────────────────────────────────────────────────────────────────────────────────────'); + expect(result.exitCode).toBe(1); +}); + test(`testInfo.attach errors`, async ({ runInlineTest }) => { const result = await runInlineTest({ 'a.test.ts': `