Playwright + TS · Lesson 25 of 36
Module 25: Fixtures
Try it yourself
Define the FixtureLifecycle interface:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
In this lesson
In traditional frameworks, beforeEach runs globally and creates implicit shared state. In Playwright:
use() call.// fixtures/test-base.ts
import { test as baseTest } from "@playwright/test";
import { LoginPage } from "../pages/LoginPage";
import { DashboardPage } from "../pages/DashboardPage";
// 1. Define the type of custom fixtures
type CustomFixtures = {
loginPage: LoginPage;
dashboardPage: DashboardPage;
};
// 2. Extend base test
export const test = baseTest.extend<CustomFixtures>({
loginPage: async ({ page }, use) => {
// Setup
const loginPage = new LoginPage(page);
// Provide fixture to test
await use(loginPage);
// Automatic teardown happens here when test finishes!
},
dashboardPage: async ({ page }, use) => {
const dashboardPage = new DashboardPage(page);
await use(dashboardPage);
},
});
export { expect } from "@playwright/test";import { test, expect } from "../fixtures/test-base";
test("successful login flow", async ({ loginPage, dashboardPage }) => {
await loginPage.navigateTo("/login");
await loginPage.login("admin@targetqa.com", "SecretPass123");
await dashboardPage.expectWelcomeMessage("Admin");
});Playwright Fixtures provide a powerful dependency injection system for your test suites. Instead of relying on rigid beforeEach hooks, custom fixtures initialize Page Objects, authenticate sessions, and perform automatic teardown only when a test requests them. In this lesson, we will create custom POM fixtures step by step. Let's dive in!
Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
Test fixtures run once per test for maximum isolation. Worker fixtures run once per worker process (e.g. setting up an expensive test database or shared HTTP server).
Yes! Playwright builds a dependency graph and executes them in topological order automatically.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.