#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Playwright Interview Q&A · #44

What is the role of fixtures in Playwright?

By Uncodemy Editorial Team · Updated 16 Nov 2026

Fixtures are how Playwright Test prepares everything a test needs and tidies up after it. A test lists what it wants in its first argument, async ({ page, request }) => ..., and the runner builds only those things, passes them in, and disposes of them when the test ends. If you know beforeEach/afterEach from other frameworks, fixtures do the same job in a more reusable way.

Built-in fixtures

FixtureWhat you getScope
pageA new tab in a fresh contextper test
contextThe fresh BrowserContext that owns pageper test
requestAn HTTP client for API testingper test
browserThe shared browser instanceper worker
browserName'chromium', 'firefox' or 'webkit'per worker

Fixtures are lazy: a test that only asks for request never launches a page.

Writing your own fixture

Suppose many tests start on the course catalogue page. Instead of repeating the navigation in every test, wrap it in a page object and expose it as a fixture:

// fixtures.ts
import { test as base, expect, type Page } from '@playwright/test';

export class CataloguePage {
  constructor(public page: Page) {}
  async open() { await this.page.goto('/courses'); }
  heading() { return this.page.getByRole('heading', { level: 1 }); }
  card(name: string) { return this.page.getByRole('listitem').filter({ hasText: name }); }
}

type Fixtures = { catalogue: CataloguePage; city: string };

export const test = base.extend<Fixtures>({
  city: ['Noida', { option: true }],            // a value tests can override

  catalogue: async ({ page, city }, use) => {
    const catalogue = new CataloguePage(page);
    await catalogue.open();                     // setup
    await page.getByLabel('City').selectOption(city);
    await use(catalogue);                       // the test runs here
    // teardown code would go here, after use()
  },
});
export { expect };
// tests/catalogue.spec.ts
import { test, expect } from '../fixtures';

test('catalogue lists courses', async ({ catalogue }) => {
  await expect(catalogue.heading()).toBeVisible();
});

test.describe('another city', () => {
  test.use({ city: 'Pune' });                   // override the option
  test('shows the selected city', async ({ catalogue }) => {
    await expect(catalogue.page.getByLabel('City')).toHaveValue('Pune');
  });
});

Everything before await use(...) is setup, everything after it is teardown, and the teardown runs even when the test fails. In a check of this pattern, the log printed "setup Noida", the first test, "teardown", then "setup Pune" for the overridden group.

Worker-scoped fixtures

Some things are expensive and safe to share, such as an API token or a seeded database. Give them { scope: 'worker' } and they are created once per worker process and torn down when the worker finishes:

export const test = base.extend<{}, { apiToken: string }>({
  apiToken: [async ({}, use) => {
    const token = await getTokenSomehow();      // runs once per worker
    await use(token);
  }, { scope: 'worker' }],
});

With two tests in one worker, the token was set up before the first test and torn down only after the second.

Why fixtures beat hooks for shared setup

Hooks still have their place; compare them in beforeAll and afterAll hooks. Fixture options live naturally in the config file.

Common mistakes

Want to master Playwright hands-on?

Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.

Explore the Course →