Skip to content

How to build a scalable Playwright framework

How-toUpdated 2026-10-11AQAPro
6layers
11files to start
1.64code checked against
On this page
  1. TL;DR
  2. Key takeaways
  3. The architecture in one picture
  4. The folder structure: click any file
  5. The CI pipeline
  6. Step 1: Scaffold the project
  7. Page objects: thin on purpose
  8. Fixtures instead of beforeEach and base classes
  9. API for setup, UI for the behaviour
  10. What not to build (yet)
  11. Pro tips
  12. FAQ
  13. Sources
TL;DR

A scalable Playwright framework is mostly Playwright defaults, organised: tests that read like scenarios, fixtures that inject page objects and auth, thin page objects built on role and label locators, test data in its own folder, API calls for setup, and CI that shards, retries and keeps traces. Everything else, wrappers, base classes, custom retry logic, waits until you can prove you need it.

Writing Playwright tests is easy. Keeping 500 of them fast, readable and trustworthy a year later is the actual job. This guide builds a framework layer by layer, every file is clickable in the explorer below and every snippet compiles against Playwright 1.64, and it is just as explicit about what not to build.

Key takeaways

  1. Start from npm init playwright@latest and change defaults only when a test forces you to.
  2. Fixtures replace beforeEach and base-class inheritance: tests ask for loginPage and get one.
  3. Page objects hold locators and intent-level actions; assertions stay in the test so failures read clearly.
  4. Use the API for setup and cleanup, the UI only for the behaviour under test, and log in once with storageState.
  5. In CI: install browsers with --with-deps, shard across machines, retry to surface flakes, and merge the shards' blob reports into one HTML report with traces.

The architecture in one picture

Each layer only talks to the one below it. Hover a layer to highlight it; Replay runs the walk-through again.

  1. TestsScenarios and assertions. Read like a user story; no selectors, no waits.
  2. FixturesInject page objects, API clients, test data and auth into each test.
  3. Page objects & APILocators and intent actions per screen; typed API helpers for setup.
  4. PlaywrightAuto-waiting, locators and browser contexts on Chromium, Firefox and WebKit.
  5. Reports & tracesHTML report, screenshots, video and a trace for every retried failure.
  6. CI/CDSharded runs on every pull request; artifacts kept for debugging.

The folder structure: click any file

Eleven files cover a real framework. Each one has a single job.

playwright-framework/
tests/
pages/
fixtures/
data/
utils/
.github/
workflows/
tests/auth.setup.ts

Logs in once per run and saves the session. Every browser project depends on it.

tests/auth.setup.tsts
import { test as setup, expect } from '@playwright/test';import { users } from '../data/users'; const authFile = 'playwright/.auth/user.json'; setup('authenticate', async ({ page }) => {  await page.goto('/login');  await page.getByLabel('Email').fill(users.valid.email);  await page.getByLabel('Password').fill(users.valid.password);  await page.getByRole('button', { name: 'Sign in' }).click();  await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();  await page.context().storageState({ path: authFile });});

The CI pipeline

What happens after git push, step by step.

  1. Push / PRA push to main or any pull request triggers the workflow.
  2. npm ciClean install from the lockfile: same versions as your laptop.
  3. Install browsersnpx playwright install --with-deps adds browsers and OS libraries.
  4. Run 4 shards--shard=1/4 … 4/4 run in parallel; each uploads a blob report.
  5. Merge reportsA merge job combines the blobs into one HTML report, even when tests fail.
  6. ReviewDownload the report, open the trace of any failed or flaky test.

Step 1: Scaffold the project

The official initializer asks four questions and writes a working config, an example test and (optionally) a GitHub Actions workflow. Accept TypeScript, keep the tests folder, say yes to the workflow and to installing browsers.

Terminalbash
npm init playwright@latest # verifynpx playwright testnpx playwright show-report

Page objects: thin on purpose

A page object answers two questions: where is it, and what can a user do here. Expose locators and intent-level actions (login, addToCart). Keep expectations in the test, so a failure message says what the test expected rather than which helper failed. Prefer getByRole and getByLabel; fall back to getByTestId for elements with no accessible name.

Tip: Return Locators, not strings or booleans. A test can then assert with auto-retrying expect(locator).toHaveText(…) instead of reading textContent once and hoping.

Fixtures instead of beforeEach and base classes

Fixtures are created only when a test asks for them, torn down automatically, and composable: a test that needs a logged-in admin and a seeded cart says so in its signature. They replace base-class inheritance chains, which get harder to change with every test you add.

API for setup, UI for the behaviour

Creating a user through five screens tests the signup flow again and again. Create it with one request call, test the screen you care about, and delete it afterwards. Combined with storageState auth, this is usually where a suite wins back the most run time.

What not to build (yet)

Each of these costs maintenance from day one. Add one only when a real test cannot be written without it.

  • Wrappers around click / fill / waitFor: Playwright already auto-waits and retries.
  • Custom retry loops: use retries in the config, and fix what the report marks as flaky.
  • Explicit waits and sleeps: web-first assertions wait for you.
  • BaseTest / BasePage inheritance trees: use fixtures and composition.
  • Excel or CSV readers before you have data-driven tests that need them.
  • A custom reporter before the built-in HTML report stops being enough.

Pro tips

Small habits that keep a framework healthy as it grows.

  • Tag tests ({ tag: '@smoke' }) and run subsets with --grep instead of maintaining separate suites.
  • Keep every test independent: its own data, no order dependency, safe to run in parallel. Prove it now and then with npx playwright test --shuffle (1.64+), which runs tests in random order and prints the seed to replay a failing order.
  • Put BASE_URL and credentials in environment variables; commit a .env.example, never .env.
  • Retries in CI are a detector, not a fix: open the trace of every test the report marks as flaky.
  • Run npx playwright test --only-changed locally for fast feedback before pushing.

FAQ

What is the best folder structure for a Playwright framework?

tests/ for specs, pages/ for page objects, fixtures/ for a custom test with injected page objects, data/ for test data, utils/ for small helpers, plus playwright.config.ts and a CI workflow. Add folders only when a real need appears.

Should I use the Page Object Model with Playwright?

Yes, in a thin form: locators and intent-level actions per screen, created through fixtures. Avoid assertions and waits inside page objects and avoid base-class hierarchies.

Fixtures or beforeEach in Playwright?

Prefer fixtures. They are created only when a test requests them, clean up automatically, compose well and make each test's dependencies visible in its signature. beforeEach is fine for a one-off step inside a single file.

How do I run Playwright tests faster in CI?

Run fullyParallel with several workers, shard across machines with --shard, log in once with storageState, create test data through the API, and install only the browsers you test.

Sources