Chapter 47: Hooks and test organization
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Hooks and test organization
test.describe groups related tests. beforeEach and afterEach run for individual tests in their scope. beforeAll and afterAll run per relevant worker execution, not necessarily once for an entire distributed run. Tests should not rely on prior tests to create state.
How it works
Step-by-step walkthrough
- Create a describe block
- Prepare a page in beforeEach
- Assert inside a test
Example
import { test, expect } from '@playwright/test';
test.describe('Dashboard', () => {
test.beforeEach(async ({ page }) => {
await page.setContent('<h1>Dashboard</h1>');
});
test('heading', async ({ page }) => {
await expect(page.getByRole('heading')).toHaveText('Dashboard');
});
});Use beforeEach for reproducible per-test setup.
✎ Your practice task
Add another test in the group.
Knowledge check
Which hook runs before each test in scope?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.