mirror of
				https://github.com/microsoft/playwright.git
				synced 2025-06-26 21:40:17 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			446 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			446 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * Copyright Microsoft Corporation. All rights reserved.
 | |
|  *
 | |
|  * 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 * as fs from 'fs';
 | |
| import * as path from 'path';
 | |
| import { test, expect } from './playwright-test-fixtures';
 | |
| 
 | |
| test('should work and remove non-failures', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'playwright.config.ts': `
 | |
|       module.exports = {
 | |
|         name: 'chromium',
 | |
|         preserveOutput: 'failures-only',
 | |
|         testDir: 'dir',
 | |
|       };
 | |
|     `,
 | |
|     'dir/my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('test 1', async ({}, testInfo) => {
 | |
|         if (testInfo.retry) {
 | |
|           expect(testInfo.outputDir).toContain('my-test-test-1-chromium-retry' + testInfo.retry);
 | |
|           expect(testInfo.outputPath('foo', 'bar')).toContain(require('path').join('my-test-test-1-chromium-retry' + testInfo.retry, 'foo', 'bar'));
 | |
|           require('fs').writeFileSync(testInfo.outputPath('file.txt'), 'content', 'utf-8');
 | |
|         } else {
 | |
|           expect(testInfo.outputDir).toContain('my-test-test-1-chromium');
 | |
|           expect(testInfo.outputPath()).toContain('my-test-test-1-chromium');
 | |
|           expect(testInfo.outputPath('foo', 'bar')).toContain(require('path').join('my-test-test-1-chromium', 'foo', 'bar'));
 | |
|           require('fs').writeFileSync(testInfo.outputPath('file.txt'), 'content', 'utf-8');
 | |
|         }
 | |
|         expect(require('fs').existsSync(testInfo.outputDir)).toBe(true);
 | |
|         if (testInfo.retry < 2)
 | |
|           throw new Error('Give me retries');
 | |
|       });
 | |
|     `,
 | |
|   }, { retries: 2 });
 | |
|   expect(result.exitCode).toBe(0);
 | |
| 
 | |
|   expect(result.results[0].status).toBe('failed');
 | |
|   expect(result.results[0].retry).toBe(0);
 | |
|   // Should only fail the last retry check.
 | |
|   expect(result.results[0].error.message).toBe('Give me retries');
 | |
| 
 | |
|   expect(result.results[1].status).toBe('failed');
 | |
|   expect(result.results[1].retry).toBe(1);
 | |
|   // Should only fail the last retry check.
 | |
|   expect(result.results[1].error.message).toBe('Give me retries');
 | |
| 
 | |
|   expect(result.results[2].status).toBe('passed');
 | |
|   expect(result.results[2].retry).toBe(2);
 | |
| 
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'my-test-test-1-chromium'))).toBe(true);
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'my-test-test-1-chromium-retry1'))).toBe(true);
 | |
|   // Last retry is successfull, so output dir should be removed.
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'my-test-test-1-chromium-retry2'))).toBe(false);
 | |
| });
 | |
| 
 | |
| test('should include repeat token', async ({ runInlineTest }) => {
 | |
|   const result = await runInlineTest({
 | |
|     'a.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('test', ({}, testInfo) => {
 | |
|         if (testInfo.repeatEachIndex)
 | |
|           expect(testInfo.outputPath('')).toContain('repeat' + testInfo.repeatEachIndex);
 | |
|         else
 | |
|           expect(testInfo.outputPath('')).not.toContain('repeat' + testInfo.repeatEachIndex);
 | |
|       });
 | |
|     `
 | |
|   }, { 'repeat-each': 3 });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.passed).toBe(3);
 | |
| });
 | |
| 
 | |
| test('should be unique for beforeAll and afterAll hooks', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'a.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test.beforeAll(({}, testInfo) => {
 | |
|         console.log('\\n%%' + testInfo.outputDir);
 | |
|       });
 | |
|       test.beforeAll(({}, testInfo) => {
 | |
|         console.log('\\n%%' + testInfo.outputDir);
 | |
|       });
 | |
|       test.afterAll(({}, testInfo) => {
 | |
|         console.log('\\n%%' + testInfo.outputDir);
 | |
|       });
 | |
|       test.afterAll(({}, testInfo) => {
 | |
|         console.log('\\n%%' + testInfo.outputDir);
 | |
|       });
 | |
|       test.describe('suite', () => {
 | |
|         test.beforeAll(({}, testInfo) => {
 | |
|           console.log('\\n%%' + testInfo.outputDir);
 | |
|         });
 | |
|         test.afterAll(({}, testInfo) => {
 | |
|           console.log('\\n%%' + testInfo.outputDir);
 | |
|         });
 | |
|         test('fails', ({}, testInfo) => {
 | |
|           expect(1).toBe(2);
 | |
|         });
 | |
|         test('passes', ({}, testInfo) => {
 | |
|         });
 | |
|       });
 | |
