Skip to content

Playwright Custom Fixture Not Working Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Playwright Custom Fixture Not Working Fix. We cover key concepts, practical examples, and best practices.

Your custom fixture is undefined or the values don't persist as expected — test.extend() isn't working the way you expect.

The Problem

// WRONG — fixture defined but not used
import { test as base } from '@playwright/test';

const test = base.extend({
  loggedInPage: async ({ page }, use) => {
    // This fixture is never called because tests don't request it
  },
});

test('dashboard test', async ({ page }) => {
  // Using page directly, not loggedInPage
  // The custom fixture setup never runs
});

The fixture is defined but the test uses page instead of loggedInPage, so the fixture logic (login) never executes.

Step-by-Step Fix

1. Define and use custom fixtures

// RIGHT — define fixture and request it
import { test as base, expect } from '@playwright/test';

const test = base.extend({
  loggedInPage: async ({ page }, use) => {
    await page.goto('https://example.com/login');
    await page.fill('#email', 'user@example.com');
    await page.fill('#password', 'secret123');
    await page.click('button[type="submit"]');
    await page.waitForURL('**/dashboard');
    await use(page);
  },
});

test('dashboard shows data', async ({ loggedInPage }) => {
  await expect(loggedInPage.locator('h1')).toHaveText('Dashboard');
});

2. Fixtures with options

// RIGHT — parameterized fixtures
const test = base.extend({
  user: async ({}, use) => {
    await use({ name: 'Alice', role: 'admin' });
  },
  authenticatedPage: async ({ page, user }, use) => {
    await page.goto('https://example.com/login');
    await page.fill('#email', `${user.name}@example.com`);
    await page.click('button[type="submit"]');
    await use(page);
  },
});

test('user-specific dashboard', async ({ authenticatedPage, user }) => {
  await expect(authenticatedPage.locator('.user-name')).toHaveText(user.name);
});

3. Auto fixtures (run automatically)

// RIGHT — auto fixture with no worker assignment
const test = base.extend({
  setupDatabase: [async ({}, use) => {
    console.log('Setting up test database...');
    await use();
    console.log('Tearing down test database...');
  }, { auto: true }],
});

4. Fixture scope management

// RIGHT — worker-scoped fixture
const test = base.extend({
  dbConnection: [async ({}, use) => {
    const db = await connectToDatabase();
    await use(db);
    await db.close();
  }, { scope: 'worker' }], // Shared across tests in same worker
});

Expected output:

✓ dashboard shows data (1.8s)
✓ user-specific dashboard (1.5s)

Prevention Tips

  • Request the custom fixture in test parameters to use it
  • Use auto: true for fixtures that must run without being requested
  • Use scope: 'worker' for expensive setup (database, server)
  • Use scope: 'test' for per-test isolation
  • Clean up resources in fixture teardown (after use())

Common Mistakes with fixture custom

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between test and worker scoped fixtures?

Test-scoped fixtures run once per test (isolated). Worker-scoped fixtures run once per worker process (shared across tests). Use worker scope for expensive operations like starting a server.

How do I make a fixture that requires setup and teardown?

Code before use() runs as setup. Code after use() runs as teardown. Use await use(value) to pass the fixture value to the test.

Can fixtures depend on other fixtures?

Yes. Fixtures can depend on built-in or custom fixtures by deconstructing them in the first parameter: async ({ page, user, db }, use) => {...}. The dependency graph is resolved automatically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro