playwright/docs/src/test-api/class-testproject.md

317 lines
7.9 KiB
Markdown
Raw Normal View History

# class: TestProject
* langs: js
Playwright Test supports running multiple test projects at the same time. This is useful for running tests in multiple configurations. For example, consider running tests against multiple browsers.
`TestProject` encapsulates configuration specific to a single project. Projects are configured in [`property: TestConfig.projects`] specified in the [configuration file](./test-configuration.md). Note that all properties of [TestProject] are available in the top-level [TestConfig], in which case they are shared between all projects.
Here is an example configuration that runs every test in Chromium, Firefox and WebKit, both Desktop and Mobile versions.
```js js-flavor=js
// playwright.config.js
// @ts-check
const { devices } = require('@playwright/test');
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
// Options shared for all projects.
timeout: 30000,
use: {
ignoreHTTPSErrors: true,
},
// Options specific to each project.
projects: [
{
name: 'Desktop Chromium',
use: {
browserName: 'chromium',
viewport: { width: 1280, height: 720 },
},
},
{
name: 'Desktop Safari',
use: {
browserName: 'webkit',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Desktop Firefox',
use: {
browserName: 'firefox',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Mobile Chrome',
use: devices['Pixel 5'],
},
{
name: 'Mobile Safari',
use: devices['iPhone 12'],
},
],
};
module.exports = config;
```
```js js-flavor=ts
// playwright.config.ts
import { PlaywrightTestConfig, devices } from '@playwright/test';
const config: PlaywrightTestConfig = {
// Options shared for all projects.
timeout: 30000,
use: {
ignoreHTTPSErrors: true,
},
// Options specific to each project.
projects: [
{
name: 'Desktop Chromium',
use: {
browserName: 'chromium',
viewport: { width: 1280, height: 720 },
},
},
{
name: 'Desktop Safari',
use: {
browserName: 'webkit',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Desktop Firefox',
use: {
browserName: 'firefox',
viewport: { width: 1280, height: 720 },
}
},
{
name: 'Mobile Chrome',
use: devices['Pixel 5'],
},
{
name: 'Mobile Safari',
use: devices['iPhone 12'],
},
],
};
export default config;
```
## property: TestProject.expect
- type: <[Object]>
- `timeout` <[float]> Default timeout for async expect matchers in milliseconds, defaults to 5000ms.
- `toMatchSnapshot` <[Object]>
- `threshold` <[float]> Image matching threshold between zero (strict) and one (lax).
Configuration for the `expect` assertion library.
## property: TestProject.metadata
- type: <[Object]>
Any JSON-serializable metadata that will be put directly to the test report.
## property: TestProject.name
- type: <[string]>
Project name is visible in the report and during test execution.
## property: TestProject.outputDir
- type: <[string]>
The output directory for files created during test execution. Defaults to `test-results`.
This directory is cleaned at the start. When running a test, a unique subdirectory inside the [`property: TestProject.outputDir`] is created, guaranteeing that test running in parallel do not conflict. This directory can be accessed by [`property: TestInfo.outputDir`] and [`method: TestInfo.outputPath`].
Here is an example that uses [`method: TestInfo.outputPath`] to create a temporary file.
```js js-flavor=js
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');
});
```
```js js-flavor=ts
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');
});
```
## property: TestProject.repeatEach
- type: <[int]>
The number of times to repeat each test, useful for debugging flaky tests.
## property: TestProject.retries
- type: <[int]>
The maximum number of retry attempts given to failed tests. Learn more about [test retries](./test-retries.md#retries).
## property: TestProject.testDir
- type: <[string]>
Directory that will be recursively scanned for test files. Defaults to the directory of the configuration file.
Each project can use a different directory. Here is an example that runs smoke tests in three browsers and all other tests in stable Chrome browser.
```js js-flavor=js
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
projects: [
{
name: 'Smoke Chromium',
testDir: './smoke-tests',
use: {
browserName: 'chromium',
}
},
{
name: 'Smoke WebKit',
testDir: './smoke-tests',
use: {
browserName: 'webkit',
}
},
{
name: 'Smoke Firefox',
testDir: './smoke-tests',
use: {
browserName: 'firefox',
}
},
{
name: 'Chrome Stable',
testDir: './',
use: {
browserName: 'chromium',
channel: 'chrome',
}
},
],
};
module.exports = config;
```
```js js-flavor=ts
// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Smoke Chromium',
testDir: './smoke-tests',
use: {
browserName: 'chromium',
}
},
{
name: 'Smoke WebKit',
testDir: './smoke-tests',
use: {
browserName: 'webkit',
}
},
{
name: 'Smoke Firefox',
testDir: './smoke-tests',
use: {
browserName: 'firefox',
}
},
{
name: 'Chrome Stable',
testDir: './',
use: {
browserName: 'chromium',
channel: 'chrome',
}
},
],
};
export default config;
```
## property: TestProject.testIgnore
- type: <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Files 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.
## property: TestProject.testMatch
- type: <[string]|[RegExp]|[Array]<[string]>|[Array]<[RegExp]>>
Only 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)`.
## property: TestProject.timeout
- type: <[int]>
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 [`method: Test.setTimeout`].
## property: TestProject.use
- type: <[Fixtures]>
Additional fixtures for this project. Most useful for specifying options, for example [`property: Fixtures.browserName`]. Learn more about [Fixtures] and [configuration](./test-configuration.md).
```js js-flavor=js
// playwright.config.js
// @ts-check
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
projects: [
{
name: 'Chromium',
use: {
browserName: 'chromium',
},
},
],
};
module.exports = config;
```
```js js-flavor=ts
// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Chromium',
use: {
browserName: 'chromium',
},
},
],
};
export default config;
```