|     `
 | |
|   });
 | |
|   expect(result.exitCode).toBe(1);
 | |
|   expect(result.passed).toBe(1);
 | |
|   expect(result.failed).toBe(1);
 | |
|   expect(result.output.split('\n').filter(x => x.startsWith('%%'))).toEqual([
 | |
|     `%%${testInfo.outputPath('test-results', 'a-beforeAll-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-beforeAll1-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-suite-beforeAll-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-suite-afterAll-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-afterAll-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-afterAll1-worker0')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-beforeAll-worker1')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-beforeAll1-worker1')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-suite-beforeAll-worker1')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-suite-afterAll-worker1')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-afterAll-worker1')}`,
 | |
|     `%%${testInfo.outputPath('test-results', 'a-afterAll1-worker1')}`,
 | |
|   ]);
 | |
| });
 | |
| 
 | |
| test('should include the project name', async ({ runInlineTest }) => {
 | |
|   const result = await runInlineTest({
 | |
|     'helper.ts': `
 | |
|       export const test = pwt.test.extend({
 | |
|         auto: [ async ({}, run, testInfo) => {
 | |
|           testInfo.snapshotSuffix = '';
 | |
|           await run();
 | |
|         }, { auto: true } ]
 | |
|       });
 | |
|       export const test2 = pwt.test.extend({
 | |
|         auto: [ async ({}, run, testInfo) => {
 | |
|           testInfo.snapshotSuffix = 'suffix';
 | |
|           await run();
 | |
|         }, { auto: true } ]
 | |
|       });
 | |
|     `,
 | |
|     'playwright.config.ts': `
 | |
|       module.exports = { projects: [
 | |
|         {},
 | |
|         { name: 'foo' },
 | |
|         { name: 'foo' },
 | |
|         { name: 'Bar space!' },
 | |
|       ] };
 | |
|     `,
 | |
|     'my-test.spec.js': `
 | |
|       const { test, test2 } = require('./helper');
 | |
|       test('test 1', async ({}, testInfo) => {
 | |
|         console.log(testInfo.outputPath('bar.txt').replace(/\\\\/g, '/'));
 | |
|         console.log(testInfo.snapshotPath('bar.txt').replace(/\\\\/g, '/'));
 | |
|         if (testInfo.retry !== 1)
 | |
|           throw new Error('Give me a retry');
 | |
|       });
 | |
|       test2('test 2', async ({}, testInfo) => {
 | |
|         console.log(testInfo.outputPath('bar.txt').replace(/\\\\/g, '/'));
 | |
|         console.log(testInfo.snapshotPath('bar.txt').replace(/\\\\/g, '/'));
 | |
|       });
 | |
|     `,
 | |
|   }, { retries: 1 });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.results[0].status).toBe('failed');
 | |
|   expect(result.results[1].status).toBe('passed');
 | |
| 
 | |
|   // test1, run with empty
 | |
|   expect(result.output).toContain('test-results/my-test-test-1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar.txt');
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-retry1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar.txt');
 | |
| 
 | |
|   // test1, run with foo #1
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-foo1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo.txt');
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-foo1-retry1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo.txt');
 | |
| 
 | |
|   // test1, run with foo #2
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-foo2/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo.txt');
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-foo2-retry1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo.txt');
 | |
| 
 | |
|   // test1, run with bar
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-Bar-space-/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-.txt');
 | |
|   expect(result.output).toContain('test-results/my-test-test-1-Bar-space--retry1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space-.txt');
 | |
| 
 | |
|   // test2, run with empty
 | |
|   expect(result.output).toContain('test-results/my-test-test-2/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-suffix.txt');
 | |
| 
 | |
|   // test2, run with foo #1
 | |
|   expect(result.output).toContain('test-results/my-test-test-2-foo1/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo-suffix.txt');
 | |
| 
 | |
|   // test2, run with foo #2
 | |
|   expect(result.output).toContain('test-results/my-test-test-2-foo2/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-foo-suffix.txt');
 | |
| 
 | |
|   // test2, run with bar
 | |
|   expect(result.output).toContain('test-results/my-test-test-2-Bar-space-/bar.txt');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/bar-Bar-space--suffix.txt');
 | |
| });
 | |
| 
 | |
