2021-05-27 20:30:03 -07:00
---
id: test-configuration
2023-03-07 08:57:13 +01:00
title: "Test configuration"
2021-05-27 20:30:03 -07:00
---
2023-10-06 15:08:51 +02:00
## Introduction
2023-03-07 08:57:13 +01:00
Playwright has many options to configure how your tests are run. You can specify these options in the configuration file. Note that test runner options are **top-level** , do not put them into the `use` section.
2021-05-27 20:30:03 -07:00
2023-03-07 08:57:13 +01:00
## Basic Configuration
2021-05-27 20:30:03 -07:00
2023-03-07 08:57:13 +01:00
Here are some of the most common configuration options.
2021-05-30 15:14:44 -07:00
2023-03-06 17:17:34 +01:00
```js
2023-04-04 09:43:51 -04:00
import { defineConfig, devices } from '@playwright/test ';
2023-03-06 17:17:34 +01:00
2023-01-12 13:12:02 -08:00
export default defineConfig({
2023-03-07 08:57:13 +01:00
// Look for test files in the "tests" directory, relative to this configuration file.
testDir: 'tests',
2022-09-08 18:57:20 +02:00
2023-03-07 08:57:13 +01:00
// Run all tests in parallel.
fullyParallel: true,
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// Fail the build on CI if you accidentally left test.only in the source code.
forbidOnly: !!process.env.CI,
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// Retry on CI only.
retries: process.env.CI ? 2 : 0,
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// Opt out of parallel tests on CI.
workers: process.env.CI ? 1 : undefined,
2023-01-12 13:12:02 -08:00
2023-03-07 08:57:13 +01:00
// Reporter to use
reporter: 'html',
2023-03-06 17:17:34 +01:00
2021-07-12 17:18:02 -07:00
use: {
2023-03-07 08:57:13 +01:00
// Base URL to use in actions like `await page.goto('/')` .
baseURL: 'http://127.0.0.1:3000',
2023-01-12 13:12:02 -08:00
2023-03-07 08:57:13 +01:00
// Collect trace when retrying the failed test.
trace: 'on-first-retry',
2023-03-06 17:17:34 +01:00
},
2023-03-07 08:57:13 +01:00
// Configure projects for major browsers.
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
2023-03-06 17:17:34 +01:00
},
2023-03-07 08:57:13 +01:00
],
// Run your local dev server before starting the tests.
webServer: {
command: 'npm run start',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
2023-03-06 17:17:34 +01:00
},
2021-07-12 17:18:02 -07:00
});
```
2023-03-07 08:57:13 +01:00
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
2023-03-07 08:57:13 +01:00
| [`property: TestConfig.forbidOnly` ] | Whether to exit with an error if any tests are marked as `test.only` . Useful on CI.|
2024-06-04 04:31:36 -04:00
| [`property: TestConfig.fullyParallel` ] | have all tests in all files to run in parallel. See [Parallelism ](./test-parallel ) and [Sharding ](./test-sharding ) for more details. |
2023-03-07 08:57:13 +01:00
| [`property: TestConfig.projects` ] | Run tests in multiple configurations or on multiple browsers |
| [`property: TestConfig.reporter` ] | Reporter to use. See [Test Reporters ](/test-reporters.md ) to learn more about which reporters are available. |
| [`property: TestConfig.retries` ] | The maximum number of retry attempts per test. See [Test Retries ](/test-retries.md ) to learn more about retries.|
| [`property: TestConfig.testDir` ] | Directory with the test files. |
| [`property: TestConfig.use` ] | Options with `use{}` |
| [`property: TestConfig.webServer` ] | To launch a server during the tests, use the `webServer` option |
2024-06-04 04:31:36 -04:00
| [`property: TestConfig.workers` ] | The maximum number of concurrent worker processes to use for parallelizing tests. Can also be set as percentage of logical CPU cores, e.g. `'50%'.` . See [Parallelism ](./test-parallel ) and [Sharding ](./test-sharding ) for more details. |
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
## Filtering Tests
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
Filter tests by glob patterns or regular expressions.
2023-01-12 13:12:02 -08:00
2023-05-10 18:38:12 +02:00
```js title="playwright.config.ts"
2023-01-12 13:12:02 -08:00
import { defineConfig } from '@playwright/test ';
2023-03-06 17:17:34 +01:00
2023-01-12 13:12:02 -08:00
export default defineConfig({
2023-06-27 11:53:53 +02:00
// Glob patterns or regular expressions to ignore test files.
2023-03-07 08:57:13 +01:00
testIgnore: '*test-assets',
2021-07-12 17:18:02 -07:00
2023-06-27 11:53:53 +02:00
// Glob patterns or regular expressions that match test files.
2023-03-07 08:57:13 +01:00
testMatch: '*todo-tests/*.spec.ts',
2023-01-12 13:12:02 -08:00
});
2021-07-12 17:18:02 -07:00
```
2023-03-07 08:57:13 +01:00
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
2023-03-07 08:57:13 +01:00
| [`property: TestConfig.testIgnore` ] | Glob patterns or regular expressions that should be ignored when looking for the test files. For example, `'*test-assets'` |
2023-05-16 13:00:25 +01:00
| [`property: TestConfig.testMatch` ] | Glob patterns or regular expressions that match test files. For example, `'*todo-tests/*.spec.ts'` . By default, Playwright runs < code > .*(test& #124 ; spec)\.(js& #124 ; ts& #124 ; mjs)</ code > files. |
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
## Advanced Configuration
2021-07-12 17:18:02 -07:00
2023-05-10 18:38:12 +02:00
```js title="playwright.config.ts"
2023-03-06 17:17:34 +01:00
import { defineConfig } from '@playwright/test ';
2021-07-12 17:18:02 -07:00
2023-03-06 17:17:34 +01:00
export default defineConfig({
2023-03-07 08:57:13 +01:00
// Folder for test artifacts such as screenshots, videos, traces, etc.
outputDir: 'test-results',
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// path to the global setup files.
globalSetup: require.resolve('./global-setup'),
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// path to the global teardown files.
globalTeardown: require.resolve('./global-teardown'),
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
// Each test is given 30 seconds.
timeout: 30000,
2023-01-12 13:12:02 -08:00
});
2021-07-12 17:18:02 -07:00
```
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
2023-03-07 08:57:13 +01:00
| [`property: TestConfig.globalSetup` ] | Path to the global setup file. This file will be required and run before all the tests. It must export a single function. |
| [`property: TestConfig.globalTeardown` ] |Path to the global teardown file. This file will be required and run after all the tests. It must export a single function. |
| [`property: TestConfig.outputDir` ] | Folder for test artifacts such as screenshots, videos, traces, etc. |
| [`property: TestConfig.timeout` ] | Playwright enforces a [timeout ](./test-timeouts.md ) for each test, 30 seconds by default. Time spent by the test function, fixtures, beforeEach and afterEach hooks is included in the test timeout. |
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
## Expect Options
2021-07-12 17:18:02 -07:00
2023-03-07 08:57:13 +01:00
Configuration for the expect assertion library.
2023-01-12 13:12:02 -08:00
2023-05-10 18:38:12 +02:00
```js title="playwright.config.ts"
2023-01-12 13:12:02 -08:00
import { defineConfig } from '@playwright/test ';
2023-03-06 17:17:34 +01:00
2023-01-12 13:12:02 -08:00
export default defineConfig({
2023-03-07 08:57:13 +01:00
expect: {
// Maximum time expect() should wait for the condition to be met.
timeout: 5000,
toHaveScreenshot: {
// An acceptable amount of pixels that could be different, unset by default.
maxDiffPixels: 10,
},
2023-09-14 12:52:41 -07:00
toMatchSnapshot: {
// An acceptable ratio of pixels that are different to the
// total amount of pixels, between 0 and 1.
maxDiffPixelRatio: 0.1,
},
2021-07-12 17:18:02 -07:00
},
2023-06-27 14:39:44 +02:00
2023-01-12 13:12:02 -08:00
});
2021-07-12 17:18:02 -07:00
```
2023-03-07 08:57:13 +01:00
| Option | Description |
| :- | :- |
2023-09-14 12:52:41 -07:00
| [`property: TestConfig.expect` ] | [Web first assertions ](./test-assertions.md ) like `expect(locator).toHaveText()` have a separate timeout of 5 seconds by default. This is the maximum time the `expect()` should wait for the condition to be met. Learn more about [test and expect timeouts ](./test-timeouts.md ) and how to set them for a single test. |
2024-03-03 02:00:16 +01:00
| [`method: PageAssertions.toHaveScreenshot#1` ] | Configuration for the `expect(locator).toHaveScreenshot()` method. |
2023-09-14 12:52:41 -07:00
| [`method: SnapshotAssertions.toMatchSnapshot#1` ]| Configuration for the `expect(locator).toMatchSnapshot()` method.|
2023-03-07 08:57:13 +01:00