Playwright Hooks
Playwright Hooks
A hook is a function that Playwright runs before or after your tests.
Hooks hold the steps that many tests share, like opening a page or adding test data, so you do not repeat them in every test.
| Hook | Runs |
|---|---|
| test.beforeEach() | Before each test |
| test.afterEach() | After each test |
| test.beforeAll() | Once, before the first test in the file (or group) |
| test.afterAll() | Once, after the last test in the file (or group) |
beforeEach
These three tests all need the todo list with two todos in it.
The todo list on the practice page starts empty.
The beforeEach hook opens the page and adds the todos, before every test:
Example
tests/todo.spec.js
// @ts-check
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('https://www.w3schools.com/playwright/practice/todo.html');
const input = page.getByPlaceholder('What needs to be done?');
await input.fill('Buy milk');
await input.press('Enter');
await input.fill('Walk the dog');
await input.press('Enter');
});
test('shows two todos', async ({ page }) => {
await expect(page.getByRole('listitem')).toHaveCount(2);
await expect(page.getByTestId('todo-count')).toHaveText('2 items left');
});
test('can complete a todo', async ({ page }) => {
await page.getByRole('checkbox', { name: 'Complete Buy milk' }).check();
await expect(page.getByTestId('todo-count')).toHaveText('1 item left');
});
test('can delete a todo', async ({ page }) => {
await page.getByRole('button', { name: 'Delete Walk the dog' }).click();
await expect(page.getByRole('listitem')).toHaveCount(1);
});
Output:
Running 3 tests using 3 workers
[1/3] [chromium] › tests/todo.spec.js:23:1 › can delete a todo
[2/3] [chromium] › tests/todo.spec.js:13:1 › shows two todos
[3/3] [chromium] › tests/todo.spec.js:18:1 › can complete a todo
3 passed (2.1s)
The hook gets the same page as the test that runs after it.
Each test starts with its own fresh page, so the todo deleted in the third test does not affect the others.
The Order of Hooks
This file has all four hooks and two tests, and every one of them prints a line:
Example
tests/order.spec.js
// @ts-check
import { test } from '@playwright/test';
test.beforeAll(async () => {
console.log('beforeAll');
});
test.beforeEach(async () => {
console.log(' beforeEach');
});
test.afterEach(async () => {
console.log(' afterEach');
});
test.afterAll(async () => {
console.log('afterAll');
});
test('first test', async () => {
console.log(' first test');
});
test('second test', async () => {
console.log(' second test');
});
A worker is a process that runs tests, and Playwright starts several workers to run tests at the same time.
Run the file with one worker and the list reporter, so the lines print in order:
Example
npx playwright test tests/order.spec.js --workers=1 --reporter=list
Output:
Running 2 tests using 1 worker
beforeAll
beforeEach
first test
afterEach
ok 1 [chromium] › tests/order.spec.js:20:1 › first test (2ms)
beforeEach
second test
afterEach
afterAll
ok 2 [chromium] › tests/order.spec.js:24:1 › second test (1ms)
2 passed (709ms)
beforeAll and afterAll ran once, and beforeEach and afterEach ran around each test.
Hooks and Workers
Now run the same file with the default settings, where the tests run in parallel:
Example
npx playwright test tests/order.spec.js --reporter=list
Output:
Running 2 tests using 2 workers
beforeAll
beforeAll
beforeEach
first test
afterEach
beforeEach
second test
afterEach
afterAll
ok 1 [chromium] › tests/order.spec.js:20:1 › first test (2ms)
afterAll
ok 2 [chromium] › tests/order.spec.js:24:1 › second test (2ms)
2 passed (1.0s)
beforeAll ran twice!
Each worker is a separate process with its own browser.
The two tests ran in two workers, and each worker ran beforeAll and afterAll for itself.
So beforeAll means "once per worker", not "once for the whole run".
Note: beforeAll and afterAll cannot use page, because each test gets its own page.
They can use browser, or no fixture at all.
When to Use Which
| Hook | Use it for |
|---|---|
| beforeEach | Steps every test needs: open the page, log in, add test data. This is the hook you will use most |
| afterEach | Cleanup after each test, for example deleting test data on a server, or extra logging when a test fails |
| beforeAll | Slow setup that tests can share, for example starting a test server or creating data once |
| afterAll | Cleanup of what beforeAll created |
You do not need afterEach to close the page: Playwright closes it after every test.
Hooks at the top of a file apply to all tests in the file.
Hooks inside a test.describe() group apply only to the tests in that group.
You will learn about groups in the Groups and Tags chapter.
Tip: When setup grows big, or you need it in many files, move it into a fixture.
You will learn how in the Fixtures chapter.