2021-07-22 11:01:18 -07:00
# class: Test
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
* langs: js
Playwright Test provides a `test` function to declare tests and [`expect` function ](https://jestjs.io/docs/expect ) to write assertions.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const name = await page.innerText('.navbar__title');
expect(name).toBe('Playwright');
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const name = await page.innerText('.navbar__title');
expect(name).toBe('Playwright');
});
```
## method: Test.(call)
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
Declares a test.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const name = await page.innerText('.navbar__title');
expect(name).toBe('Playwright');
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev/');
const name = await page.innerText('.navbar__title');
expect(name).toBe('Playwright');
});
```
### param: Test.(call).title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `title` < [string]>
Test title.
### param: Test.(call).testFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 14:47:12 -07:00
- `testFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].
## method: Test.afterAll
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Declares an `afterAll` hook that is executed once per worker after all tests. When called in the scope of a test file, runs after all tests in the file. When called inside a [`method: Test.describe#1` ] group, runs after all tests in the group. If multiple `afterAll` hooks are added, they will run in the order of their registration.
2021-11-19 13:47:30 -08:00
2022-03-25 19:30:45 +01:00
Note that worker process is restarted on test failures, and `afterAll` hook runs again in the new worker. Learn more about [workers and failures ](../test-retries.md ).
2021-07-22 11:01:18 -07:00
### param: Test.afterAll.hookFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-09 13:26:33 -07:00
- `hookFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
2021-11-02 16:51:41 +01:00
Hook function that takes one or two arguments: an object with worker fixtures and optional [TestInfo].
2021-07-22 11:01:18 -07:00
## method: Test.afterEach
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Declares an `afterEach` hook that is executed after each test. When called in the scope of a test file, runs after each test in the file. When called inside a [`method: Test.describe#1` ] group, runs after each test in the group. If multiple `afterEach` hooks are added, they will run in the order of their registration.
2021-07-22 11:01:18 -07:00
2021-10-22 16:32:22 -07:00
You can access all the same [Fixtures] as the test function itself, and also the [TestInfo] object that gives a lot of useful information. For example, you can check whether the test succeeded or failed.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-10-22 16:32:22 -07:00
// example.spec.js
const { test, expect } = require('@playwright/test ');
test.afterEach(async ({ page }, testInfo) => {
console.log(`Finished ${testInfo.title} with status ${testInfo.status}` );
if (testInfo.status !== testInfo.expectedStatus)
console.log(`Did not run as expected, ended up at ${page.url()}` );
});
test('my test', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-10-22 16:32:22 -07:00
// example.spec.ts
import { test, expect } from '@playwright/test ';
test.afterEach(async ({ page }, testInfo) => {
console.log(`Finished ${testInfo.title} with status ${testInfo.status}` );
if (testInfo.status !== testInfo.expectedStatus)
console.log(`Did not run as expected, ended up at ${page.url()}` );
});
test('my test', async ({ page }) => {
// ...
});
```
2021-07-22 11:01:18 -07:00
### param: Test.afterEach.hookFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 14:47:12 -07:00
- `hookFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
Hook function that takes one or two arguments: an object with fixtures and optional [TestInfo].
## method: Test.beforeAll
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Declares a `beforeAll` hook that is executed once per worker process before all tests. When called in the scope of a test file, runs before all tests in the file. When called inside a [`method: Test.describe#1` ] group, runs before all tests in the group. If multiple `beforeAll` hooks are added, they will run in the order of their registration.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
// example.spec.js
const { test, expect } = require('@playwright/test ');
test.beforeAll(async () => {
console.log('Before tests');
});
test.afterAll(async () => {
console.log('After tests');
});
test('my test', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
// example.spec.ts
import { test, expect } from '@playwright/test ';
test.beforeAll(async () => {
console.log('Before tests');
});
test.afterAll(async () => {
console.log('After tests');
});
test('my test', async ({ page }) => {
// ...
});
```
2022-03-25 19:30:45 +01:00
Note that worker process is restarted on test failures, and `beforeAll` hook runs again in the new worker. Learn more about [workers and failures ](../test-retries.md ).
2021-11-19 13:47:30 -08:00
2021-07-22 11:01:18 -07:00
You can use [`method: Test.afterAll` ] to teardown any resources set up in `beforeAll` .
### param: Test.beforeAll.hookFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-09 13:26:33 -07:00
- `hookFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
2021-11-02 16:51:41 +01:00
Hook function that takes one or two arguments: an object with worker fixtures and optional [TestInfo].
2021-07-22 11:01:18 -07:00
## method: Test.beforeEach
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Declares a `beforeEach` hook that is executed before each test. When called in the scope of a test file, runs before each test in the file. When called inside a [`method: Test.describe#1` ] group, runs before each test in the group. If multiple `beforeEach` hooks are added, they will run in the order of their registration.
2021-07-22 11:01:18 -07:00
2021-10-22 16:32:22 -07:00
You can access all the same [Fixtures] as the test function itself, and also the [TestInfo] object that gives a lot of useful information. For example, you can navigate the page before starting the test.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
// example.spec.js
const { test, expect } = require('@playwright/test ');
2021-10-22 16:32:22 -07:00
test.beforeEach(async ({ page }, testInfo) => {
console.log(`Running ${testInfo.title}` );
2021-07-22 11:01:18 -07:00
await page.goto('https://my.start.url/');
});
test('my test', async ({ page }) => {
expect(page.url()).toBe('https://my.start.url/');
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
// example.spec.ts
import { test, expect } from '@playwright/test ';
2021-10-22 16:32:22 -07:00
test.beforeEach(async ({ page }, testInfo) => {
console.log(`Running ${testInfo.title}` );
2021-07-22 11:01:18 -07:00
await page.goto('https://my.start.url/');
});
test('my test', async ({ page }) => {
expect(page.url()).toBe('https://my.start.url/');
});
```
You can use [`method: Test.afterEach` ] to teardown any resources set up in `beforeEach` .
### param: Test.beforeEach.hookFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 14:47:12 -07:00
- `hookFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
Hook function that takes one or two arguments: an object with fixtures and optional [TestInfo].
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-18 15:45:52 -08:00
2022-07-06 13:54:11 -07:00
## method: Test.describe#1
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
Declares a group of tests.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
```
2022-07-06 13:54:11 -07:00
### param: Test.describe#1.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `title` < [string]>
Group title.
2022-07-06 13:54:11 -07:00
### param: Test.describe#1.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `callback` < [function]>
2022-07-06 13:54:11 -07:00
A callback that is run immediately when calling [`method: Test.describe#1` ]. Any tests added in this callback will belong to the group.
## method: Test.describe#2
* since: v1.24
Declares an anonymous group of tests. This is convenient to give a group of tests a common option with [`method: Test.use` ].
```js tab=js-js
test.describe(() => {
test.use({ colorScheme: 'dark' });
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
```
```js tab=js-ts
test.describe(() => {
test.use({ colorScheme: 'dark' });
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
```
### param: Test.describe#2.callback
* since: v1.24
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe#2` ]. Any tests added in this callback will belong to the group.
2021-07-22 11:01:18 -07:00
2022-02-02 20:44:11 -08:00
## method: Test.describe.configure
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-02-02 20:44:11 -08:00
Set execution mode of execution for the enclosing scope. Can be executed either on the top level or inside a describe. Configuration applies to the entire scope, regardless of whether it run before or after the test
declaration.
2022-03-25 19:30:45 +01:00
Learn more about the execution modes [here ](../test-parallel.md ).
2022-02-02 20:44:11 -08:00
2022-02-04 13:50:46 -08:00
Running tests in parallel:
2022-06-10 16:34:31 -08:00
```js tab=js-js
2022-02-02 20:44:11 -08:00
// Run all the tests in the file concurrently using parallel workers.
test.describe.configure({ mode: 'parallel' });
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2022-02-02 20:44:11 -08:00
// Run all the tests in the file concurrently using parallel workers.
test.describe.configure({ mode: 'parallel' });
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});
```
2022-02-04 13:50:46 -08:00
Running tests sequentially:
2022-06-10 16:34:31 -08:00
```js tab=js-js
2022-02-02 20:44:11 -08:00
// Annotate tests as inter-dependent.
test.describe.configure({ mode: 'serial' });
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2022-02-02 20:44:11 -08:00
// Annotate tests as inter-dependent.
test.describe.configure({ mode: 'serial' });
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});
```
### option: Test.describe.configure.mode
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-03-08 16:02:31 -08:00
- `mode` < [TestMode]< "parallel"|"serial">>
2022-02-02 20:44:11 -08:00
2022-07-29 12:44:22 -07:00
## method: Test.describe.fixme
* since: v1.25
2022-08-09 21:47:17 +08:00
Declares a test group similarly to [`method: Test.describe#1` ]. Tests in this group are marked as "fixme" and will not be executed.
2022-07-29 12:44:22 -07:00
```js tab=js-js
test.describe.fixme('broken tests', () => {
test('example', async ({ page }) => {
// This test will not run
});
});
```
```js tab=js-ts
test.describe.fixme('broken tests', () => {
test('example', async ({ page }) => {
// This test will not run
});
});
```
### param: Test.describe.fixme.title
* since: v1.25
- `title` < [string]>
Group title.
### param: Test.describe.fixme.callback
* since: v1.25
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.fixme` ]. Any tests added in this callback will belong to the group, and will not be run.
2021-07-22 11:01:18 -07:00
## method: Test.describe.only
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
Declares a focused group of tests. If there are some focused tests or suites, all of them will be run but nothing else.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
test.describe.only('focused group', () => {
test('in the focused group', async ({ page }) => {
// This test will run
});
});
test('not in the focused group', async ({ page }) => {
// This test will not run
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
test.describe.only('focused group', () => {
test('in the focused group', async ({ page }) => {
// This test will run
});
});
test('not in the focused group', async ({ page }) => {
// This test will not run
});
```
### param: Test.describe.only.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.only.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.only` ]. Any tests added in this callback will belong to the group.
2021-08-10 21:26:45 -07:00
2021-09-02 15:42:07 -07:00
## method: Test.describe.parallel
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
Declares a group of tests that could be run in parallel. By default, tests in a single test file run one after another, but using [`method: Test.describe.parallel` ] allows them to run in parallel.
2022-02-08 14:36:14 -08:00
:::note
2022-02-02 20:44:11 -08:00
See [`method: Test.describe.configure` ] for the preferred way of configuring the execution mode.
2022-02-08 14:36:14 -08:00
:::
2022-02-02 20:44:11 -08:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-09-02 15:42:07 -07:00
test.describe.parallel('group', () => {
2022-02-02 20:44:11 -08:00
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});
2021-09-02 15:42:07 -07:00
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-09-02 15:42:07 -07:00
test.describe.parallel('group', () => {
2022-02-02 20:44:11 -08:00
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});
2021-09-02 15:42:07 -07:00
});
```
Note that parallel tests are executed in separate processes and cannot share any state or global variables. Each of the parallel tests executes all relevant hooks.
### param: Test.describe.parallel.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.parallel.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.parallel` ]. Any tests added in this callback will belong to the group.
## method: Test.describe.parallel.only
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
2021-09-26 21:30:11 -07:00
Declares a focused group of tests that could be run in parallel. This is similar to [`method: Test.describe.parallel` ], but focuses the group. If there are some focused tests or suites, all of them will be run but nothing else.
2021-09-02 15:42:07 -07:00
### param: Test.describe.parallel.only.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.parallel.only.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-02 15:42:07 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.parallel.only` ]. Any tests added in this callback will belong to the group.
2021-08-10 21:26:45 -07:00
## method: Test.describe.serial
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
Declares a group of tests that should always be run serially. If one of the tests fails, all subsequent tests are skipped. All tests in a group are retried together.
2022-02-08 14:36:14 -08:00
:::note
2022-02-02 20:44:11 -08:00
See [`method: Test.describe.configure` ] for the preferred way of configuring the execution mode.
2022-02-08 14:36:14 -08:00
:::
2022-02-02 20:44:11 -08:00
2021-08-10 21:26:45 -07:00
:::note
Using serial is not recommended. It is usually better to make your tests isolated, so they can be run independently.
:::
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-08-10 21:26:45 -07:00
test.describe.serial('group', () => {
2022-02-02 20:44:11 -08:00
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});
2021-08-10 21:26:45 -07:00
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-08-10 21:26:45 -07:00
test.describe.serial('group', () => {
2022-02-02 20:44:11 -08:00
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});
2021-08-10 21:26:45 -07:00
});
```
### param: Test.describe.serial.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.serial.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.serial` ]. Any tests added in this callback will belong to the group.
## method: Test.describe.serial.only
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
Declares a focused group of tests that should always be run serially. If one of the tests fails, all subsequent tests are skipped. All tests in a group are retried together. If there are some focused tests or suites, all of them will be run but nothing else.
:::note
Using serial is not recommended. It is usually better to make your tests isolated, so they can be run independently.
:::
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-08-10 21:26:45 -07:00
test.describe.serial.only('group', () => {
test('runs first', async ({ page }) => {
});
test('runs second', async ({ page }) => {
});
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-08-10 21:26:45 -07:00
test.describe.serial.only('group', () => {
test('runs first', async ({ page }) => {
});
test('runs second', async ({ page }) => {
});
});
```
### param: Test.describe.serial.only.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.serial.only.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-10 21:26:45 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.serial.only` ]. Any tests added in this callback will belong to the group.
2021-07-22 11:01:18 -07:00
2022-03-18 16:07:11 -07:00
## method: Test.describe.skip
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-03-18 16:07:11 -07:00
2022-07-06 13:54:11 -07:00
Declares a skipped test group, similarly to [`method: Test.describe#1` ]. Tests in the skipped group are never run.
2022-03-18 16:07:11 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2022-03-18 16:07:11 -07:00
test.describe.skip('skipped group', () => {
test('example', async ({ page }) => {
// This test will not run
});
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2022-03-18 16:07:11 -07:00
test.describe.skip('skipped group', () => {
test('example', async ({ page }) => {
// This test will not run
});
});
```
### param: Test.describe.skip.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-03-18 16:07:11 -07:00
- `title` < [string]>
Group title.
### param: Test.describe.skip.callback
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-03-18 16:07:11 -07:00
- `callback` < [function]>
A callback that is run immediately when calling [`method: Test.describe.skip` ]. Any tests added in this callback will belong to the group, and will not be run.
2021-07-22 11:01:18 -07:00
## property: Test.expect
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- type: < [Object]>
`expect` function can be used to create test assertions. Read [expect library documentation ](https://jestjs.io/docs/expect ) for more details.
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-18 15:45:52 -08:00
## method: Test.extend
2022-07-05 16:24:50 -08:00
* since: v1.10
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-18 15:45:52 -08:00
- returns: < [Test]>
Extends the `test` object by defining fixtures and/or options that can be used in the tests.
First define a fixture and/or an option.
2022-06-10 16:34:31 -08:00
```js tab=js-js
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-18 15:45:52 -08:00
// my-test.js
const base = require('@playwright/test ');
const { TodoPage } = require('./todo-page');
// Extend basic test by providing a "defaultItem" option and a "todoPage" fixture.
exports.test = base.test.extend({
// Define an option and provide a default value.
// We can later override it in the config.
defaultItem: ['Do stuff', { option: true }],
// Define a fixture. Note that it can use built-in fixture "page"
// and a new option "defaultItem".
todoPage: async ({ page, defaultItem }, use) => {
const todoPage = new TodoPage(page);
await todoPage.goto();
await todoPage.addToDo(defaultItem);
await use(todoPage);
await todoPage.removeAll();
},
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
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-18 15:45:52 -08:00
import { test as base } from '@playwright/test ';
import { TodoPage } from './todo-page';
export type Options = { defaultItem: string };
// Extend basic test by providing a "defaultItem" option and a "todoPage" fixture.
export const test = base.extend< Options & { todoPage: TodoPage } > ({
// Define an option and provide a default value.
// We can later override it in the config.
defaultItem: ['Do stuff', { option: true }],
// Define a fixture. Note that it can use built-in fixture "page"
// and a new option "defaultItem".
todoPage: async ({ page, defaultItem }, use) => {
const todoPage = new TodoPage(page);
await todoPage.goto();
await todoPage.addToDo(defaultItem);
await use(todoPage);
await todoPage.removeAll();
},
});
```
Then use the fixture in the test.
2022-06-10 16:34:31 -08:00
```js tab=js-js
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-18 15:45:52 -08:00
// example.spec.js
const { test } = require('./my-test');
test('test 1', async ({ todoPage }) => {
await todoPage.addToDo('my todo');
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
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-18 15:45:52 -08:00
// example.spec.ts
import { test } from './my-test';
test('test 1', async ({ todoPage }) => {
await todoPage.addToDo('my todo');
// ...
});
```
Configure the option in config file.
2022-06-10 16:34:31 -08:00
```js tab=js-js
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-18 15:45:52 -08:00
// playwright.config.js
// @ts -check
/** @type {import('@playwright/test ').PlaywrightTestConfig< { defaultItem: string }>} */
const config = {
projects: [
{
name: 'shopping',
use: { defaultItem: 'Buy milk' },
},
{
name: 'wellbeing',
use: { defaultItem: 'Exercise!' },
},
]
};
module.exports = config;
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
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-18 15:45:52 -08:00
// playwright.config.ts
2022-05-27 12:36:59 -07:00
import type { PlaywrightTestConfig } from '@playwright/test ';
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-18 15:45:52 -08:00
import { Options } from './my-test';
const config: PlaywrightTestConfig< Options > = {
projects: [
{
name: 'shopping',
use: { defaultItem: 'Buy milk' },
},
{
name: 'wellbeing',
use: { defaultItem: 'Exercise!' },
},
]
};
export default config;
```
2022-03-25 19:30:45 +01:00
Learn more about [fixtures ](../test-fixtures.md ) and [parametrizing tests ](../test-parameterize.md ).
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-18 15:45:52 -08:00
### param: Test.extend.fixtures
2022-07-05 16:24:50 -08:00
* since: v1.10
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-18 15:45:52 -08:00
- `fixtures` < [Object]>
2022-03-25 19:30:45 +01:00
An object containing fixtures and/or options. Learn more about [fixtures format ](../test-fixtures.md ).
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-18 15:45:52 -08:00
2022-04-06 13:36:20 -07:00
## method: Test.fail#1
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
Unconditonally marks a test as "should fail". Playwright Test runs this test and ensures that it is actually failing. This is useful for documentation purposes to acknowledge that some functionality is broken until it is fixed.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('not yet ready', async ({ page }) => {
test.fail();
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('not yet ready', async ({ page }) => {
test.fail();
// ...
});
```
2022-04-06 13:36:20 -07:00
## method: Test.fail#2
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
Conditionally mark a test as "should fail" with an optional description.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('fail in WebKit', async ({ page, browserName }) => {
test.fail(browserName === 'webkit', 'This feature is not implemented for Mac yet');
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('fail in WebKit', async ({ page, browserName }) => {
test.fail(browserName === 'webkit', 'This feature is not implemented for Mac yet');
// ...
});
```
2022-04-06 13:36:20 -07:00
### param: Test.fail#2.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
- `condition` < [boolean]>
Test is marked as "should fail" when the condition is `true` .
### param: Test.fail#2.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
## method: Test.fail#3
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
2022-07-06 13:54:11 -07:00
Conditionally mark all tests in a file or [`method: Test.describe#1` ] group as "should fail".
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test.fail(({ browserName }) => browserName === 'webkit');
test('fail in WebKit 1', async ({ page }) => {
// ...
});
test('fail in WebKit 2', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test.fail(({ browserName }) => browserName === 'webkit');
test('fail in WebKit 1', async ({ page }) => {
// ...
});
test('fail in WebKit 2', async ({ page }) => {
// ...
});
```
2022-04-06 13:36:20 -07:00
### param: Test.fail#3.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
- `callback` < [function]\([Fixtures]\):[boolean]>
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
A function that returns whether to mark as "should fail", based on test fixtures. Test or tests are marked as "should fail" when the return value is `true` .
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
### param: Test.fail#3.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2021-07-22 11:01:18 -07:00
Optional description that will be reflected in a test report.
2021-11-19 11:40:40 -08:00
## method: Test.fixme#1
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2021-11-19 11:40:40 -08:00
Declares a test to be fixed, similarly to [`method: Test.(call)` ]. This test will not be run.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-11-19 11:40:40 -08:00
test.fixme('test to be fixed', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-11-19 11:40:40 -08:00
test.fixme('test to be fixed', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2021-11-19 11:40:40 -08:00
### param: Test.fixme#1.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
- `title` < [string]>
Test title.
### param: Test.fixme#1.testFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
- `testFunction` < [function]\([Fixtures], [TestInfo]\)>
Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].
## method: Test.fixme#2
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
Mark a test as "fixme", with the intention to fix it. Test is immediately aborted when you call [`method: Test.fixme#2` ].
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-11-19 11:40:40 -08:00
test('test to be fixed', async ({ page }) => {
test.fixme();
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-11-19 11:40:40 -08:00
test('test to be fixed', async ({ page }) => {
test.fixme();
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-07-06 13:54:11 -07:00
Mark all tests in a file or [`method: Test.describe#1` ] group as "fixme".
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-11-19 11:40:40 -08:00
test.fixme();
2021-07-22 11:01:18 -07:00
2021-11-19 11:40:40 -08:00
test('test to be fixed 1', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
2021-11-19 11:40:40 -08:00
test('test to be fixed 2', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-11-19 11:40:40 -08:00
test.fixme();
2021-07-22 11:01:18 -07:00
2021-11-19 11:40:40 -08:00
test('test to be fixed 1', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
2021-11-19 11:40:40 -08:00
test('test to be fixed 2', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2021-11-19 11:40:40 -08:00
## method: Test.fixme#3
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
Conditionally mark a test as "fixme" with an optional description.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-11-19 11:40:40 -08:00
test('broken in WebKit', async ({ page, browserName }) => {
test.fixme(browserName === 'webkit', 'This feature is not implemented on Mac yet');
// ...
2021-07-22 11:01:18 -07:00
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-11-19 11:40:40 -08:00
test('broken in WebKit', async ({ page, browserName }) => {
test.fixme(browserName === 'webkit', 'This feature is not implemented on Mac yet');
// ...
2021-07-22 11:01:18 -07:00
});
```
2021-11-19 11:40:40 -08:00
### param: Test.fixme#3.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
- `condition` < [boolean]>
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
Test is marked as "fixme" when the condition is `true` .
2021-11-19 11:40:40 -08:00
### param: Test.fixme#3.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
2021-11-19 11:40:40 -08:00
## method: Test.fixme#4
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
2022-07-06 13:54:11 -07:00
Conditionally mark all tests in a file or [`method: Test.describe#1` ] group as "fixme".
2021-11-19 11:40:40 -08:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-11-19 11:40:40 -08:00
const { test, expect } = require('@playwright/test ');
test.fixme(({ browserName }) => browserName === 'webkit');
test('broken in WebKit 1', async ({ page }) => {
// ...
});
test('broken in WebKit 2', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-11-19 11:40:40 -08:00
import { test, expect } from '@playwright/test ';
test.fixme(({ browserName }) => browserName === 'webkit');
test('broken in WebKit 1', async ({ page }) => {
// ...
});
test('broken in WebKit 2', async ({ page }) => {
// ...
});
```
### param: Test.fixme#4.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-11-19 11:40:40 -08:00
- `callback` < [function]\([Fixtures]\):[boolean]>
A function that returns whether to mark as "fixme", based on test fixtures. Test or tests are marked as "fixme" when the return value is `true` .
### param: Test.fixme#4.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2021-11-19 11:40:40 -08:00
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
2021-07-22 11:01:18 -07:00
2021-12-06 09:25:11 -08:00
## method: Test.info
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-12-06 09:25:11 -08:00
- returns: < [TestInfo]>
Returns information about the currently running test. This method can only be called during the test execution, otherwise it throws.
2021-07-22 11:01:18 -07:00
## method: Test.only
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
Declares a focused test. If there are some focused tests or suites, all of them will be run but nothing else.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
test.only('focus this test', async ({ page }) => {
// Run only focused tests in the entire project.
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
test.only('focus this test', async ({ page }) => {
// Run only focused tests in the entire project.
});
```
### param: Test.only.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `title` < [string]>
Test title.
### param: Test.only.testFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 14:47:12 -07:00
- `testFunction` < [function]\([Fixtures], [TestInfo]\)>
2021-07-22 11:01:18 -07:00
Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].
## method: Test.setTimeout
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-03-25 19:30:45 +01:00
Changes the timeout for the test. Zero means no timeout. Learn more about [various timeouts ](../test-timeouts.md ).
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('very slow test', async ({ page }) => {
test.setTimeout(120000);
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('very slow test', async ({ page }) => {
test.setTimeout(120000);
// ...
});
```
2022-06-06 15:18:38 -07:00
Changing timeout from a slow `beforeEach` or `afterEach` hook. Note that this affects the test timeout that is shared with `beforeEach` /`afterEach` hooks.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
test.setTimeout(testInfo.timeout + 30000);
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
test.setTimeout(testInfo.timeout + 30000);
});
```
2022-06-06 15:18:38 -07:00
Changing timeout for a `beforeAll` or `afterAll` hook. Note this affects the hook's timeout, not the test timeout.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2022-06-06 15:18:38 -07:00
const { test, expect } = require('@playwright/test ');
test.beforeAll(async () => {
// Set timeout for this hook.
test.setTimeout(60000);
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2022-06-06 15:18:38 -07:00
import { test, expect } from '@playwright/test ';
test.beforeAll(async () => {
// Set timeout for this hook.
test.setTimeout(60000);
});
```
2022-07-06 13:54:11 -07:00
Changing timeout for all tests in a [`method: Test.describe#1` ] group.
2022-06-06 15:18:38 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2022-06-06 15:18:38 -07:00
const { test, expect } = require('@playwright/test ');
test.describe('group', () => {
// Applies to all tests in this group.
test.setTimeout(60000);
test('test one', async () => { /* ... */ });
test('test two', async () => { /* ... */ });
test('test three', async () => { /* ... */ });
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2022-06-06 15:18:38 -07:00
import { test, expect } from '@playwright/test ';
test.describe('group', () => {
// Applies to all tests in this group.
test.setTimeout(60000);
test('test one', async () => { /* ... */ });
test('test two', async () => { /* ... */ });
test('test three', async () => { /* ... */ });
});
```
2021-07-22 11:01:18 -07:00
Timeout for the currently running test is available through [`property: TestInfo.timeout` ].
### param: Test.setTimeout.timeout
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
- `timeout` < [int]>
Timeout in milliseconds.
2021-08-27 21:57:40 -07:00
## method: Test.skip#1
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2021-08-27 21:57:40 -07:00
Declares a skipped test, similarly to [`method: Test.(call)` ]. Skipped test is never run.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-07-29 14:33:37 -07:00
test.skip('broken test', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-07-29 14:33:37 -07:00
test.skip('broken test', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2021-08-27 21:57:40 -07:00
### param: Test.skip#1.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
- `title` < [string]>
Test title.
### param: Test.skip#1.testFunction
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
- `testFunction` < [function]\([Fixtures], [TestInfo]\)>
Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].
## method: Test.skip#2
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
Unconditionally skip a test. Test is immediately aborted when you call [`method: Test.skip#2` ].
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-08-27 21:57:40 -07:00
test('skipped test', async ({ page }) => {
test.skip();
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-08-27 21:57:40 -07:00
test('skipped test', async ({ page }) => {
test.skip();
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-07-06 13:54:11 -07:00
Unconditionally skip all tests in a file or [`method: Test.describe#1` ] group:
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
2021-08-27 21:57:40 -07:00
test.skip();
2021-07-22 11:01:18 -07:00
2021-08-27 21:57:40 -07:00
test('skipped test 1', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
2021-08-27 21:57:40 -07:00
test('skipped test 2', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
2021-08-27 21:57:40 -07:00
test.skip();
2021-07-22 11:01:18 -07:00
2021-08-27 21:57:40 -07:00
test('skipped test 1', async ({ page }) => {
2021-07-22 11:01:18 -07:00
// ...
});
2021-08-27 21:57:40 -07:00
test('skipped test 2', async ({ page }) => {
// ...
});
```
## method: Test.skip#3
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
Conditionally skip a test with an optional description.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-08-27 21:57:40 -07:00
const { test, expect } = require('@playwright/test ');
test('skip in WebKit', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'This feature is not implemented for Mac');
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-08-27 21:57:40 -07:00
import { test, expect } from '@playwright/test ';
test('skip in WebKit', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'This feature is not implemented for Mac');
2021-07-22 11:01:18 -07:00
// ...
});
```
2021-08-27 21:57:40 -07:00
Skip from [`method: Test.beforeEach` ] hook:
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test.beforeEach(async ({ page }) => {
test.skip(process.env.APP_VERSION === 'v1', 'There are no settings in v1');
await page.goto('/settings');
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test.beforeEach(async ({ page }) => {
test.skip(process.env.APP_VERSION === 'v1', 'There are no settings in v1');
await page.goto('/settings');
});
```
2021-08-27 21:57:40 -07:00
### param: Test.skip#3.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
- `condition` < [boolean]>
2022-04-06 13:36:20 -07:00
A skip condition. Test is skipped when the condition is `true` .
2021-08-27 21:57:40 -07:00
### param: Test.skip#3.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [void]|[string]>
2021-08-27 21:57:40 -07:00
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
2021-08-27 21:57:40 -07:00
## method: Test.skip#4
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Conditionally skips all tests in a file or [`method: Test.describe#1` ] group.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-08-27 21:57:40 -07:00
const { test, expect } = require('@playwright/test ');
test.skip(({ browserName }) => browserName === 'webkit');
test('skip in WebKit 1', async ({ page }) => {
// ...
});
test('skip in WebKit 2', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-08-27 21:57:40 -07:00
import { test, expect } from '@playwright/test ';
test.skip(({ browserName }) => browserName === 'webkit');
test('skip in WebKit 1', async ({ page }) => {
// ...
});
test('skip in WebKit 2', async ({ page }) => {
// ...
});
```
### param: Test.skip#4.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-27 21:57:40 -07:00
- `callback` < [function]\([Fixtures]\):[boolean]>
A function that returns whether to skip, based on test fixtures. Test or tests are skipped when the return value is `true` .
### param: Test.skip#4.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
## method: Test.slow#1
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
Unconditionally marks a test as "slow". Slow test will be given triple the default timeout.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('slow test', async ({ page }) => {
test.slow();
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('slow test', async ({ page }) => {
test.slow();
// ...
});
```
2022-06-06 15:18:38 -07:00
:::note
[`method: Test.slow#1` ] cannot be used in a `beforeAll` or `afterAll` hook. Use [`method: Test.setTimeout` ] instead.
:::
2022-04-06 13:36:20 -07:00
## method: Test.slow#2
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
Conditionally mark a test as "slow" with an optional description. Slow test will be given triple the default timeout.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test('slow in WebKit', async ({ page, browserName }) => {
test.slow(browserName === 'webkit', 'This feature is slow on Mac');
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test('slow in WebKit', async ({ page, browserName }) => {
test.slow(browserName === 'webkit', 'This feature is slow on Mac');
// ...
});
```
2022-04-06 13:36:20 -07:00
### param: Test.slow#2.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
- `condition` < [boolean]>
Test is marked as "slow" when the condition is `true` .
### param: Test.slow#2.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2022-04-06 13:36:20 -07:00
Optional description that will be reflected in a test report.
## method: Test.slow#3
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
2022-07-06 13:54:11 -07:00
Conditionally mark all tests in a file or [`method: Test.describe#1` ] group as "slow". Slow tests will be given triple the default timeout.
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test.slow(({ browserName }) => browserName === 'webkit');
test('slow in WebKit 1', async ({ page }) => {
// ...
});
test('slow in WebKit 2', async ({ page }) => {
// ...
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test.slow(({ browserName }) => browserName === 'webkit');
test('slow in WebKit 1', async ({ page }) => {
// ...
});
test('fail in WebKit 2', async ({ page }) => {
// ...
});
```
2022-04-06 13:36:20 -07:00
### param: Test.slow#3.condition
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 13:36:20 -07:00
- `callback` < [function]\([Fixtures]\):[boolean]>
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
A function that returns whether to mark as "slow", based on test fixtures. Test or tests are marked as "slow" when the return value is `true` .
2021-07-22 11:01:18 -07:00
2022-04-06 13:36:20 -07:00
### param: Test.slow#3.description
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-04-06 19:02:10 -07:00
- `description` ?< [string]>
2021-07-22 11:01:18 -07:00
Optional description that will be reflected in a test report.
2022-04-11 09:40:46 -07:00
## async method: Test.step
2022-07-05 16:24:50 -08:00
* since: v1.10
2022-08-10 11:21:13 -07:00
- returns: < [any]>
2021-08-02 22:11:37 -07:00
Declares a test step.
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-08-02 22:11:37 -07:00
const { test, expect } = require('@playwright/test ');
test('test', async ({ page }) => {
await test.step('Log in', async () => {
// ...
});
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-08-02 22:11:37 -07:00
import { test, expect } from '@playwright/test ';
test('test', async ({ page }) => {
await test.step('Log in', async () => {
// ...
});
});
```
2022-08-10 11:21:13 -07:00
The method returns value retuned by the step callback.
```js tab=js-js
const { test, expect } = require('@playwright/test ');
test('test', async ({ page }) => {
const user = await test.step('Log in', async () => {
// ...
return 'john';
});
expect(user).toBe('john');
});
```
```js tab=js-ts
import { test, expect } from '@playwright/test ';
test('test', async ({ page }) => {
const user = await test.step('Log in', async () => {
// ...
return 'john';
});
expect(user).toBe('john');
});
```
2021-08-02 22:11:37 -07:00
### param: Test.step.title
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-02 22:11:37 -07:00
- `title` < [string]>
Step name.
### param: Test.step.body
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-08-02 22:11:37 -07:00
- `body` < [function]\(\):[Promise]< [any]>>
Step body.
2021-07-22 11:01:18 -07:00
## method: Test.use
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-07-22 11:01:18 -07:00
2022-07-06 13:54:11 -07:00
Specifies options or fixtures to use in a single test file or a [`method: Test.describe#1` ] group. Most useful to set an option, for example set `locale` to configure `context` fixture. `test.use` can be called either in the global scope or inside `test.describe` , it's is an error to call it within `beforeEach` or `beforeAll` .
2021-07-22 11:01:18 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 11:01:18 -07:00
const { test, expect } = require('@playwright/test ');
test.use({ locale: 'en-US' });
test('test with locale', async ({ page }) => {
// Default context and page have locale as specified
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 11:01:18 -07:00
import { test, expect } from '@playwright/test ';
test.use({ locale: 'en-US' });
test('test with locale', async ({ page }) => {
// Default context and page have locale as specified
});
```
2021-09-17 17:13:47 -07:00
It is also possible to override a fixture by providing a function.
2021-07-22 14:47:12 -07:00
2022-06-10 16:34:31 -08:00
```js tab=js-js
2021-07-22 14:47:12 -07:00
const { test, expect } = require('@playwright/test ');
test.use({
locale: async ({}, use) => {
// Read locale from some configuration file.
const locale = await fs.promises.readFile('test-locale', 'utf-8');
await use(locale);
},
});
test('test with locale', async ({ page }) => {
// Default context and page have locale as specified
});
```
2022-06-10 16:34:31 -08:00
```js tab=js-ts
2021-07-22 14:47:12 -07:00
import { test, expect } from '@playwright/test ';
test.use({
locale: async ({}, use) => {
// Read locale from some configuration file.
const locale = await fs.promises.readFile('test-locale', 'utf-8');
await use(locale);
},
});
test('test with locale', async ({ page }) => {
// Default context and page have locale as specified
});
```
2021-07-22 11:01:18 -07:00
### param: Test.use.fixtures
2022-07-05 16:24:50 -08:00
* since: v1.10
2021-09-17 17:13:47 -07:00
- `options` < [TestOptions]>
2021-07-22 11:01:18 -07:00
2021-09-17 17:13:47 -07:00
An object with local options.
2021-07-22 11:01:18 -07:00