Playwright Fundamentals
Playwright is a modern browser automation framework with auto-waiting, multiple browsers and excellent debugging tools.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Learning objectives#
- Install Playwright and run tests
- Understand browsers, contexts and pages
- Use codegen to explore locators
- Debug with UI mode and traces
Explanation#
npm init playwright@latest scaffolds a project with config, example tests and browsers (Chromium, Firefox, WebKit).
Each test gets a fresh browser context — like a new incognito profile — so tests never share cookies or storage.
Actions auto-wait for elements to be visible, enabled and stable, removing most manual waits.
npx playwright codegen records actions into code; --ui and traces let you step through failures.
Code example: Search journey#
import { test, expect } from '@playwright/test';
test('customer can search and open a product', async ({ page }) => {
await page.goto('/');
await page.getByRole('searchbox', { name: 'Search' }).fill('laptop');
await page.keyboard.press('Enter');
const firstResult = page.getByTestId('result').first();
await expect(firstResult).toBeVisible();
await firstResult.click();
await expect(page.getByRole('heading', { level: 1 })).toContainText('Laptop');
await expect(page.getByRole('button', { name: 'Add to cart' })).toBeEnabled();
});
// npx playwright test --project=chromium npx playwright test --ui npx playwright show-report
✓ [chromium] › search.spec.ts:3:5 › customer can search and open a product (2.1s) 1 passed (3.0s)
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- baseURL in the config lets page.goto("/") work.
- Role-based locators find elements like a user would.
- Each action and expect auto-waits.
- The HTML report shows steps, screenshots and traces.
Real-world use case#
A React team runs Playwright tests against Chromium, Firefox and WebKit on every pull request.
Common mistakes#
Playwright already waits. Use web-first assertions instead.
Use storageState to reuse an authenticated session safely.
Best practices#
- Set baseURL in config
- Use role and test-id locators
- Turn on traces for retries
- Run tests in parallel
Remember this#
Fresh context per test.
Actions and expects auto-wait.
codegen · --ui · traces for debugging.
Practice exercise#
Your task: Run only tests whose title contains “search”.
Show solution
npx playwright test -g "search"
Mini quiz#
What does a browser context give each test?
Want more? Practise all Automation Testing MCQs · Automation Testing interview questions
My notes#
Saved in this browser.