mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-13 17:14:02 +03:00
64e7557fb9
In several of the Playwright APIs, falsey values were not handled correctly. This changeset adds tests (and some fixes): - route.continue: If options.postData was the empty string, the continue failed to override the post data. - page.post (application/json with options.data: false|''|0|null): Raw falsey values were getting dropped (i.e. you can't do the equivalent of curl --header application/json … -d 'false'). This has been fixed with most values across all browsers, but an additional fix is needed for 'null' which the channel serializer treats extra specially. - testInfo.attach: This didn't get reported as an error when options.path was the empty string, but should have been. #11413 (and its fix #11414) inspired this search as they are the same class of bug.
189 lines
6.9 KiB
TypeScript
189 lines
6.9 KiB
TypeScript
/**
|
|
* Copyright (c) Microsoft Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { test, expect, stripAscii } from './playwright-test-fixtures';
|
|
|
|
test('render text attachment', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('one', async ({}, testInfo) => {
|
|
testInfo.attachments.push({
|
|
name: 'attachment',
|
|
body: Buffer.from('Hello world'),
|
|
contentType: 'text/plain'
|
|
});
|
|
expect(1).toBe(0);
|
|
});
|
|
`,
|
|
}, { reporter: 'line' });
|
|
const text = stripAscii(result.output);
|
|
expect(text).toContain(' attachment #1: attachment (text/plain) ---------------------------------------------------------');
|
|
expect(text).toContain(' Hello world');
|
|
expect(text).toContain(' ------------------------------------------------------------------------------------------------');
|
|
expect(result.exitCode).toBe(1);
|
|
});
|
|
|
|
test('render screenshot attachment', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('one', async ({}, testInfo) => {
|
|
testInfo.attachments.push({
|
|
name: 'screenshot',
|
|
path: testInfo.outputPath('some/path.png'),
|
|
contentType: 'image/png'
|
|
});
|
|
expect(1).toBe(0);
|
|
});
|
|
`,
|
|
}, { reporter: 'line' });
|
|
const text = stripAscii(result.output).replace(/\\/g, '/');
|
|
expect(text).toContain(' attachment #1: screenshot (image/png) ----------------------------------------------------------');
|
|
expect(text).toContain(' test-results/a-one/some/path.png');
|
|
expect(text).toContain(' ------------------------------------------------------------------------------------------------');
|
|
expect(result.exitCode).toBe(1);
|
|
});
|
|
|
|
test('render trace attachment', async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('one', async ({}, testInfo) => {
|
|
testInfo.attachments.push({
|
|
name: 'trace',
|
|
path: testInfo.outputPath('trace.zip'),
|
|
contentType: 'application/zip'
|
|
});
|
|
expect(1).toBe(0);
|
|
});
|
|
`,
|
|
}, { reporter: 'line' });
|
|
const text = stripAscii(result.output).replace(/\\/g, '/');
|
|
expect(text).toContain(' attachment #1: trace (application/zip) ---------------------------------------------------------');
|
|
expect(text).toContain(' test-results/a-one/trace.zip');
|
|
expect(text).toContain('npx playwright show-trace test-results/a-one/trace.zip');
|
|
expect(text).toContain(' ------------------------------------------------------------------------------------------------');
|
|
expect(result.exitCode).toBe(1);
|
|
});
|
|
|
|
test(`testInfo.attach errors`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('fail1', async ({}, testInfo) => {
|
|
await testInfo.attach('name', { path: 'foo.txt' });
|
|
});
|
|
test('fail2', async ({}, testInfo) => {
|
|
await testInfo.attach('name', { path: 'foo.txt', body: 'bar' });
|
|
});
|
|
test('fail3', async ({}, testInfo) => {
|
|
await testInfo.attach('name', {});
|
|
});
|
|
`,
|
|
}, { reporter: 'line', workers: 1 });
|
|
const text = stripAscii(result.output).replace(/\\/g, '/');
|
|
expect(text).toMatch(/Error: ENOENT: no such file or directory, copyfile '.*foo.txt.*'/);
|
|
expect(text).toContain(`Exactly one of "path" and "body" must be specified`);
|
|
expect(result.passed).toBe(0);
|
|
expect(result.failed).toBe(3);
|
|
expect(result.exitCode).toBe(1);
|
|
});
|
|
|
|
test(`testInfo.attach errors with empty path`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('fail', async ({}, testInfo) => {
|
|
await testInfo.attach('name', { path: '' });
|
|
});
|
|
`,
|
|
}, { reporter: 'line', workers: 1 });
|
|
expect(stripAscii(result.output)).toMatch(/Error: ENOENT: no such file or directory, copyfile ''/);
|
|
expect(result.exitCode).toBe(1);
|
|
});
|
|
|
|
test(`testInfo.attach error in fixture`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const test = pwt.test.extend({
|
|
fixture: async ({}, use, testInfo) => {
|
|
await use();
|
|
await testInfo.attach('name', { path: 'foo.txt' });
|
|
},
|
|
});
|
|
test('fail1', async ({ fixture }) => {
|
|
});
|
|
`,
|
|
}, { reporter: 'line', workers: 1 });
|
|
const text = stripAscii(result.output).replace(/\\/g, '/');
|
|
expect(text).toMatch(/Error: ENOENT: no such file or directory, copyfile '.*foo.txt.*'/);
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.passed).toBe(0);
|
|
expect(result.failed).toBe(1);
|
|
});
|
|
|
|
test(`testInfo.attach success in fixture`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const test = pwt.test.extend({
|
|
fixture: async ({}, use, testInfo) => {
|
|
const filePath = testInfo.outputPath('foo.txt');
|
|
require('fs').writeFileSync(filePath, 'hello');
|
|
await use();
|
|
await testInfo.attach('name', { path: filePath });
|
|
},
|
|
});
|
|
test('success', async ({ fixture }) => {
|
|
expect(true).toBe(false);
|
|
});
|
|
`,
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.failed).toBe(1);
|
|
expect(stripAscii(result.output)).toContain('attachment #1: name (text/plain)');
|
|
});
|
|
|
|
test(`testInfo.attach allow empty string body`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('success', async ({}, testInfo) => {
|
|
await testInfo.attach('name', { body: '', contentType: 'text/plain' });
|
|
expect(0).toBe(1);
|
|
});
|
|
`,
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.failed).toBe(1);
|
|
expect(stripAscii(result.output)).toMatch(/^.*attachment #1: name \(text\/plain\).*\n.*\n.*------/gm);
|
|
});
|
|
|
|
test(`testInfo.attach allow empty buffer body`, async ({ runInlineTest }) => {
|
|
const result = await runInlineTest({
|
|
'a.test.js': `
|
|
const { test } = pwt;
|
|
test('success', async ({}, testInfo) => {
|
|
await testInfo.attach('name', { body: Buffer.from(''), contentType: 'text/plain' });
|
|
expect(0).toBe(1);
|
|
});
|
|
`,
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.failed).toBe(1);
|
|
expect(stripAscii(result.output)).toMatch(/^.*attachment #1: name \(text\/plain\).*\n.*\n.*------/gm);
|
|
});
|