2021-06-07 03:09:53 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
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 merge options', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.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
|
|
|
foo: 'foo',
|
|
|
|
bar: 'bar',
|
|
|
|
});
|
|
|
|
|
|
|
|
test.use({ foo: 'foo2' });
|
|
|
|
test.use({ bar: 'bar2' });
|
|
|
|
test('test', ({ foo, bar }) => {
|
|
|
|
expect(foo).toBe('foo2');
|
|
|
|
expect(bar).toBe('bar2');
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should run tests with different test options in the same worker', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'helper.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base } from '@playwright/test';
|
|
|
|
export * from '@playwright/test';
|
|
|
|
export const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
foo: 'foo',
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from './helper';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('test', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('foo');
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.describe('suite1', () => {
|
|
|
|
test.use({ foo: 'bar' });
|
|
|
|
test('test1', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('bar');
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test.describe('suite2', () => {
|
|
|
|
test.use({ foo: 'baz' });
|
|
|
|
test('test2', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('baz');
|
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
`
|
|
|
|
}, { workers: 1 });
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(3);
|
|
|
|
});
|
|
|
|
|
2021-08-11 02:32:32 +03:00
|
|
|
test('should throw when setting worker options in describe', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base, expect } from '@playwright/test';
|
|
|
|
const test = base.extend({
|
2021-08-11 02:32:32 +03:00
|
|
|
foo: [undefined, { scope: 'worker' }],
|
|
|
|
});
|
|
|
|
test.describe('suite', () => {
|
|
|
|
test.use({ foo: 'bar' });
|
|
|
|
test('test', ({ foo }, testInfo) => {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
`
|
|
|
|
}, { workers: 1 });
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(result.output).toContain([
|
|
|
|
`Cannot use({ foo }) in a describe group, because it forces a new worker.`,
|
|
|
|
`Make it top-level in the test file or put in the configuration file.`,
|
|
|
|
].join('\n'));
|
2023-02-08 02:11:44 +03:00
|
|
|
expect(result.output).toContain(`{ foo: 'bar' }`);
|
2021-08-11 02:32:32 +03:00
|
|
|
});
|
|
|
|
|
2021-06-07 03:09:53 +03:00
|
|
|
test('should run tests with different worker options', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'helper.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base } from '@playwright/test';
|
|
|
|
export * from '@playwright/test';
|
|
|
|
export const test = base.extend({
|
2021-06-07 03:09:53 +03:00
|
|
|
foo: [undefined, { scope: 'worker' }],
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from './helper';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('test', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe(undefined);
|
2021-08-11 02:32:32 +03:00
|
|
|
expect(testInfo.workerIndex).toBe(0);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
2021-08-11 02:32:32 +03:00
|
|
|
`,
|
|
|
|
'b.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from './helper';
|
2021-08-11 02:32:32 +03:00
|
|
|
test.use({ foo: 'bar' });
|
2021-06-07 03:09:53 +03:00
|
|
|
|
2021-08-11 02:32:32 +03:00
|
|
|
test('test1', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('bar');
|
|
|
|
expect(testInfo.workerIndex).toBe(1);
|
|
|
|
});
|
2021-06-07 03:09:53 +03:00
|
|
|
|
2021-08-11 02:32:32 +03:00
|
|
|
test('test2', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('bar');
|
|
|
|
expect(testInfo.workerIndex).toBe(1);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`,
|
2021-08-11 02:32:32 +03:00
|
|
|
'c.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from './helper';
|
2021-08-11 02:32:32 +03:00
|
|
|
test.use({ foo: 'baz' });
|
|
|
|
test('test2', ({ foo }, testInfo) => {
|
|
|
|
expect(foo).toBe('baz');
|
|
|
|
expect(testInfo.workerIndex).toBe(2);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`
|
|
|
|
}, { workers: 1 });
|
|
|
|
|
|
|
|
expect(result.exitCode).toBe(0);
|
2021-08-11 02:32:32 +03:00
|
|
|
expect(result.passed).toBe(4);
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should use options from the config', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'helper.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test as base } from '@playwright/test';
|
|
|
|
export * from '@playwright/test';
|
|
|
|
export const test = 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
|
|
|
foo: [ 'foo', { option: true } ],
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
'playwright.config.ts': `
|
|
|
|
module.exports = { use: { foo: 'bar' } };
|
|
|
|
`,
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from './helper';
|
2021-06-07 03:09:53 +03:00
|
|
|
test('test1', ({ foo }) => {
|
|
|
|
expect(foo).toBe('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
test.describe('suite1', () => {
|
|
|
|
test.use({ foo: 'baz' });
|
|
|
|
|
|
|
|
test('test2', ({ foo }) => {
|
|
|
|
expect(foo).toBe('baz');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
`
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(0);
|
|
|
|
expect(result.passed).toBe(2);
|
|
|
|
});
|
2021-10-15 22:03:26 +03:00
|
|
|
|
|
|
|
test('test.use() should throw if called from beforeAll ', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
2023-02-15 06:20:56 +03:00
|
|
|
import { test, expect } from '@playwright/test';
|
2021-10-15 22:03:26 +03:00
|
|
|
test.beforeAll(() => {
|
|
|
|
test.use({});
|
|
|
|
});
|
|
|
|
test('should work', async () => {
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
expect(result.exitCode).toBe(1);
|
2022-02-19 05:25:18 +03:00
|
|
|
expect(result.output).toContain('Playwright Test did not expect test.use() to be called here');
|
2021-10-15 22:03:26 +03:00
|
|
|
});
|
|
|
|
|