From 969e5ff1aab0ee957abbfe1809cd376440b85aec Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 18 May 2023 11:48:53 -0700 Subject: [PATCH] test: add attachment tests (#23143) --- .../playwright-test/src/worker/testInfo.ts | 2 +- .../playwright-test/playwright.trace.spec.ts | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/playwright-test/src/worker/testInfo.ts b/packages/playwright-test/src/worker/testInfo.ts index dbb0d53747..7e4364d526 100644 --- a/packages/playwright-test/src/worker/testInfo.ts +++ b/packages/playwright-test/src/worker/testInfo.ts @@ -332,7 +332,7 @@ export class TestInfoImpl implements TestInfo { async attach(name: string, options: { path?: string, body?: string | Buffer, contentType?: string } = {}) { const step = this._addStep({ - title: `attach "${name}"`, + title: `attach "${name}"`, category: 'attach', wallTime: Date.now(), }); diff --git a/tests/playwright-test/playwright.trace.spec.ts b/tests/playwright-test/playwright.trace.spec.ts index 0fc8d072c2..b049e4327d 100644 --- a/tests/playwright-test/playwright.trace.spec.ts +++ b/tests/playwright-test/playwright.trace.spec.ts @@ -526,3 +526,59 @@ test(`trace:retain-on-failure should create trace if request context is disposed expect(trace.apiNames).toContain('apiRequestContext.get'); expect(result.failed).toBe(1); }); + +test('should include attachments by default', async ({ runInlineTest, server }, testInfo) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { trace: 'on' } }; + `, + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + + test('pass', async ({}, testInfo) => { + testInfo.attach('foo', { body: 'bar' }); + }); + `, + }, { workers: 1 }); + + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); + const trace = await parseTrace(testInfo.outputPath('test-results', 'a-pass', 'trace.zip')); + expect(trace.apiNames).toEqual([ + 'Before Hooks', + `attach "foo"`, + 'After Hooks', + ]); + expect(trace.actions[1].attachments).toEqual([{ + name: 'foo', + contentType: 'text/plain', + sha1: expect.any(String), + }]); + expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(1); +}); + +test('should opt out of attachments', async ({ runInlineTest, server }, testInfo) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { trace: { mode: 'on', attachments: false } } }; + `, + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + + test('pass', async ({}, testInfo) => { + testInfo.attach('foo', { body: 'bar' }); + }); + `, + }, { workers: 1 }); + + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); + const trace = await parseTrace(testInfo.outputPath('test-results', 'a-pass', 'trace.zip')); + expect(trace.apiNames).toEqual([ + 'Before Hooks', + `attach "foo"`, + 'After Hooks', + ]); + expect(trace.actions[1].attachments).toEqual(undefined); + expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(0); +});