With the basics in hand, this chapter covers the patterns that keep a growing test suite maintainable: the Page Object Model, network mocking, fixtures, parallelism, and CI.
Page Object Model
A Page Object wraps a page’s locators and actions behind a small class. Tests then read in domain terms instead of DOM terms.
// pages/login.page.ts
import { type Page, type Locator, expect } from "@playwright/test";
export class LoginPage {
readonly page: Page;
readonly username: Locator;
readonly password: Locator;
readonly submit: Locator;
constructor(page: Page) {
this.page = page;
this.username = page.getByLabel("Username");
this.password = page.getByLabel("Password");
this.submit = page.getByRole("button", { name: "Sign in" });
}
async goto() {
await this.page.goto("/login");
}
async loginAs(user: string, pass: string) {
await this.username.fill(user);
await this.password.fill(pass);
await this.submit.click();
}
async expectError(message: string) {
await expect(this.page.getByRole("alert")).toHaveText(message);
}
}
The test becomes intent-revealing:
import { test } from "@playwright/test";
import { LoginPage } from "../pages/login.page";
test("wrong password shows an error", async ({ page }) => {
const login = new LoginPage(page);
await login.goto();
await login.loginAs("alice", "wrong");
await login.expectError("Invalid username or password");
});
Keep assertions in the test when they are the point of the test, and let page objects own reusable actions and element lookups.
Mocking the network with page.route()
page.route() intercepts requests so tests do not depend on a live backend. This
makes them fast and deterministic.
test("shows an error when the API fails", async ({ page }) => {
await page.route("**/api/products", route =>
route.fulfill({
status: 500,
contentType: "application/json",
body: JSON.stringify({ error: "boom" }),
})
);
await page.goto("/shop");
await expect(page.getByRole("alert")).toContainText("Something went wrong");
});
You can also stub a successful payload, or modify a real response:
await page.route("**/api/user", async route => {
const response = await route.fetch();
const json = await response.json();
await route.fulfill({ json: { ...json, plan: "pro" } });
});
Fixtures: shared setup without duplication
A custom fixture bundles setup into a reusable, typed dependency.
// fixtures.ts
import { test as base } from "@playwright/test";
import { LoginPage } from "./pages/login.page";
type Fixtures = {
loginPage: LoginPage;
authenticatedPage: LoginPage;
};
export const test = base.extend<Fixtures>({
loginPage: async ({ page }, use) => {
await use(new LoginPage(page));
},
authenticatedPage: async ({ page }, use) => {
const login = new LoginPage(page);
await login.goto();
await login.loginAs("alice", process.env.TEST_PASSWORD!);
await use(login);
},
});
export { expect } from "@playwright/test";
Tests opt into exactly the setup they need, and the fixture is created lazily and torn down automatically.
Parallel execution
Playwright parallelises across files by default using worker processes:
export default defineConfig({
fullyParallel: true,
workers: process.env.CI ? 2 : undefined,
});
Control isolation per file with test.describe.configure:
test.describe.configure({ mode: "parallel" });
Tests in a file share a worker unless you opt in; keep tests independent so any ordering is valid.
CI with GitHub Actions
A minimal workflow that installs browsers, runs tests, and uploads the report:
name: Playwright tests
on:
push:
branches: [main]
pull_request:
jobs:
test:
timeout-minutes: 20
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 7
trace: "on-first-retry" (set in the config) means a failing test that gets retried
produces a trace you can open from the uploaded artifact.
Where to go from here
- Add visual comparisons with
expect(page).toHaveScreenshot(). - Emulate mobile devices with
devices["iPhone 13"]in the project config. - Drive authenticated flows once and reuse
storageStateacross tests instead of logging in every time.
That completes the Playwright course. Revisit any chapter from the course index, and keep the official documentation close for the full API surface.