test: add attachment tests (#23143)

This commit is contained in:
Pavel Feldman 2023-05-18 11:48:53 -07:00 committed by GitHub
parent 2501bbb715
commit 969e5ff1aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View File

@ -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(),
});

View File

@ -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);
});