#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Playwright Interview Q&A · #28

What is the significance of beforeAll and afterAll hooks in Playwright?

By Uncodemy Editorial Team · Updated 16 Nov 2026

test.beforeAll() runs once before the tests in its file or describe block, and test.afterAll() runs once after them. They are for expensive setup and cleanup that many tests can share: seeding a test database, creating a test user through an API, starting a mock server. The catch, and the part interviewers like to probe, is what "once" really means in Playwright.

A working example

// tests/batch.spec.ts
import { test, expect } from '@playwright/test';

let batchId: string;

test.beforeAll(async ({ request }) => {
  // create shared test data once, through the API
  const res = await request.post('/api/test/batches', { data: { name: 'Weekend batch' } });
  expect(res.ok()).toBeTruthy();
  batchId = (await res.json()).id;
});

test.afterAll(async ({ request }) => {
  await request.delete(`/api/test/batches/${batchId}`);
});

test('batch page shows the name', async ({ page }) => {
  await page.goto(`/batches/${batchId}`);
  await expect(page.getByRole('heading', { name: 'Weekend batch' })).toBeVisible();
});

test('student can join the batch', async ({ page }) => {
  await page.goto(`/batches/${batchId}`);
  await page.getByRole('button', { name: 'Join' }).click();
  await expect(page.getByText('You have joined')).toBeVisible();
});

The endpoints are placeholders. The pattern is what matters: shared data is created by the API, not by clicking through the UI, and it is removed in afterAll.

The order hooks run in

With one beforeAll, one beforeEach, one afterAll and three tests in a file, a run on one worker prints:

beforeAll
beforeEach → test a
beforeEach → test b
beforeEach → test c
afterAll

beforeEach and afterEach wrap every test; the "All" hooks wrap the whole group. Hooks declared inside a test.describe() apply only to that block.

"Once" means once per worker

Playwright runs tests in worker processes, and hooks belong to the worker. Two consequences follow:

More on workers in parallelism in Playwright.

Why you cannot use page in beforeAll

A common first attempt is test.beforeAll(async ({ page }) => { ... }). It fails with:

Error: "context" and "page" fixtures are not supported in "beforeAll"
since they are created on a per-test basis.

Each test gets its own page so tests stay isolated (see browser isolation). In beforeAll you can use worker-level fixtures such as browser and request. To log in once, save a storageState file in a setup project instead; see authentication in Playwright tests.

Better tools for some jobs

NeedUse
Setup shared by every file (log in, seed DB once per run)A setup project with dependencies, or globalSetup
Reusable setup that tests ask for by nameA custom fixture; see fixtures in Playwright
Fresh state before every testbeforeEach
Setup for one file's tests onlybeforeAll

Gotchas

Want to master Playwright hands-on?

Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.

Explore the Course →