playwright/docs/src/test-configuration-js.md

299 lines
11 KiB
Markdown
Raw Normal View History

2021-05-27 20:30:03 -07:00
---
id: test-configuration
2023-03-06 17:17:34 +01:00
title: "Test Options with use"
2021-05-27 20:30:03 -07:00
---
2023-03-06 17:17:34 +01:00
In addition to configuring the test runner you can also configure [Emulation](#emulation-options), [Network](#network-options) and [Recording](#recording-options) for the [Browser] or [BrowserContext],. These options are passed to the `use: {}` object in the Playwright config.
2021-05-27 20:30:03 -07:00
2023-03-06 17:17:34 +01:00
### Basic Options
2021-05-27 20:30:03 -07:00
2023-03-06 17:17:34 +01:00
Set the base URL and storage state for all tests:
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
2023-03-06 17:17:34 +01:00
// Base URL to use in actions like `await page.goto('/')`.
baseURL: 'http://127.0.0.1:3000'
2023-03-06 17:17:34 +01:00
// Populates context with given storage state.
storageState: 'state.json',
},
});
```
2022-09-08 18:57:20 +02:00
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
| [`property: TestOptions.baseURL`] | Base URL used for all pages in the context. Allows navigating by using just the path, for example `page.goto('/settings')`. |
| [`property: TestOptions.storageState`] | Populates context with given storage state. Useful for easy authentication, [learn more](./auth.md). |
2023-03-06 17:17:34 +01:00
### Emulation Options
2023-03-06 17:17:34 +01:00
With Playwright you can emulate a real device such as a mobile phone or tablet. See our [guide on projects](./test-projects.md) for more info on emulating devices. You can also emulate the `"geolocation"`, `"locale"` and `"timezone"` for all tests or for a specific test as well as set the `"permissions"` to show notifications or change the `"colorScheme"`. See our [Emulation](./emulation.md) guide to learn more.
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
2023-03-06 17:17:34 +01:00
// Emulates `'prefers-colors-scheme'` media feature.
colorScheme: 'dark',
2023-03-06 17:17:34 +01:00
// Context geolocation.
geolocation: { longitude: 12.492507, latitude: 41.889938 },
2023-03-06 17:17:34 +01:00
// Emulates the user locale.
locale: 'en-GB',
2023-03-06 17:17:34 +01:00
// Grants specified permissions to the browser context.
permissions: 'geolocation',
2023-03-06 17:17:34 +01:00
// Emulates the user timezone.
timezoneId: 'Europe/Paris',
2023-03-06 17:17:34 +01:00
// Viewport used for all pages in the context.
viewport: { width: 1280, height: 720 },
},
});
```
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
| [`property: TestOptions.colorScheme`] | [Emulates](./emulation.md#color-scheme-and-media) `'prefers-colors-scheme'` media feature, supported values are `'light'`, `'dark'`, `'no-preference'` |
| [`property: TestOptions.geolocation`] | Context [geolocation](./emulation.md#geolocation). |
| [`property: TestOptions.locale`] | [Emulates](./emulation.md#locale--timezone) the user locale, for example `en-GB`, `de-DE`, etc. |
| [`property: TestOptions.permissions`] | A list of [permissions](./emulation.md#permissions) to grant to all pages in the context. |
| [`property: TestOptions.timezoneId`] | Changes the [timezone](./emulation.md#locale--timezone) of the context. |
| [`property: TestOptions.viewport`] | [Viewport](./emulation.md#viewport) used for all pages in the context. |
2023-03-06 17:17:34 +01:00
### Network Options
Available options to configure networking:
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
// Whether to automatically download all the attachments.
acceptDownloads: false,
2023-03-06 17:17:34 +01:00
// An object containing additional HTTP headers to be sent with every request.
extraHTTPHeaders: {
'X-My-Header': 'value',
},
2023-03-06 17:17:34 +01:00
// Credentials for HTTP authentication.
httpCredentials: {
username: 'user',
password: 'pass',
},
2023-03-06 17:17:34 +01:00
// Whether to ignore HTTPS errors during navigation.
ignoreHTTPSErrors: true,
2023-03-06 17:17:34 +01:00
// Whether to emulate network being offline.
offline: true,
2023-03-06 17:17:34 +01:00
// Proxy settings used for all pages in the test.
proxy: {
server: 'http://myproxy.com:3128',
bypass: 'localhost',
},
},
});
```
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
| [`property: TestOptions.acceptDownloads`] | Whether to automatically download all the attachments, defaults to `true`. [Learn more](./downloads.md) about working with downloads. |
| [`property: TestOptions.extraHTTPHeaders`] | An object containing additional HTTP headers to be sent with every request. All header values must be strings. |
| [`property: TestOptions.httpCredentials`] | Credentials for [HTTP authentication](./network.md#http-authentication). |
| [`property: TestOptions.ignoreHTTPSErrors`] | Whether to ignore HTTPS errors during navigation. |
| [`property: TestOptions.offline`] | Whether to emulate network being offline. |
| [`property: TestOptions.proxy`] | [Proxy settings](./network.md#http-proxy) used for all pages in the test. |
2023-03-06 17:17:34 +01:00
:::note
You don't have to configure anything to mock network requests. Just define a custom [Route] that mocks the network for a browser context. See our [network mocking guide](./network.md) to learn more.
:::
2023-03-06 17:17:34 +01:00
### Recording Options
2023-03-06 17:17:34 +01:00
With Playwright you can capture screenshots, record videos as well as traces of your test. By default these are turned off but you can enable them by setting the `screenshot`, `video` and `trace` options in your `playwright.config.js` file.
2023-03-06 17:17:34 +01:00
Trace files, screenshots and videos will appear in the test output directory, typically `test-results`.
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
2023-03-06 17:17:34 +01:00
// Capture screenshot after each test failure.
screenshot: 'only-on-failure'
2023-03-06 17:17:34 +01:00
// Record trace only when retrying a test for the first time.
trace: 'on-first-retry',
2023-03-06 17:17:34 +01:00
// Record video only when retrying a test for the first time.
video: 'on-first-retry'
},
});
```
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
| [`property: TestOptions.screenshot`] | Capture [screenshots](./screenshots.md) of your test. Options include `'off'`, `'on'` and `'only-on-failure'` |
| [`property: TestOptions.trace`] | Playwright can produce test traces while running the tests. Later on, you can view the trace and get detailed information about Playwright execution by opening [Trace Viewer](./trace-viewer.md). Options include: `'off'`, `'on'`, `'retain-on-failure'` and `'on-first-retry'` |
| [`property: TestOptions.video`] | Playwright can record [videos](./videos.md) for your tests. Options include: `'off'`, `'on'`, `'retain-on-failure'` and `'on-first-retry'` |
2023-03-06 17:17:34 +01:00
### Other Options
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
// Maximum time each action such as `click()` can take. Defaults to 0 (no limit).
actionTimeout: 0,
2023-03-06 17:17:34 +01:00
// Name of the browser that runs tests. For example `chromium`, `firefox`, `webkit`.
browserName: 'chromium',
2023-03-06 17:17:34 +01:00
// Toggles bypassing Content-Security-Policy.
bypassCSP: true,
2023-03-06 17:17:34 +01:00
// Channel to use, for example "chrome", "chrome-beta", "msedge", "msedge-beta".
channel: 'chrome',
2023-03-06 17:17:34 +01:00
// Run browser in headless mode.
headless: false,
2023-03-06 17:17:34 +01:00
// Change the default data-testid attribute.
testIdAttribute: 'pw-test-id',
},
});
```
2023-03-06 17:17:34 +01:00
| Option | Description |
| :- | :- |
| [`property: TestOptions.actionTimeout`] | Timeout for each Playwright action in milliseconds. Defaults to `0` (no timeout). Learn more about [timeouts](./test-timeouts.md) and how to set them for a single test. |
| [`property: TestOptions.browserName`] | Name of the browser that runs tests. Defaults to 'chromium'. Options include `chromium`, `firefox`, or `webkit`. |
| [`property: TestOptions.bypassCSP`] |Toggles bypassing Content-Security-Policy. Useful when CSP includes the production origin. Defaults to `false`. |
| [`property: TestOptions.channel`] | Browser channel to use. [Learn more](./browsers.md) about different browsers and channels. |
| [`property: TestOptions.headless`] | Whether to run the browser in headless mode meaning no browser is shown when running tests. Defaults to `true`. |
| [`property: TestOptions.testIdAttribute`] | Changes the default [`data-testid` attribute](./locators.md#locate-by-test-id) used by Playwright locators. |
2023-03-06 17:17:34 +01:00
### More browser and context options
2023-03-06 17:17:34 +01:00
Any options accepted by [`method: BrowserType.launch`] or [`method: Browser.newContext`] can be put into `launchOptions` or `contextOptions` respectively in the `use` section.
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
launchOptions: {
slowMo: 50,
},
},
});
```
However, most common ones like `headless` or `viewport` are available directly in the `use` section - see [basic options](#basic-options), [emulation](./emulation.md) or [network](#network).
2023-03-06 17:17:34 +01:00
### Explicit Context Creation and Option Inheritance
2023-01-30 09:53:28 +01:00
If using the built-in `browser` fixture, calling [`method: Browser.newContext`] will create a context with options inherited from the config:
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
userAgent: 'some custom ua',
viewport: { width: 100, height: 100 },
},
});
```
An example test illustrating the initial context options are set:
2023-03-06 17:17:34 +01:00
```js
import { test, expect } from "@playwright/test";
test('should inherit use options on context when using built-in browser fixture', async ({
browser,
}) => {
const context = await browser.newContext();
const page = await context.newPage();
expect(await page.evaluate(() => navigator.userAgent)).toBe('some custom ua');
expect(await page.evaluate(() => window.innerWidth)).toBe(100);
await context.close();
});
```
2023-03-06 17:17:34 +01:00
### Configuration Scopes
2023-03-06 17:17:34 +01:00
You can configure Playwright globally, per project, or per test. For example, you can set the locale to be used globally by adding `locale` to the `use` option of the Playwright config, and then override it for a specific project using the `project` option in the config. You can also override it for a specific test by adding `test.use({})` in the test file and passing in the options.
2023-03-06 17:17:34 +01:00
```js
import { defineConfig } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
use: {
locale: 'en-GB'
},
});
```
2023-03-06 17:17:34 +01:00
You can override options for a specific project using the `project` option in the Playwright config.
2023-03-06 17:17:34 +01:00
```js
import { defineConfig, devices } from '@playwright/test';
2023-03-06 17:17:34 +01:00
export default defineConfig({
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
locale: 'de-DE'
},
},
],
});
```
2023-03-06 17:17:34 +01:00
You can override options for a specific test file by using the `test.use()` method and passing in the options. For example to run tests with the French locale for a specific test:
2023-03-06 17:17:34 +01:00
```js
import { test, expect } from '@playwright/test';
2023-03-06 17:17:34 +01:00
test.use({ locale: 'fr-FR' });
2023-03-06 17:17:34 +01:00
test('example', async ({ page }) => {
// ...
});
```
2023-03-06 17:17:34 +01:00
The same works inside a describe block. For example to run tests in a describe block with the French locale:
2023-03-06 17:17:34 +01:00
```js
import { test, expect } from '@playwright/test';
2021-05-27 20:30:03 -07:00
2023-03-06 17:17:34 +01:00
test.describe('french language block', () => {
2023-03-06 17:17:34 +01:00
test.use({ { locale: 'fr-FR' }});
2023-03-06 17:17:34 +01:00
test('example', async ({ page }) => {
// ...
});
});
2021-05-27 20:30:03 -07:00
```