2021-05-28 06:30:03 +03:00
---
id: test-configuration
title: "Configuration"
---
<!-- TOC -->
2021-05-31 05:46:16 +03:00
## Configure browser, context, videos and screenshots
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
Playwright Tests supports browser and context options that you typically pass to [`method: BrowserType.launch`] and [`method: Browser.newContext`] methods, for example `headless` , `viewport` or `ignoreHTTPSErrors` . It also provides options to record video for the test or capture screenshot at the end.
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
You can specify any options either locally in a test file, or globally in the configuration file.
2021-05-28 06:30:03 +03:00
2021-06-15 11:11:15 +03:00
- `launchOptions` - Browser launch options match [`method: BrowserType.launch`] method.
- `contextOptions` - Context options match [`method: Browser.newContext`] method.
2021-05-31 01:14:44 +03:00
- `screenshot` option - whether to capture a screenshot after each test, off by default. Screenshot will appear in the test output directory, typically `test-results` .
2021-05-31 05:46:16 +03:00
- `'off'` - Do not capture screenshots.
- `'on'` - Capture screenshot after each test.
- `'only-on-failure'` - Capture screenshot after each test failure.
2021-06-08 21:22:07 +03:00
- `trace` option - whether to record trace for each test, off by default. Trace will appear in the test output directory, typically `test-results` .
- `'off'` - Do not record trace.
- `'on'` - Record trace for each test.
- `'retain-on-failure'` - Record trace for each test, but remove it from successful test runs.
2021-06-17 02:05:30 +03:00
- `'on-first-retry'` - Record trace only when retrying a test for the first time.
2021-05-31 05:46:16 +03:00
- `video` option - whether to record video for each test, off by default. Video will appear in the test output directory, typically `test-results` .
- `'off'` - Do not record video.
- `'on'` - Record video for each test.
- `'retain-on-failure'` - Record video for each test, but remove all videos from successful test runs.
2021-06-17 02:05:30 +03:00
- `'on-first-retry'` - Record video only when retrying a test for the first time.
2021-05-31 01:14:44 +03:00
### Global configuration
2021-05-31 05:46:16 +03:00
Create `playwright.config.js` (or `playwright.config.ts` ) and specify options in the `use` section.
2021-05-31 01:14:44 +03:00
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-05-31 01:14:44 +03:00
module.exports = {
use: {
// Browser options
headless: false,
2021-06-14 18:12:53 +03:00
launchOptions: {
slowMo: 50,
},
2021-05-31 01:14:44 +03:00
// Context options
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true,
2021-05-31 05:46:16 +03:00
// Artifacts
screenshot: 'only-on-failure',
2021-06-17 02:05:30 +03:00
video: 'on-first-retry',
2021-05-31 01:14:44 +03:00
},
};
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-06-04 00:46:58 +03:00
import { PlaywrightTestConfig } from '@playwright/test';
2021-05-31 01:14:44 +03:00
const config: PlaywrightTestConfig = {
use: {
// Browser options
headless: false,
2021-06-14 18:12:53 +03:00
launchOptions: {
slowMo: 50,
},
2021-05-31 01:14:44 +03:00
// Context options
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true,
2021-05-31 05:46:16 +03:00
// Artifacts
screenshot: 'only-on-failure',
2021-06-17 02:05:30 +03:00
video: 'on-first-retry',
2021-05-31 01:14:44 +03:00
},
};
export default config;
```
2021-05-31 05:46:16 +03:00
Now run tests as usual, Playwright Test will pick up the configuration file automatically.
2021-06-02 19:23:06 +03:00
```bash
2021-05-31 05:46:16 +03:00
npx playwright test --browser=firefox
```
2021-05-31 01:14:44 +03:00
If you put your configuration file in a different place, pass it with `--config` option.
2021-06-02 19:23:06 +03:00
```bash
2021-05-31 01:14:44 +03:00
npx playwright test --config=tests/my.config.js
```
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
### Local configuration
2021-05-31 05:46:16 +03:00
With `test.use()` you can override some options for a file or a `test.describe` block.
2021-05-28 06:30:03 +03:00
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-05-31 01:14:44 +03:00
// example.spec.js
2021-06-04 00:46:58 +03:00
const { test, expect } = require('@playwright/test');
2021-05-31 01:14:44 +03:00
// Run tests in this file with portrait-like viewport.
test.use({ viewport: { width: 600, height: 900 } });
test('my portrait test', async ({ page }) => {
// ...
});
2021-05-31 05:46:16 +03:00
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-05-31 05:46:16 +03:00
// example.spec.ts
2021-06-04 00:46:58 +03:00
import { test, expect } from '@playwright/test';
2021-05-31 05:46:16 +03:00
// Run tests in this file with portrait-like viewport.
test.use({ viewport: { width: 600, height: 900 } });
test('my portrait test', async ({ page }) => {
// ...
});
```
2021-05-31 01:14:44 +03:00
2021-06-03 01:31:51 +03:00
The same works inside describe.
```js js-flavor=js
2021-05-31 05:46:16 +03:00
// example.spec.js
2021-06-04 00:46:58 +03:00
const { test, expect } = require('@playwright/test');
2021-05-31 01:14:44 +03:00
test.describe('headed block', () => {
// Run tests in this describe block in headed mode.
test.use({ headless: false });
2021-05-31 05:46:16 +03:00
test('my headed test', async ({ page }) => {
2021-05-31 01:14:44 +03:00
// ...
});
});
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-05-31 01:14:44 +03:00
// example.spec.ts
2021-06-04 00:46:58 +03:00
import { test, expect } from '@playwright/test';
2021-05-31 01:14:44 +03:00
test.describe('headed block', () => {
// Run tests in this describe block in headed mode.
test.use({ headless: false });
2021-05-31 05:46:16 +03:00
test('my headed test', async ({ page }) => {
2021-05-31 01:14:44 +03:00
// ...
});
});
```
## Testing options
2021-05-31 05:46:16 +03:00
In addition to configuring [Browser] or [BrowserContext], videos or screenshots, Playwright Test has many options to configure how your tests are run. Below are the most common ones, see [advanced configuration ](./test-advanced.md ) for the full list.
2021-05-31 01:14:44 +03:00
- `forbidOnly` : Whether to exit with an error if any tests are marked as `test.only` . Useful on CI.
- `globalSetup` : Path to the global setup file. This file will be required and run before all the tests. It must export a single function.
- `globalTeardown` : Path to the global teardown file. This file will be required and run after all the tests. It must export a single function.
- `retries` : The maximum number of retry attempts per test.
- `testDir` : Directory with the test files.
- `testIgnore` : Glob patterns or regular expressions that should be ignored when looking for the test files. For example, `'**/test-assets'` .
- `testMatch` : Glob patterns or regular expressions that match test files. For example, `'**/todo-tests/*.spec.ts'` . By default, Playwright Test runs `.*(test|spec)\.(js|ts|mjs)` files.
- `timeout` : Time in milliseconds given to each test.
- `workers` : The maximum number of concurrent worker processes to use for parallelizing tests.
You can specify these options in the configuration file.
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-05-31 01:14:44 +03:00
// playwright.config.js
module.exports = {
// Look for test files in the "tests" directory, relative to this configuration file
testDir: 'tests',
// Each test is given 30 seconds
timeout: 30000,
// Forbid test.only on CI
forbidOnly: !!process.env.CI,
// Two retries for each test
retries: 2,
// Limit the number of workers on CI, use default locally
workers: process.env.CI ? 2 : undefined,
use: {
// Configure browser and context here
},
};
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-05-31 01:14:44 +03:00
// playwright.config.ts
2021-06-04 00:46:58 +03:00
import { PlaywrightTestConfig } from '@playwright/test';
2021-05-28 06:30:03 +03:00
const config: PlaywrightTestConfig = {
2021-05-31 01:14:44 +03:00
// Look for test files in the "tests" directory, relative to this configuration file
testDir: 'tests',
// Each test is given 30 seconds
timeout: 30000,
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
// Forbid test.only on CI
2021-05-28 06:30:03 +03:00
forbidOnly: !!process.env.CI,
2021-05-31 01:14:44 +03:00
// Two retries for each test
2021-05-28 06:30:03 +03:00
retries: 2,
2021-05-31 01:14:44 +03:00
// Limit the number of workers on CI, use default locally
workers: process.env.CI ? 2 : undefined,
use: {
// Configure browser and context here
},
};
2021-05-28 06:30:03 +03:00
export default config;
```
2021-05-31 01:14:44 +03:00
## Different options for each browser
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
To specify different options per browser, for example command line arguments for Chromium, create multiple projects in your configuration file. Below is an example that runs all tests in three browsers, with different options.
2021-05-28 06:30:03 +03:00
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-05-31 01:14:44 +03:00
// playwright.config.js
module.exports = {
// Put any shared options on the top level.
use: {
headless: true,
},
2021-05-28 06:30:03 +03:00
projects: [
{
2021-05-31 01:14:44 +03:00
name: 'Chromium',
2021-05-28 06:30:03 +03:00
use: {
2021-05-31 01:14:44 +03:00
// Configure the browser to use.
2021-05-28 06:30:03 +03:00
browserName: 'chromium',
2021-05-31 01:14:44 +03:00
// Any Chromium-specific options.
viewport: { width: 600, height: 800 },
},
},
{
name: 'Firefox',
use: { browserName: 'firefox' },
},
{
name: 'WebKit',
use: { browserName: 'webkit' },
},
],
};
```
2021-05-28 06:30:03 +03:00
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-05-31 01:14:44 +03:00
// playwright.config.ts
2021-06-04 00:46:58 +03:00
import { PlaywrightTestConfig } from '@playwright/test';
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
const config: PlaywrightTestConfig = {
// Put any shared options on the top level.
use: {
headless: true,
},
projects: [
{
name: 'Chromium',
use: {
// Configure the browser to use.
browserName: 'chromium',
// Any Chromium-specific options.
viewport: { width: 600, height: 800 },
2021-05-28 06:30:03 +03:00
},
},
2021-05-31 01:14:44 +03:00
{
name: 'Firefox',
use: { browserName: 'firefox' },
},
{
name: 'WebKit',
use: { browserName: 'webkit' },
},
2021-05-28 06:30:03 +03:00
],
};
export default config;
```
2021-05-31 05:46:16 +03:00
Playwright Test will run all projects by default.
2021-05-28 06:30:03 +03:00
2021-06-02 19:23:06 +03:00
```bash
2021-05-31 01:14:44 +03:00
$ npx playwright test
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
Running 3 tests using 3 workers
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
✓ example.spec.ts:3:1 › [Chromium] should work (2s)
✓ example.spec.ts:3:1 › [Firefox] should work (2s)
✓ example.spec.ts:3:1 › [WebKit] should work (2s)
2021-05-28 06:30:03 +03:00
```
2021-05-31 05:46:16 +03:00
Use `--project` command line option to run a single project.
2021-06-02 19:23:06 +03:00
```bash
2021-05-31 01:14:44 +03:00
$ npx playwright test --project=webkit
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
Running 1 test using 1 worker
✓ example.spec.ts:3:1 › [WebKit] should work (2s)
```
There are many more things you can do with projects:
- Run a subset of test by specifying different `testDir` for each project.
2021-05-31 05:46:16 +03:00
- Run tests in multiple configurations, for example with desktop Chromium and emulating Chrome for Android.
- Run "core" tests without retries to ensure stability of the core functionality, and use `retries` for other tests.
- And much more! See [advanced configuration ](./test-advanced.md ) for more details.
2021-05-28 06:30:03 +03:00
2021-05-31 01:14:44 +03:00
:::note
`--browser` command line option is not compatible with projects. Specify `browserName` in each project instead.
:::
2021-06-01 08:01:21 +03:00
## Mobile emulation
You can use configuration file to make default `context` emulate a mobile device.
Here is an example configuration that runs tests in "Pixel 4" and "iPhone 11" emulation modes. Note that it uses the [projects ](./test-advanced.md#projects ) feature to run the same set of tests in multiple configurations.
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-06-01 08:01:21 +03:00
// playwright.config.js
const { devices } = require('playwright');
module.exports = {
projects: [
// "Pixel 4" tests use Chromium browser.
{
name: 'Pixel 4',
use: {
browserName: 'chromium',
...devices['Pixel 4'],
},
},
// "iPhone 11" tests use WebKit browser.
{
name: 'iPhone 11',
use: {
browserName: 'webkit',
...devices['iPhone 11'],
},
},
],
};
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-06-01 08:01:21 +03:00
// playwright.config.ts
2021-06-04 00:46:58 +03:00
import { PlaywrightTestConfig } from '@playwright/test';
2021-06-01 08:01:21 +03:00
import { devices } from 'playwright';
const config: PlaywrightTestConfig = {
projects: [
// "Pixel 4" tests use Chromium browser.
{
name: 'Pixel 4',
use: {
browserName: 'chromium',
...devices['Pixel 4'],
},
},
// "iPhone 11" tests use WebKit browser.
{
name: 'iPhone 11',
use: {
browserName: 'webkit',
...devices['iPhone 11'],
},
},
],
};
export default config;
```
## Network mocking
You don't have to configure anything to mock network requests. Just define a custom [Route] that mocks network for a browser context.
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-06-01 08:01:21 +03:00
// example.spec.js
2021-06-04 00:46:58 +03:00
const { test, expect } = require('@playwright/test');
2021-06-01 08:01:21 +03:00
test.beforeEach(async ({ context }) => {
// Block any css requests for each test in this file.
await context.route(/.css/, route => route.abort());
});
test('loads page without css', async ({ page }) => {
await page.goto('https://playwright.dev');
// ... test goes here
});
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-06-01 08:01:21 +03:00
// example.spec.ts
2021-06-04 00:46:58 +03:00
import { test, expect } from '@playwright/test';
2021-06-01 08:01:21 +03:00
test.beforeEach(async ({ context }) => {
// Block any css requests for each test in this file.
await context.route(/.css/, route => route.abort());
});
test('loads page without css', async ({ page }) => {
await page.goto('https://playwright.dev');
// ... test goes here
});
```
Alternatively, you can use [`method: Page.route`] to mock network in a single test.
2021-06-03 01:31:51 +03:00
```js js-flavor=js
2021-06-01 08:01:21 +03:00
// example.spec.js
2021-06-04 00:46:58 +03:00
const { test, expect } = require('@playwright/test');
2021-06-01 08:01:21 +03:00
test('loads page without images', async ({ page }) => {
// Block png and jpeg images.
await page.route(/(png|jpeg)$/, route => route.abort());
await page.goto('https://playwright.dev');
// ... test goes here
});
```
2021-06-03 01:31:51 +03:00
```js js-flavor=ts
2021-06-01 08:01:21 +03:00
// example.spec.ts
2021-06-04 00:46:58 +03:00
import { test, expect } from '@playwright/test';
2021-06-01 08:01:21 +03:00
test('loads page without images', async ({ page }) => {
// Block png and jpeg images.
await page.route(/(png|jpeg)$/, route => route.abort());
await page.goto('https://playwright.dev');
// ... test goes here
});
```