Playwright + JS · Lesson 26 of 35
Module 26: Hooks & Test Isolation
Try it yourself
Implement recordLifecycleStep:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
import { test, expect } from "@playwright/test";
test.describe("User Profile Suite", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/profile");
});
test.afterEach(async ({ page }) => {
// Optional per-test cleanup
});
test("can update username", async ({ page }) => {
await page.getByLabel("Username").fill("new_name");
await page.getByRole("button", { name: "Save" }).click();
});
});Lifecycle hooks control how tests execute before and after each suite. In this lesson, we will explore beforeAll, beforeEach, afterEach, and afterAll, and discuss why strict test isolation is essential for running reliable tests in parallel. Let's master test lifecycles!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Playwright executes tests in parallel across multiple worker processes. If Test B relies on data created by Test A, running in parallel will cause failures.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.