Skip to content

Commit 7196f82

Browse files
committed
cherry-pick(#23143): test: add attachment tests
1 parent 32c247b commit 7196f82

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

packages/playwright-test/src/worker/testInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export class TestInfoImpl implements TestInfo {
332332

333333
async attach(name: string, options: { path?: string, body?: string | Buffer, contentType?: string } = {}) {
334334
const step = this._addStep({
335-
title: `attach "${name}"`,
335+
title: `attach "${name}"`,
336336
category: 'attach',
337337
wallTime: Date.now(),
338338
});

tests/playwright-test/playwright.trace.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,59 @@ test(`trace:retain-on-failure should create trace if request context is disposed
526526
expect(trace.apiNames).toContain('apiRequestContext.get');
527527
expect(result.failed).toBe(1);
528528
});
529+
530+
test('should include attachments by default', async ({ runInlineTest, server }, testInfo) => {
531+
const result = await runInlineTest({
532+
'playwright.config.ts': `
533+
module.exports = { use: { trace: 'on' } };
534+
`,
535+
'a.spec.ts': `
536+
import { test, expect } from '@playwright/test';
537+
538+
test('pass', async ({}, testInfo) => {
539+
testInfo.attach('foo', { body: 'bar' });
540+
});
541+
`,
542+
}, { workers: 1 });
543+
544+
expect(result.exitCode).toBe(0);
545+
expect(result.passed).toBe(1);
546+
const trace = await parseTrace(testInfo.outputPath('test-results', 'a-pass', 'trace.zip'));
547+
expect(trace.apiNames).toEqual([
548+
'Before Hooks',
549+
`attach "foo"`,
550+
'After Hooks',
551+
]);
552+
expect(trace.actions[1].attachments).toEqual([{
553+
name: 'foo',
554+
contentType: 'text/plain',
555+
sha1: expect.any(String),
556+
}]);
557+
expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(1);
558+
});
559+
560+
test('should opt out of attachments', async ({ runInlineTest, server }, testInfo) => {
561+
const result = await runInlineTest({
562+
'playwright.config.ts': `
563+
module.exports = { use: { trace: { mode: 'on', attachments: false } } };
564+
`,
565+
'a.spec.ts': `
566+
import { test, expect } from '@playwright/test';
567+
568+
test('pass', async ({}, testInfo) => {
569+
testInfo.attach('foo', { body: 'bar' });
570+
});
571+
`,
572+
}, { workers: 1 });
573+
574+
expect(result.exitCode).toBe(0);
575+
expect(result.passed).toBe(1);
576+
const trace = await parseTrace(testInfo.outputPath('test-results', 'a-pass', 'trace.zip'));
577+
expect(trace.apiNames).toEqual([
578+
'Before Hooks',
579+
`attach "foo"`,
580+
'After Hooks',
581+
]);
582+
expect(trace.actions[1].attachments).toEqual(undefined);
583+
expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(0);
584+
});

0 commit comments

Comments
 (0)