Playwright + TS · Lesson 25 of 36

0/36 done

Module 25: Fixtures

25. Dependency Injection with Custom Playwright Fixtures

20 minlesson

Try it yourself

Define the FixtureLifecycle interface:

  1. Generic parameter T.
  2. setup property returning Promise<T>.
  3. teardown property accepting resource of type T and returning Promise<void>.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Why Fixtures Beat Traditional Hooks

In traditional frameworks, beforeEach runs globally and creates implicit shared state. In Playwright:

  1. On-Demand: A fixture only runs if a test actually declares it in its argument list.
  2. Encapsulated Cleanup: The teardown logic lives right after the use() call.
  3. Composable: Fixtures can depend on other fixtures.

Creating Custom POM Fixtures

typescript
// 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";

Using Custom Fixtures in Tests

typescript
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");
});

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

What is the difference between Test fixtures and Worker fixtures?

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).

Can a fixture depend on another custom fixture?

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.

TypeScript
Skip