TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 47 of 60~18 min • Beginner
Chapter 47Playwright

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

  1. Create a describe block
  2. Prepare a page in beforeEach
  3. Assert inside a test

Example

TypeScript
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');
  });
});
Expected result: Dashboard test passes
💡 Point to remember
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