| test('should include path option in snapshot', async ({ runInlineTest }) => {
 | |
|   const result = await runInlineTest({
 | |
|     'helper.ts': `
 | |
|       export const test = pwt.test.extend({
 | |
|         auto: [ async ({}, run, testInfo) => {
 | |
|           testInfo.snapshotSuffix = 'suffix';
 | |
|           await run();
 | |
|         }, { auto: true } ]
 | |
|       });
 | |
|     `,
 | |
|     'playwright.config.ts': `
 | |
|     module.exports = { projects: [
 | |
|       { name: 'foo' },
 | |
|     ] };
 | |
|     `,
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = require('./helper');
 | |
|       test('test with path', async ({}, testInfo) => {
 | |
|         console.log(testInfo.snapshotPath('test', 'path', 'bar.txt').replace(/\\\\/g, '/'));
 | |
|       });
 | |
|     `,
 | |
|   });
 | |
| 
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.results[0].status).toBe('passed');
 | |
|   expect(result.output).toContain('my-test.spec.js-snapshots/test/path/bar-foo-suffix.txt');
 | |
| });
 | |
| 
 | |
| test('should error if snapshotPath is resolved to outside of parent', async ({ runInlineTest }) => {
 | |
|   const result = await runInlineTest({
 | |
|     'helper.ts': `
 | |
|       export const test = pwt.test.extend({
 | |
|         auto: [ async ({}, run, testInfo) => {
 | |
|           testInfo.snapshotSuffix = 'suffix';
 | |
|           await run();
 | |
|         }, { auto: true } ]
 | |
|       });
 | |
|     `,
 | |
|     'playwright.config.ts': `
 | |
|       module.exports = { projects: [
 | |
|         { name: 'foo' },
 | |
|       ] };
 | |
|     `,
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = require('./helper');
 | |
|       test('test with parent path', async ({}, testInfo) => {
 | |
|         console.log(testInfo.snapshotPath('..', 'test', 'path', 'bar.txt').replace(/\\\\/g, '/'));
 | |
|       });
 | |
|     `,
 | |
|   });
 | |
| 
 | |
|   expect(result.exitCode).toBe(1);
 | |
|   expect(result.results[0].status).toBe('failed');
 | |
|   expect(result.output).toContain('The snapshotPath is not allowed outside of the parent directory. Please fix the defined path.');
 | |
|   const badPath = path.join('..', 'test', 'path', 'bar-foo-suffix.txt');
 | |
|   expect(result.output).toContain(`snapshotPath: ${badPath}`);
 | |
| });
 | |
| 
 | |
| test('should error if outputPath is resolved to outside of parent', async ({ runInlineTest }) => {
 | |
|   const result = await runInlineTest({
 | |
|     'helper.ts': `
 | |
|       export const test = pwt.test.extend({
 | |
|         auto: [ async ({}, run, testInfo) => {
 | |
|           testInfo.snapshotSuffix = 'suffix';
 | |
|           await run();
 | |
|         }, { auto: true } ]
 | |
|       });
 | |
|     `,
 | |
|     'playwright.config.ts': `
 | |
|       module.exports = { projects: [
 | |
|         { name: 'foo' },
 | |
|       ] };
 | |
|     `,
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = require('./helper');
 | |
|       test('test with parent path', async ({}, testInfo) => {
 | |
|         console.log(testInfo.outputPath('..', 'test', 'path', 'bar-test').replace(/\\\\/g, '/'));
 | |
|       });
 | |
|     `,
 | |
|   });
 | |
| 
 | |
|   expect(result.exitCode).toBe(1);
 | |
|   expect(result.results[0].status).toBe('failed');
 | |
|   expect(result.output).toContain('The outputPath is not allowed outside of the parent directory. Please fix the defined path.');
 | |
|   const badPath = path.join('..', 'test', 'path', 'bar-test');
 | |
|   expect(result.output).toContain(`outputPath: ${badPath}`);
 | |
| });
 | |
| 
 | |
| test('should remove output dirs for projects run', async ({ runInlineTest }, testInfo) => {
 | |
|   const paths: string[] = [];
 | |
|   const files: string[] = [];
 | |
| 
 | |
|   for (let i = 0; i < 3; i++) {
 | |
|     const p = testInfo.outputPath('path' + i);
 | |
|     await fs.promises.mkdir(p, { recursive: true });
 | |
|     const f = path.join(p, 'my-file.txt');
 | |
|     await fs.promises.writeFile(f, 'contents', 'utf-8');
 | |
|     paths.push(p);
 | |
|     files.push(f);
 | |
|   }
 | |
| 
 | |
|   const result = await runInlineTest({
 | |
|     'playwright.config.js': `
 | |
|       module.exports = { projects: [
 | |
|         { outputDir: ${JSON.stringify(paths[0])} },
 | |
|         { outputDir: ${JSON.stringify(paths[2])} },
 | |
|       ] };
 | |
|     `,
 | |
|     'a.test.js': `
 | |
|       const { test } = pwt;
 | |
|       test('my test', ({}, testInfo) => {});
 | |
|     `
 | |
|   }, { output: '' });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.passed).toBe(2);
 | |
| 
 | |
|   expect(fs.existsSync(files[0])).toBe(false);
 | |
|   expect(fs.existsSync(files[1])).toBe(true);
 | |
|   expect(fs.existsSync(files[2])).toBe(false);
 | |
| });
 | |
| 
 | |
| test('should remove folders with preserveOutput=never', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'playwright.config.ts': `
 | |
|       export default { preserveOutput: 'never' };
 | |
|     `,
 | |
|     'dir/my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('test 1', async ({}, testInfo) => {
 | |
|         require('fs').writeFileSync(testInfo.outputPath('file.txt'), 'content', 'utf-8');
 | |
|         if (testInfo.retry < 2)
 | |
|           throw new Error('Give me retries');
 | |
|       });
 | |
|     `,
 | |
|   }, { retries: 2 });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.results.length).toBe(3);
 | |
| 
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1'))).toBe(false);
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1-retry1'))).toBe(false);
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1-retry2'))).toBe(false);
 | |
| });
 | |
