TestConfig
Playwright Test provides many options to configure how your tests are collected and executed, for example timeout
or testDir
. These options are described in the TestConfig object in the configuration file.
Playwright Test supports running multiple test projects at the same time. Project-specific options should be put to testConfig.projects, but top-level TestConfig can also define base options shared between all projects.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
timeout: 30000,
globalTimeout: 600000,
reporter: 'list',
testDir: './tests',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
timeout: 30000,
globalTimeout: 600000,
reporter: 'list',
testDir: './tests',
};
module.exports = config;
- testConfig.expect
- testConfig.forbidOnly
- testConfig.fullyParallel
- testConfig.globalSetup
- testConfig.globalTeardown
- testConfig.globalTimeout
- testConfig.grep
- testConfig.grepInvert
- testConfig.ignoreSnapshots
- testConfig.maxFailures
- testConfig.metadata
- testConfig.name
- testConfig.outputDir
- testConfig.preserveOutput
- testConfig.projects
- testConfig.quiet
- testConfig.repeatEach
- testConfig.reporter
- testConfig.reportSlowTests
- testConfig.retries
- testConfig.shard
- testConfig.snapshotDir
- testConfig.testDir
- testConfig.testIgnore
- testConfig.testMatch
- testConfig.timeout
- testConfig.updateSnapshots
- testConfig.use
- testConfig.webServer
- testConfig.workers
testConfig.expect
Added in: v1.10- type: <Object>
timeout?
<number> Default timeout for async expect matchers in milliseconds, defaults to 5000ms.toHaveScreenshot?
<Object> Configuration for the expect(page).toHaveScreenshot(name[, options]) method.threshold?
<number> an acceptable perceived color difference in the YIQ color space between the same pixel in compared images, between zero (strict) and one (lax). Defaults to0.2
.maxDiffPixels?
<number> an acceptable amount of pixels that could be different, unset by default.maxDiffPixelRatio?
<number> an acceptable ratio of pixels that are different to the total amount of pixels, between0
and1
, unset by default.animations?
<"allow"|"disabled"> Seeanimations
in page.screenshot([options]). Defaults to"disabled"
.caret?
<"hide"|"initial"> Seecaret
in page.screenshot([options]). Defaults to"hide"
.scale?
<"css"|"device"> Seescale
in page.screenshot([options]). Defaults to"css"
.
toMatchSnapshot?
<Object> Configuration for the expect(screenshot).toMatchSnapshot(name[, options]) method.threshold?
<number> an acceptable perceived color difference in the YIQ color space between the same pixel in compared images, between zero (strict) and one (lax). Defaults to0.2
.maxDiffPixels?
<number> an acceptable amount of pixels that could be different, unset by default.maxDiffPixelRatio?
<number> an acceptable ratio of pixels that are different to the total amount of pixels, between0
and1
, unset by default.
Configuration for the expect
assertion library. Learn more about various timeouts.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
expect: {
timeout: 10000,
toMatchSnapshot: {
maxDiffPixels: 10,
},
},
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
expect: {
timeout: 10000,
toMatchSnapshot: {
maxDiffPixels: 10,
},
},
};
module.exports = config;
testConfig.forbidOnly
Added in: v1.10- type: <boolean>
Whether to exit with an error if any tests or groups are marked as test.only(title, testFunction) or test.describe.only(title, callback). Useful on CI.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
forbidOnly: !!process.env.CI,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
forbidOnly: !!process.env.CI,
};
module.exports = config;
testConfig.fullyParallel
Added in: v1.10- type: <boolean>
Playwright Test runs tests in parallel. In order to achieve that, it runs several worker processes that run at the same time. By default, test files are run in parallel. Tests in a single file are run in order, in the same worker process.
You can configure entire test run to concurrently execute all tests in all files using this option.
testConfig.globalSetup
Added in: v1.10- type: <string>
Path to the global setup file. This file will be required and run before all the tests. It must export a single function that takes a [TestConfig
] argument.
Learn more about global setup and teardown.
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
globalSetup: './global-setup',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
globalSetup: './global-setup',
};
module.exports = config;
testConfig.globalTeardown
Added in: v1.10- type: <string>
Path to the global teardown file. This file will be required and run after all the tests. It must export a single function. See also testConfig.globalSetup.
Learn more about global setup and teardown.
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
globalTeardown: './global-teardown',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
globalTeardown: './global-teardown',
};
module.exports = config;
testConfig.globalTimeout
Added in: v1.10- type: <number>
Maximum time in milliseconds the whole test suite can run. Zero timeout (default) disables this behavior. Useful on CI to prevent broken setup from running too long and wasting resources. Learn more about various timeouts.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
globalTimeout: process.env.CI ? 60 * 60 * 1000 : undefined,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
globalTimeout: process.env.CI ? 60 * 60 * 1000 : undefined,
};
module.exports = config;
testConfig.grep
Added in: v1.10Filter to only run tests with a title matching one of the patterns. For example, passing grep: /cart/
should only run tests with "cart" in the title. Also available in the command line with the -g
option.
grep
option is also useful for tagging tests.
testConfig.grepInvert
Added in: v1.10Filter to only run tests with a title not matching one of the patterns. This is the opposite of testConfig.grep. Also available in the command line with the --grep-invert
option.
grepInvert
option is also useful for tagging tests.
testConfig.ignoreSnapshots
Added in: v1.26- type: <boolean>
Whether to skip snapshot expectations, such as expect(value).toMatchSnapshot()
and await expect(page).toHaveScreenshot()
.
testConfig.maxFailures
Added in: v1.10- type: <number>
The maximum number of test failures for the whole test suite run. After reaching this number, testing will stop and exit with an error. Setting to zero (default) disables this behavior.
Also available in the command line with the --max-failures
and -x
options.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
maxFailures: process.env.CI ? 1 : 0,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
maxFailures: process.env.CI ? 1 : 0,
};
module.exports = config;
testConfig.metadata
Added in: v1.10- type: <[Metadata]>
Metadata that will be put directly to the test report serialized as JSON.
testConfig.name
Added in: v1.10- type: <string>
Config name is visible in the report and during test execution, unless overridden by testProject.name.
testConfig.outputDir
Added in: v1.10- type: <string>
The output directory for files created during test execution. Defaults to <package.json-directory>/test-results
.
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
outputDir: './test-results',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
outputDir: './test-results',
};
module.exports = config;
This directory is cleaned at the start. When running a test, a unique subdirectory inside the testConfig.outputDir is created, guaranteeing that test running in parallel do not conflict. This directory can be accessed by testInfo.outputDir and testInfo.outputPath(...pathSegments).
Here is an example that uses testInfo.outputPath(...pathSegments) to create a temporary file.
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('example test', async ({}, testInfo) => {
const file = testInfo.outputPath('temporary-file.txt');
await fs.promises.writeFile(file, 'Put some data to the file', 'utf8');
});
const { test, expect } = require('@playwright/test');
const fs = require('fs');
test('example test', async ({}, testInfo) => {
const file = testInfo.outputPath('temporary-file.txt');
await fs.promises.writeFile(file, 'Put some data to the file', 'utf8');
});
testConfig.preserveOutput
Added in: v1.10- type: <"always"|"never"|"failures-only">
Whether to preserve test output in the testConfig.outputDir. Defaults to 'always'
.
'always'
- preserve output for all tests;'never'
- do not preserve output for any tests;'failures-only'
- only preserve output for failed tests.
testConfig.projects
Added in: v1.10- type: <Array<TestProject>>
Playwright Test supports running multiple test projects at the same time. See TestProject for more information.
testConfig.quiet
Added in: v1.10- type: <boolean>
Whether to suppress stdio and stderr output from the tests.
testConfig.repeatEach
Added in: v1.10- type: <number>
The number of times to repeat each test, useful for debugging flaky tests.
testConfig.reportSlowTests
Added in: v1.10Whether to report slow test files. Pass null
to disable this feature.
Test files that took more than threshold
milliseconds are considered slow, and the slowest ones are reported, no more than max
number of them. Passing zero as max
reports all test files that exceed the threshold.
testConfig.reporter
Added in: v1.10The list of reporters to use. Each reporter can be:
- A builtin reporter name like
'list'
or'json'
. - A module name like
'my-awesome-reporter'
. - A relative path to the reporter like
'./reporters/my-awesome-reporter.js'
.
You can pass options to the reporter in a tuple like ['json', { outputFile: './report.json' }]
.
Learn more in the reporters guide.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
reporter: 'line',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
reporter: 'line',
};
module.exports = config;
testConfig.retries
Added in: v1.10- type: <number>
The maximum number of retry attempts given to failed tests. By default failing tests are not retried. Learn more about test retries.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
retries: 2,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
retries: 2,
};
module.exports = config;
testConfig.shard
Added in: v1.10Shard tests and execute only the selected shard. Specify in the one-based form like { total: 5, current: 2 }
.
Learn more about parallelism and sharding with Playwright Test.
testConfig.snapshotDir
Added in: v1.10- type: <string>
The base directory, relative to the config file, for snapshot files created with toMatchSnapshot
. Defaults to testConfig.testDir.
The directory for each test can be accessed by testInfo.snapshotDir and testInfo.snapshotPath(...pathSegments).
This path will serve as the base directory for each test file snapshot directory. Setting snapshotDir
to 'snapshots'
, the testInfo.snapshotDir would resolve to snapshots/a.spec.js-snapshots
.
testConfig.testDir
Added in: v1.10- type: <string>
Directory that will be recursively scanned for test files. Defaults to the directory of the configuration file.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
testDir: './tests/playwright',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
testDir: './tests/playwright',
};
module.exports = config;
testConfig.testIgnore
Added in: v1.10Files matching one of these patterns are not executed as test files. Matching is performed against the absolute file path. Strings are treated as glob patterns.
For example, '**/test-assets/**'
will ignore any files in the test-assets
directory.
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
testIgnore: '**/test-assets/**',
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
testIgnore: '**/test-assets/**',
};
module.exports = config;
testConfig.testMatch
Added in: v1.10Only the files matching one of these patterns are executed as test files. Matching is performed against the absolute file path. Strings are treated as glob patterns.
By default, Playwright Test looks for files matching .*(test|spec)\.(js|ts|mjs)
.
- TypeScript
- JavaScript
// playwright.config.ts
import { type PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
testMatch: /.*\.e2e\.js/,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
testMatch: /.*\.e2e\.js/,
};
module.exports = config;
testConfig.timeout
Added in: v1.10- type: <number>
Timeout for each test in milliseconds. Defaults to 30 seconds.
This is a base timeout for all tests. In addition, each test can configure its own timeout with test.setTimeout(timeout). Learn more about various timeouts.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
timeout: 5 * 60 * 1000,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
timeout: 5 * 60 * 1000,
};
module.exports = config;
testConfig.updateSnapshots
Added in: v1.10- type: <"all"|"none"|"missing">
Whether to update expected snapshots with the actual results produced by the test run. Defaults to 'missing'
.
'all'
- All tests that are executed will update snapshots that did not match. Matching snapshots will not be updated.'none'
- No snapshots are updated.'missing'
- Missing snapshots are created, for example when authoring a new test and running it for the first time. This is the default.
Learn more about snapshots.
testConfig.use
Added in: v1.10- type: <TestOptions>
Global options for all tests, for example testOptions.browserName. Learn more about configuration and see available options.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
use: {
browserName: 'chromium',
},
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
use: {
browserName: 'chromium',
},
};
module.exports = config;
testConfig.webServer
Added in: v1.10- type: <Object|Array<Object>>
command
<string> Shell command to start. For examplenpm run start
..port?
<number> The port that your http server is expected to appear on. It does wait until it accepts connections. Exactly one ofport
orurl
is required.url?
<string> The url on your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections. Exactly one ofport
orurl
is required.ignoreHTTPSErrors?
<boolean> Whether to ignore HTTPS errors when fetching theurl
. Defaults tofalse
.timeout?
<number> How long to wait for the process to start up and be available in milliseconds. Defaults to 60000.reuseExistingServer?
<boolean> If true, it will re-use an existing server on theport
orurl
when available. If no server is running on thatport
orurl
, it will run the command to start a new server. Iffalse
, it will throw if an existing process is listening on theport
orurl
. This should be commonly set to!process.env.CI
to allow the local dev server when running tests locally.cwd?
<string> Current working directory of the spawned process, defaults to the directory of the configuration file.env?
<Object<string, string>> Environment variables to set for the command,process.env
by default.
Launch a development web server (or multiple) during the tests.
If the port is specified, Playwright Test will wait for it to be available on 127.0.0.1
or ::1
, before running the tests. If the url is specified, Playwright Test will wait for the URL to return a 2xx, 3xx, 400, 401, 402, or 403 status code before running the tests.
For continuous integration, you may want to use the reuseExistingServer: !process.env.CI
option which does not use an existing server on the CI. To see the stdout, you can set the DEBUG=pw:webserver
environment variable.
The port
(but not the url
) gets passed over to Playwright as a testOptions.baseURL. For example port 8080
produces baseURL
equal http://localhost:8080
. If webServer
is specified as an array, you must explicitly configure the baseURL
(even if it only has one entry).
It is also recommended to specify testOptions.baseURL in the config, so that tests could use relative urls.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
webServer: {
command: 'npm run start',
port: 3000,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
use: {
baseURL: 'http://localhost:3000/',
},
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
webServer: {
command: 'npm run start',
port: 3000,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
use: {
baseURL: 'http://localhost:3000/',
},
};
module.exports = config;
Now you can use a relative path when navigating the page:
- TypeScript
- JavaScript
// test.spec.ts
import { test } from '@playwright/test';
test('test', async ({ page }) => {
// This will result in http://localhost:3000/foo
await page.goto('/foo');
});
// test.spec.js
const { test } = require('@playwright/test');
test('test', async ({ page }) => {
// This will result in http://localhost:3000/foo
await page.goto('/foo');
});
Multiple web servers (or background processes) can be launched:
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
webServer: [
{
command: 'npm run start',
port: 3000,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
{
command: 'npm run backend',
port: 3333,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
}
],
use: {
baseURL: 'http://localhost:3000/',
},
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
webServer: [
{
command: 'npm run start',
port: 3000,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
{
command: 'npm run backend',
port: 3333,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
}
],
use: {
baseURL: 'http://localhost:3000/',
},
};
module.exports = config;
testConfig.workers
Added in: v1.10The maximum number of concurrent worker processes to use for parallelizing tests. Can also be set as percentage of logical CPU cores, e.g. '50%'.
Playwright Test uses worker processes to run tests. There is always at least one worker process, but more can be used to speed up test execution.
Defaults to half of the number of logical CPU cores. Learn more about parallelism and sharding with Playwright Test.
- TypeScript
- JavaScript
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
workers: 3,
};
export default config;
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
workers: 3,
};
module.exports = config;