2021-06-07 03:09:53 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-08 02:11:44 +03:00
|
|
|
import { test, expect } from './playwright-test-fixtures';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
test('should work', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', async ({asdf}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
2023-02-21 23:51:40 +03:00
|
|
|
test('should work with comments inside fixtures', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
foo: async ({}, test) => await test('foo'),
|
|
|
|
bar: async ({}, test) => await test('bar'),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', async ({ // }) {,,, /*
|
|
|
|
asdf, // a comment
|
|
|
|
/*/aa* /* */ // line // //
|
|
|
|
/* // */ foo, /* what // */ bar // whoa
|
|
|
|
/* some // comment */ : //
|
|
|
|
/* // /* // */ barbar /* /* /* */
|
|
|
|
}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
expect(foo).toBe('foo');
|
|
|
|
expect(barbar).toBe('bar');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
2023-03-14 19:35:52 +03:00
|
|
|
test('should throw a pretty error if fixtures use rest property', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
|
|
|
asdf: async ({...props}, use) => await use(123),
|
|
|
|
});
|
|
|
|
test('should not allow rest property inside tests', ({...all}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
test('should not allow rest property inside fixtures', ({asdf}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.output).toContain('Rest property "...all" is not supported. List all used fixtures explicitly, separated by comma.');
|
|
|
|
expect(result.output).toContain('Rest property "...props" is not supported. List all used fixtures explicitly, separated by comma.');
|
|
|
|
});
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
test('should work with a sync test function', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', ({asdf}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with a sync fixture function', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: ({}, use) => {
|
|
|
|
use(123);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', ({asdf}) => {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with a non-arrow function', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', function ({asdf}) {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with a named function', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', async function hello({asdf}) {
|
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with renamed parameters', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', function ({asdf: renamed}) {
|
|
|
|
expect(renamed).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with destructured object', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test({ foo: 'foo', bar: { x: 'x', y: 'y' }, baz: 'baz' }),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', async ({ asdf: { foo,
|
|
|
|
bar: { x, y }, baz } }) => {
|
|
|
|
expect(foo).toBe('foo');
|
|
|
|
expect(x).toBe('x');
|
|
|
|
expect(y).toBe('y');
|
|
|
|
expect(baz).toBe('baz');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with destructured array', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(['foo', 'bar', { baz: 'baz' }]),
|
|
|
|
more: async ({}, test) => await test(55),
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should use asdf', async (
|
|
|
|
|
|
|
|
{
|
|
|
|
asdf: [foo, bar, { baz}]
|
|
|
|
|
|
|
|
|
|
|
|
,more}) => {
|
|
|
|
expect(foo).toBe('foo');
|
|
|
|
expect(bar).toBe('bar');
|
|
|
|
expect(baz).toBe('baz');
|
|
|
|
expect(more).toBe(55);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail if parameters are not destructured', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(123),
|
|
|
|
});
|
|
|
|
test('should pass', function () {
|
|
|
|
expect(1).toBe(1);
|
|
|
|
});
|
|
|
|
test('should use asdf', function (abc) {
|
|
|
|
expect(abc.asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.output).toContain('First argument must use the object destructuring pattern: abc');
|
2023-02-15 06:20:56 +03:00
|
|
|
expect(result.output).toContain('a.test.ts:9');
|
2023-02-08 02:11:44 +03:00
|
|
|
expect(result.output).toContain('function (abc)');
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(result.results.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should fail with an unknown fixture', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test, expect } from '@playwright/test';
|
|
|
|
test('should use asdf', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(123);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.output).toContain('Test has unknown parameter "asdf".');
|
2023-02-15 06:20:56 +03:00
|
|
|
expect(result.output).toContain('a.test.ts:3');
|
2023-02-08 02:11:44 +03:00
|
|
|
expect(result.output).toContain('async ({asdf})');
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(result.results.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should run the fixture every time', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
let counter = 0;
|
2023-02-15 06:20:56 +03:00
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: async ({}, test) => await test(counter++),
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 1', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(0);
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 2', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(1);
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 3', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(2);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results.map(r => r.status)).toEqual(['passed', 'passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should only run worker fixtures once', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
2021-06-07 03:09:53 +03:00
|
|
|
let counter = 0;
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
asdf: [ async ({}, test) => await test(counter++), { scope: 'worker' } ],
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 1', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(0);
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 2', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(0);
|
|
|
|
});
|
2021-06-28 23:13:35 +03:00
|
|
|
test('should use asdf 3', async ({asdf}) => {
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(asdf).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results.map(r => r.status)).toEqual(['passed', 'passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('each file should get their own fixtures', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
worker: [ async ({}, test) => await test('worker-a'), { scope: 'worker' } ],
|
|
|
|
test: async ({}, test) => await test('test-a'),
|
|
|
|
});
|
|
|
|
test('should use worker', async ({worker, test}) => {
|
|
|
|
expect(worker).toBe('worker-a');
|
|
|
|
expect(test).toBe('test-a');
|
|
|
|
});
|
|
|
|
`,
|
2023-02-15 06:20:56 +03:00
|
|
|
'b.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
worker: [ async ({}, test) => await test('worker-b'), { scope: 'worker' } ],
|
|
|
|
test: async ({}, test) => await test('test-b'),
|
|
|
|
});
|
|
|
|
test('should use worker', async ({worker, test}) => {
|
|
|
|
expect(worker).toBe('worker-b');
|
|
|
|
expect(test).toBe('test-b');
|
|
|
|
});
|
|
|
|
`,
|
2023-02-15 06:20:56 +03:00
|
|
|
'c.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
worker: [ async ({}, test) => await test('worker-c'), { scope: 'worker' } ],
|
|
|
|
test: async ({}, test) => await test('test-c'),
|
|
|
|
});
|
|
|
|
test('should use worker', async ({worker, test}) => {
|
|
|
|
expect(worker).toBe('worker-c');
|
|
|
|
expect(test).toBe('test-c');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results.map(r => r.status)).toEqual(['passed', 'passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('tests should be able to share worker fixtures', async ({ runInlineTest }) => {
|
|
|
|
const { results } = await runInlineTest({
|
|
|
|
'worker.js': `
|
|
|
|
global.counter = 0;
|
2023-02-15 06:20:56 +03:00
|
|
|
const { test: base, expect } = require('@playwright/test');
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
worker: [ async ({}, test) => await test(global.counter++), { scope: 'worker' } ],
|
|
|
|
});
|
2023-02-15 06:20:56 +03:00
|
|
|
module.exports = { test, expect };
|
2021-06-07 03:09:53 +03:00
|
|
|
`,
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
const { test, expect } = require('./worker.js');
|
2021-06-07 03:09:53 +03:00
|
|
|
test('should use worker', async ({worker}) => {
|
|
|
|
expect(worker).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
2023-02-15 06:20:56 +03:00
|
|
|
'b.test.ts': `
|
|
|
|
const { test, expect } = require('./worker.js');
|
2021-06-07 03:09:53 +03:00
|
|
|
test('should use worker', async ({worker}) => {
|
|
|
|
expect(worker).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
2023-02-15 06:20:56 +03:00
|
|
|
'c.test.ts': `
|
|
|
|
const { test, expect } = require('./worker.js');
|
2021-06-07 03:09:53 +03:00
|
|
|
test('should use worker', async ({worker}) => {
|
|
|
|
expect(worker).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(results.map(r => r.status)).toEqual(['passed', 'passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('automatic fixtures should work', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
let counterTest = 0;
|
2022-05-13 13:17:20 +03:00
|
|
|
let counterHooksIncluded = 0;
|
2021-06-07 03:09:53 +03:00
|
|
|
let counterWorker = 0;
|
|
|
|
test.use({
|
|
|
|
automaticTestFixture: [ async ({}, runTest) => {
|
|
|
|
++counterTest;
|
|
|
|
await runTest();
|
|
|
|
}, { auto: true } ],
|
|
|
|
|
2022-05-13 13:17:20 +03:00
|
|
|
automaticTestFixtureHooksIncluded: [ async ({}, runTest) => {
|
|
|
|
++counterHooksIncluded;
|
|
|
|
await runTest();
|
|
|
|
}, { auto: 'all-hooks-included' } ],
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
automaticWorkerFixture: [ async ({}, runTest) => {
|
|
|
|
++counterWorker;
|
|
|
|
await runTest();
|
|
|
|
}, { scope: 'worker', auto: true } ],
|
|
|
|
});
|
|
|
|
test.beforeAll(async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2022-05-13 13:17:20 +03:00
|
|
|
expect(counterHooksIncluded).toBe(1);
|
|
|
|
expect(counterTest).toBe(0);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
test.beforeEach(async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2022-03-09 03:35:14 +03:00
|
|
|
expect(counterTest === 1 || counterTest === 2).toBe(true);
|
2023-05-02 21:04:51 +03:00
|
|
|
expect(counterHooksIncluded === 2 || counterHooksIncluded === 3).toBe(true);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
test('test 1', async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2023-05-02 21:04:51 +03:00
|
|
|
expect(counterHooksIncluded).toBe(2);
|
2022-03-09 03:35:14 +03:00
|
|
|
expect(counterTest).toBe(1);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
test('test 2', async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2023-05-02 21:04:51 +03:00
|
|
|
expect(counterHooksIncluded).toBe(3);
|
2022-03-09 03:35:14 +03:00
|
|
|
expect(counterTest).toBe(2);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
test.afterEach(async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2022-03-09 03:35:14 +03:00
|
|
|
expect(counterTest === 1 || counterTest === 2).toBe(true);
|
2023-05-02 21:04:51 +03:00
|
|
|
expect(counterHooksIncluded === 2 || counterHooksIncluded === 3).toBe(true);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
test.afterAll(async ({}) => {
|
|
|
|
expect(counterWorker).toBe(1);
|
2023-05-02 21:04:51 +03:00
|
|
|
expect(counterHooksIncluded).toBe(4);
|
2022-03-09 03:35:14 +03:00
|
|
|
expect(counterTest).toBe(2);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.results.map(r => r.status)).toEqual(['passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
2021-08-09 23:26:33 +03:00
|
|
|
test('automatic fixture should start before regular fixture and teardown after', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-08-09 23:26:33 +03:00
|
|
|
test.use({
|
|
|
|
auto: [ async ({}, runTest) => {
|
|
|
|
console.log('\\n%%auto-setup');
|
|
|
|
await runTest();
|
|
|
|
console.log('\\n%%auto-teardown');
|
|
|
|
}, { auto: true } ],
|
|
|
|
foo: async ({}, runTest) => {
|
|
|
|
console.log('\\n%%foo-setup');
|
|
|
|
await runTest();
|
|
|
|
console.log('\\n%%foo-teardown');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
test('test 1', async ({ foo }) => {
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
2023-02-08 02:11:44 +03:00
|
|
|
expect(result.outputLines).toEqual([
|
|
|
|
'auto-setup',
|
|
|
|
'foo-setup',
|
|
|
|
'foo-teardown',
|
|
|
|
'auto-teardown',
|
2021-08-09 23:26:33 +03:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2021-07-08 22:55:43 +03:00
|
|
|
test('automatic fixtures should keep workerInfo after conditional skip', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-07-08 22:55:43 +03:00
|
|
|
test.use({
|
|
|
|
automaticTestFixture: [ async ({}, runTest, workerInfo) => {
|
|
|
|
await runTest();
|
|
|
|
expect(workerInfo.workerIndex).toBe(0);
|
|
|
|
console.log('success test fixture')
|
|
|
|
}, { auto: true } ],
|
|
|
|
|
|
|
|
automaticWorkerFixture: [ async ({}, runTest, workerInfo) => {
|
|
|
|
await runTest();
|
|
|
|
expect(workerInfo.workerIndex).toBe(0);
|
|
|
|
console.log('success worker fixture')
|
|
|
|
}, { scope: 'worker', auto: true } ],
|
|
|
|
});
|
|
|
|
test.skip(({ }) => false);
|
|
|
|
test('good', async ({ }) => {
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.output).toContain('success test fixture');
|
|
|
|
expect(result.output).toContain('success worker fixture');
|
|
|
|
expect(result.results.map(r => r.status)).toEqual(['passed']);
|
|
|
|
});
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
test('tests does not run non-automatic worker fixtures', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
2021-06-07 03:09:53 +03:00
|
|
|
let counter = 0;
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
nonAutomaticWorkerFixture: [ async ({}, runTest) => {
|
|
|
|
++counter;
|
|
|
|
await runTest();
|
|
|
|
}, { scope: 'worker' }],
|
|
|
|
});
|
|
|
|
test('test 1', async ({}) => {
|
|
|
|
expect(counter).toBe(0);
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.results.map(r => r.status)).toEqual(['passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should teardown fixtures after timeout', async ({ runInlineTest }, testInfo) => {
|
|
|
|
const file = testInfo.outputPath('log.txt');
|
|
|
|
require('fs').writeFileSync(file, '', 'utf8');
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.spec.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
file: [ ${JSON.stringify(file)}, { scope: 'worker' } ],
|
|
|
|
w: [ async ({ file }, runTest) => {
|
|
|
|
await runTest('w');
|
|
|
|
require('fs').appendFileSync(file, 'worker fixture teardown\\n', 'utf8');
|
|
|
|
}, { scope: 'worker' } ],
|
|
|
|
t: async ({ file }, runTest) => {
|
|
|
|
await runTest('t');
|
|
|
|
require('fs').appendFileSync(file, 'test fixture teardown\\n', 'utf8');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
test('test', async ({t, w}) => {
|
|
|
|
expect(t).toBe('t');
|
|
|
|
expect(w).toBe('w');
|
|
|
|
await new Promise(() => {});
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { timeout: 1000 });
|
|
|
|
expect(result.results[0].status).toBe('timedOut');
|
|
|
|
const content = require('fs').readFileSync(file, 'utf8');
|
|
|
|
expect(content).toContain('worker fixture teardown');
|
|
|
|
expect(content).toContain('test fixture teardown');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with two different test objects', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test1 = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
foo: async ({}, test) => await test(123),
|
|
|
|
});
|
2023-02-15 06:20:56 +03:00
|
|
|
const test2 = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
bar: async ({}, test) => await test(456),
|
|
|
|
});
|
|
|
|
test1('test 1', async ({foo}) => {
|
|
|
|
expect(foo).toBe(123);
|
|
|
|
});
|
|
|
|
test2('test 2', async ({bar}) => {
|
|
|
|
expect(bar).toBe(456);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.results.map(r => r.workerIndex).sort()).toEqual([0, 0]);
|
|
|
|
expect(result.results.map(r => r.status).sort()).toEqual(['passed', 'passed']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with overrides calling base', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test1 = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
dep: async ({}, test) => await test('override'),
|
|
|
|
foo: async ({}, test) => await test('base'),
|
|
|
|
bar: async ({foo}, test) => await test(foo + '-bar'),
|
|
|
|
});
|
|
|
|
const test2 = test1.extend({
|
|
|
|
foo: async ({ foo, dep }, test) => await test(foo + '-' + dep + '1'),
|
|
|
|
});
|
|
|
|
const test3 = test2.extend({
|
|
|
|
foo: async ({ foo, dep }, test) => await test(foo + '-' + dep + '2'),
|
|
|
|
});
|
|
|
|
test3('test', async ({bar}) => {
|
|
|
|
expect(bar).toBe('base-override1-override2-bar');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should understand worker fixture params in overrides calling base', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test1 = base.extend({
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
param: [ 'param', { scope: 'worker', option: true }],
|
|
|
|
}).extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
foo: async ({}, test) => await test('foo'),
|
|
|
|
bar: async ({foo}, test) => await test(foo + '-bar'),
|
|
|
|
});
|
|
|
|
const test2 = test1.extend({
|
|
|
|
foo: async ({ foo, param }, test) => await test(foo + '-' + param),
|
|
|
|
});
|
|
|
|
const test3 = test2.extend({
|
|
|
|
foo: async ({ foo }, test) => await test(foo + '-override'),
|
|
|
|
});
|
|
|
|
test3('test', async ({ bar }) => {
|
|
|
|
console.log(bar);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'playwright.config.ts': `
|
|
|
|
module.exports = { projects: [
|
|
|
|
{ use: { param: 'p1' } },
|
|
|
|
{ use: { param: 'p2' } },
|
|
|
|
{ use: { param: 'p3' } },
|
|
|
|
]};
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
const outputs = result.results.map(r => r.stdout[0].text.replace(/\s/g, ''));
|
|
|
|
expect(outputs.sort()).toEqual(['foo-p1-override-bar', 'foo-p2-override-bar', 'foo-p3-override-bar']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should work with two overrides calling base', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test1 = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
foo: async ({}, test) => await test('foo'),
|
|
|
|
bar: async ({}, test) => await test('bar'),
|
|
|
|
baz: async ({foo, bar}, test) => await test(foo + '-baz-' + bar),
|
|
|
|
});
|
|
|
|
const test2 = test1.extend({
|
|
|
|
foo: async ({ foo, bar }, test) => await test(foo + '-' + bar),
|
|
|
|
bar: async ({ bar }, test) => await test(bar + '-override'),
|
|
|
|
});
|
|
|
|
test2('test', async ({baz}) => {
|
|
|
|
expect(baz).toBe('foo-bar-override-baz-bar-override');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.results[0].status).toBe('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not create a new worker for test fixtures', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('base test', async ({}, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
const test2 = test.extend({
|
|
|
|
foo: async ({}, run) => {
|
|
|
|
console.log('foo-a');
|
|
|
|
await run();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
test2('a test', async ({ foo }, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'b.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
const test2 = test.extend({
|
|
|
|
foo: async ({}, run) => {
|
|
|
|
console.log('foo-b');
|
|
|
|
await run();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const test3 = test2.extend({
|
|
|
|
foo: async ({ foo }, run) => {
|
|
|
|
console.log('foo-c');
|
|
|
|
await run();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
test3('b test', async ({ foo }, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
|
|
|
expect(result.output).toContain('foo-a');
|
|
|
|
expect(result.output).toContain('foo-b');
|
|
|
|
expect(result.output).toContain('foo-c');
|
|
|
|
expect(result.passed).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should create a new worker for worker fixtures', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('base test', async ({}, testInfo) => {
|
2021-07-27 21:04:38 +03:00
|
|
|
console.log('\\n%%base-' + testInfo.workerIndex);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const test2 = test.extend({
|
|
|
|
foo: [async ({}, run) => {
|
|
|
|
console.log('foo-a');
|
|
|
|
await run();
|
|
|
|
}, { scope: 'worker' }],
|
|
|
|
});
|
|
|
|
test2('a test', async ({ foo }, testInfo) => {
|
2021-07-27 21:04:38 +03:00
|
|
|
console.log('\\n%%a-' + testInfo.workerIndex);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
'b.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
const test2 = test.extend({
|
|
|
|
bar: async ({}, run) => {
|
|
|
|
console.log('bar-b');
|
|
|
|
await run();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
test2('b test', async ({ bar }, testInfo) => {
|
2021-07-27 21:04:38 +03:00
|
|
|
console.log('\\n%%b-' + testInfo.workerIndex);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
|
|
|
expect(result.output).toContain('foo-a');
|
|
|
|
expect(result.output).toContain('bar-b');
|
2022-07-01 03:05:08 +03:00
|
|
|
const baseWorker = +result.output.match(/%%base-(\d)/)![1];
|
2021-07-27 21:04:38 +03:00
|
|
|
expect(result.output).toContain(`%%base-${baseWorker}`);
|
|
|
|
expect(result.output).toContain(`%%a-${1 - baseWorker}`);
|
|
|
|
expect(result.output).toContain(`%%b-${baseWorker}`);
|
2021-06-07 03:09:53 +03:00
|
|
|
expect(result.passed).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should run tests in order', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('test1', async ({}, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
console.log('\\n%%test1');
|
|
|
|
});
|
|
|
|
|
|
|
|
const child = test.extend({
|
|
|
|
foo: async ({}, run) => {
|
|
|
|
console.log('\\n%%beforeEach');
|
|
|
|
await run();
|
|
|
|
console.log('\\n%%afterEach');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
child('test2', async ({ foo }, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
console.log('\\n%%test2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test3', async ({}, testInfo) => {
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
console.log('\\n%%test3');
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
|
|
|
expect(result.passed).toBe(3);
|
2023-02-08 02:11:44 +03:00
|
|
|
expect(result.outputLines).toEqual([
|
|
|
|
'test1',
|
|
|
|
'beforeEach',
|
|
|
|
'test2',
|
|
|
|
'afterEach',
|
|
|
|
'test3',
|
2021-06-07 03:09:53 +03:00
|
|
|
]);
|
|
|
|
});
|
2021-08-09 23:26:33 +03:00
|
|
|
|
|
|
|
test('worker fixture should not receive TestInfo', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test, expect } from '@playwright/test';
|
2021-08-09 23:26:33 +03:00
|
|
|
test.use({
|
|
|
|
worker: [async ({}, use, info) => {
|
|
|
|
expect(info.title).toBe(undefined);
|
|
|
|
await use();
|
|
|
|
}, { scope: 'worker' }],
|
|
|
|
test: async ({ worker }, use, info) => {
|
|
|
|
expect(info.title).not.toBe(undefined);
|
|
|
|
await use();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
test('test 1', async ({ test }) => {
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
});
|
2021-09-28 20:56:50 +03:00
|
|
|
|
|
|
|
test('worker teardown errors reflected in timed-out tests', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
2023-02-15 06:20:56 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-09-28 20:56:50 +03:00
|
|
|
foo: [async ({}, use) => {
|
|
|
|
let cb;
|
|
|
|
await use(new Promise((f, r) => cb = r));
|
|
|
|
cb(new Error('Rejecting!'));
|
|
|
|
}, { scope: 'worker' }]
|
|
|
|
});
|
|
|
|
test('timedout', async ({ foo }) => {
|
|
|
|
await foo;
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { timeout: 1000 });
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
2022-07-01 03:05:08 +03:00
|
|
|
expect(result.output).toContain('Test timeout of 1000ms exceeded.');
|
2021-09-28 20:56:50 +03:00
|
|
|
expect(result.output).toContain('Rejecting!');
|
|
|
|
});
|
2023-09-16 00:51:31 +03:00
|
|
|
|
|
|
|
test('automatic worker fixtures should start before automatic test fixtures', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
|
|
|
autoTest: [async ({}, use) => {
|
|
|
|
console.log('\\n%%TEST FIXTURE 1');
|
|
|
|
await use();
|
|
|
|
console.log('\\n%%TEST FIXTURE 2');
|
|
|
|
}, { scope: 'test', auto: true }],
|
|
|
|
|
|
|
|
autoWorker: [async ({}, use) => {
|
|
|
|
console.log('\\n%%WORKER FIXTURE 1');
|
|
|
|
await use();
|
|
|
|
console.log('\\n%%WORKER FIXTURE 2');
|
|
|
|
}, { scope: 'worker', auto: true }],
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test', async () => {
|
|
|
|
console.log('\\n%%TEST');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.outputLines).toEqual([
|
|
|
|
'WORKER FIXTURE 1',
|
|
|
|
'TEST FIXTURE 1',
|
|
|
|
'TEST',
|
|
|
|
'TEST FIXTURE 2',
|
|
|
|
'WORKER FIXTURE 2',
|
|
|
|
]);
|
|
|
|
});
|