26. Test Lifecycle Hooks & Clean Isolation

Lifecycle Hooks in Playwright

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();
});
});

Task

  • Implement recordLifecycleStep:
  • 1. Accept log array and step string: "beforeAll" | "beforeEach" | "test" | "afterEach" | "afterAll".
  • 2. Push step into log array.
πŸ’‘Common doubts
Did you like the problem?
Lesson 26 of 35
JavaScript
Loading code editor…
Next