Automation
Typescript
Intermediate
Playwright Cheat Sheet
Playwright essentials — auto-waits, locators, network interception, and assertions.
Playwright Cheat Sheet
Setup
import { test, expect } from '@playwright/test';
test('example', async ({ page }) => {
await page.goto('https://example.com');
});
Locators (auto-waiting built in)
page.getByRole('button', { name: 'Submit' })
page.getByText('Welcome')
page.getByLabel('Email')
page.getByPlaceholder('Enter email')
page.getByTestId('login-form')
page.locator('.css-selector')
page.locator('xpath=//div[@id="app"]')
Assertions
await expect(page).toHaveTitle(/Dashboard/);
await expect(page.getByText('Success')).toBeVisible();
await expect(page.getByRole('button')).toBeEnabled();
await expect(page.locator('.items')).toHaveCount(3);
Network
// Intercept API calls
await page.route('**/api/users', route => {
route.fulfill({ status: 200, body: JSON.stringify([]) });
});
Run
npx playwright test
npx playwright test --headed
npx playwright codegen https://example.com