Back to Cheat SheetsAutomation · Intermediate

Playwright cheat sheet

Locators, assertions, fixtures, network mocking and CLI flags.

Locators

By role

Preferred — accessible and stable

page.getByRole('button', { name: 'Submit' })

By label / placeholder

page.getByLabel('Email'); page.getByPlaceholder('Search')

By test id

page.getByTestId('cart-total')

Filter / nth

rows.filter({ hasText: 'QA' }).first()

Assertions

Visible

await expect(loc).toBeVisible();

Text / value

await expect(loc).toHaveText('Done'); await expect(input).toHaveValue('qa')

Count

await expect(rows).toHaveCount(5);

URL

await expect(page).toHaveURL(/dashboard/);

Fixtures and hooks

Custom fixture

export const test = base.extend<{ user: User }>({ user: async ({}, use) => { await use(await make()); } });

Reuse auth

test.use({ storageState: 'auth.json' });

Network and CLI

Mock a route

await page.route('**/api/cart', r => r.fulfill({ json: { items: [] } }));

API request

await request.post('/api/users', { data });

Run / debug

npx playwright test --headed --debug

Codegen / report

npx playwright codegen url

Go beyond the reference

Book a free demo class and apply these commands on live project work.