Playwright Browsers and Projects
Playwright Browsers and Projects
A project is a named group of settings in playwright.config.js.
Playwright runs every test once for each project.
The most common use is to test in several browsers: one project for each browser.
Playwright supports three browser engines:
- Chromium: the engine of Google Chrome and Microsoft Edge
- Firefox: the engine of Mozilla Firefox
- WebKit: the engine of Apple Safari
Test in Three Browsers
Add one project per browser to the projects list:
Example
playwright.config.js
// @ts-check
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
fullyParallel: true,
reporter: 'html',
use: {
baseURL: 'https://www.w3schools.com/playwright/practice/',
trace: 'on-first-retry',
},
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'firefox', use: { ...devices['Desktop Firefox'] } },
{ name: 'webkit', use: { ...devices['Desktop Safari'] } },
],
});
...devices['Desktop Chrome'] copies the ready-made settings for that browser, like the viewport size, into the project.
The Mobile Emulation chapter explains devices.
The config from npm init playwright has the same three projects.
The test file has two tests:
Example
tests/todo.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('has title', async ({ page }) => {
await page.goto('todo.html');
await expect(page).toHaveTitle(/Todo List/);
});
test('can add a todo', async ({ page }) => {
await page.goto('todo.html');
await page.getByPlaceholder('What needs to be done?').fill('Learn Playwright');
await page.getByRole('button', { name: 'Add' }).click();
await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
});
Two tests in three projects make six test runs.
You can see them with --list, which lists the tests without running them:
Example
npx playwright test --list
Output:
Listing tests:
[chromium] › todo.spec.js:4:1 › has title
[chromium] › todo.spec.js:9:1 › can add a todo
[firefox] › todo.spec.js:4:1 › has title
[firefox] › todo.spec.js:9:1 › can add a todo
[webkit] › todo.spec.js:4:1 › has title
[webkit] › todo.spec.js:9:1 › can add a todo
Total: 6 tests in 1 file
The project name is shown in brackets in front of each test.
Run them all with npx playwright test.
Playwright runs each test once in every project, and the report groups the results by project.
Run One Project
Use --project to run only some of the projects:
Example
npx playwright test --project=webkit
Output:
Running 2 tests using 2 workers
[1/2] [webkit] › tests/todo.spec.js:9:1 › can add a todo
[2/2] [webkit] › tests/todo.spec.js:4:1 › has title
2 passed (2.3s)
Repeat --project to run more than one:
Example
npx playwright test --project=chromium --project=webkit
Output:
Running 4 tests using 4 workers
[1/4] [chromium] › tests/todo.spec.js:9:1 › can add a todo
[2/4] [webkit] › tests/todo.spec.js:9:1 › can add a todo
[3/4] [webkit] › tests/todo.spec.js:4:1 › has title
[4/4] [chromium] › tests/todo.spec.js:4:1 › has title
4 passed (2.2s)
Tip: While you write a test, run one project to get fast results.
Run all browsers before you share your work, or let your CI server do it.
Setup Projects and Dependencies
A project can depend on another project.
Playwright runs the other project first, and runs the dependent project only if the first one passed.
This is useful for a setup step, like checking that a server is running or logging in once.
Example
playwright.config.js
// @ts-check
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
fullyParallel: true,
reporter: 'html',
use: {
baseURL: 'https://www.w3schools.com/playwright/practice/',
trace: 'on-first-retry',
},
projects: [
{ name: 'setup', testMatch: /.*\.setup\.js/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup'],
},
],
});
testMatch is a pattern (a regular expression) for file names.
Here, the setup project only runs files that end with .setup.js.
dependencies: ['setup'] makes the chromium project depend on the setup project.
This setup file checks that the products API of the practice shop works:
Example
tests/api.setup.js
// @ts-check
import { test as setup, expect } from '@playwright/test';
setup('products API is up', async ({ request }) => {
const response = await request.get('api/products.php');
expect(response.ok()).toBeTruthy();
});
test as setup gives test another name, so the file is easy to read.
It works the same as test.
The request fixture sends HTTP requests without a browser, see API Testing.
The normal tests run in the chromium project:
Example
tests/shop.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test('shows all products', async ({ page }) => {
await page.goto('shop.html');
await expect(page.getByTestId('product')).toHaveCount(5);
});
test('can add a product to the cart', async ({ page }) => {
await page.goto('shop.html');
await page.getByRole('button', { name: 'Add to cart' }).first().click();
await expect(page.getByTestId('cart-count')).toHaveText('1');
});
The output of npx playwright test:
Output:
Running 3 tests using 2 workers
[1/3] [setup] › tests/api.setup.js:4:1 › products API is up
[2/3] [chromium] › tests/shop.spec.js:4:1 › shows all products
[3/3] [chromium] › tests/shop.spec.js:9:1 › can add a product to the cart
3 passed (4.5s)
The setup test ran first, and the shop tests ran after it.
If the setup test fails, the shop tests do not run.
Use --no-deps to run a project without its dependencies.
The Authentication chapter uses a setup project to log in once for all tests.