| 
 | |
| test('should preserve failed results', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'dir/my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('test 1', async ({}, testInfo) => {
 | |
|         require('fs').writeFileSync(testInfo.outputPath('file.txt'), 'content', 'utf-8');
 | |
|         if (testInfo.retry < 2)
 | |
|           throw new Error('Give me retries');
 | |
|       });
 | |
|     `,
 | |
|   }, { 'retries': 2 });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(result.results.length).toBe(3);
 | |
| 
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1'))).toBe(true);
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'dir-my-test-test-1-retry1'))).toBe(true);
 | |
| });
 | |
| 
 | |
| 
 | |
| test('should accept a relative path for outputDir', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('test', async ({}, testInfo) => {
 | |
|         expect(testInfo.outputDir).toBe(${JSON.stringify(path.join(testInfo.outputDir, './my-output-dir', 'my-test-test'))});
 | |
|       });
 | |
|     `,
 | |
|     'playwright.config.js': `
 | |
|     module.exports = { projects: [
 | |
|       { outputDir: './my-output-dir' },
 | |
|     ] };
 | |
|     `,
 | |
|   }, {}, {}, { usesCustomOutputDir: true });
 | |
|   expect(result.exitCode).toBe(0);
 | |
| });
 | |
| 
 | |
| test('should have output dir based on rootDir (cwd)', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'playwright.config.js': `
 | |
|       const path = require('path');
 | |
|       module.exports = {
 | |
|         testDir: path.join(__dirname, 'e2e'),
 | |
|         outputDir: 'test-results/',
 | |
|       };`,
 | |
|     'e2e/example.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       const fs = require('fs');
 | |
|       test('hello world', async ({ }, testInfo) => {
 | |
|         fs.writeFileSync(testInfo.outputPath('foo.txt'), 'hello');
 | |
|       });
 | |
|     `,
 | |
|   }, {}, {}, { usesCustomOutputDir: true });
 | |
|   expect(result.exitCode).toBe(0);
 | |
|   expect(fs.existsSync(testInfo.outputPath('test-results', 'example-hello-world', 'foo.txt'))).toBe(true);
 | |
| });
 | |
| 
 | |
| 
 | |
| test('should allow nonAscii characters in the output dir', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test('こんにちは世界', async ({}, testInfo) => {
 | |
|         console.log('\\n%%' + testInfo.outputDir);
 | |
|       });
 | |
|     `,
 | |
|   });
 | |
|   const outputDir = result.output.split('\n').filter(x => x.startsWith('%%'))[0].slice('%%'.length);
 | |
|   expect(outputDir).toBe(path.join(testInfo.outputDir, 'test-results', 'my-test-こんにちは世界'));
 | |
| });
 | |
| 
 | |
| test('should allow include the describe name the output dir', async ({ runInlineTest }, testInfo) => {
 | |
|   const result = await runInlineTest({
 | |
|     'my-test.spec.js': `
 | |
|       const { test } = pwt;
 | |
|       test.describe('hello', () => {
 | |
|         test('world', async ({}, testInfo) => {
 | |
|           console.log('\\n%%' + testInfo.outputDir);
 | |
|         });
 | |
|       });
 | |
|     `,
 | |
|   });
 | |
|   const outputDir = result.output.split('\n').filter(x => x.startsWith('%%'))[0].slice('%%'.length);
 | |
|   expect(outputDir).toBe(path.join(testInfo.outputDir, 'test-results', 'my-test-hello-world'));
 | |
| });
 | 
