· 2 min read
Page Object Model in Playwright with TypeScript: Complete Guide
Learn how to structure scalable Playwright tests using the Page Object Model pattern. Includes TypeScript examples, best practices, and real-world architecture patterns.
Published: · 4 min read
Learn Playwright from scratch. This step-by-step tutorial will have you writing your first automated test in under 10 minutes — no prior automation experience required.
By the end of this tutorial, you'll have:
Total time: about 10 minutes. Let's go.
You need Node.js 18+ installed. Check your version:
node --version
If you need to install Node.js, download it from nodejs.org.
That's it. No other dependencies required.
Open your terminal and create a new folder:
mkdir playwright-tutorial
cd playwright-tutorial
Run the Playwright installer:
npm init playwright@latest
You'll see some prompts. Accept the defaults:
This installs Playwright, downloads browser binaries, and creates a basic project structure.
After installation, you'll see:
Delete the example test and create a new file called tests/first.spec.ts:
import { test, expect } from '@playwright/test';
test('homepage has correct title', async ({ page }) => {
await page.goto('https://playwright.dev');
await expect(page).toHaveTitle(/Playwright/);
});
What this does:
In your terminal:
npx playwright test
You should see:
Running 1 test using 1 worker
✓ first.spec.ts:3:1 › homepage has correct title (1.2s)
1 passed (2.1s)
Congratulations! You just ran your first Playwright test.
Playwright generates beautiful HTML reports. View it:
npx playwright show-report
This opens a browser with your test results, including timing, screenshots, and traces.
Playwright's UI mode lets you watch tests run in real-time:
npx playwright test --ui
This opens a visual interface where you can:
This is my favorite Playwright feature for development.
Let's expand our test to click a link and verify navigation:
import { test, expect } from '@playwright/test';
test('can navigate to getting started page', async ({ page }) => {
await page.goto('https://playwright.dev');
// Click the Getting Started link
await page.getByRole('link', { name: 'Get started' }).click();
// Verify we're on the right page
await expect(page).toHaveURL(/.*intro/);
// Check the heading exists
await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});
New concepts:
Playwright can generate test code by recording your actions:
npx playwright codegen playwright.dev
This opens a browser. Click around, and Playwright writes code for you in real-time. Copy the generated code into your test file.
This is extremely useful for learning locators and speeding up test creation.
Locators:
Playwright finds elements using locators. Best practices:
Avoid CSS selectors and XPath when possible — role-based locators are more resilient.
Auto-waiting:
Playwright automatically waits for elements to be ready before interacting. No more:
await page.waitForSelector('.button'); // Not needed!
Just write:
await page.click('.button'); // Playwright handles waiting
This dramatically reduces flaky tests.
Now that you have the basics:
1. Using sleep/delay:
Bad:
await page.waitForTimeout(5000);
Good: Let Playwright's auto-waiting handle it, or wait for specific conditions.
2. Fragile locators:
Bad:
page.locator('#app > div:nth-child(3) > button')
Good:
page.getByRole('button', { name: 'Submit' })
3. Not using test isolation:
Each test should be independent. Don't rely on state from previous tests.
npx playwright codegen [url]
npx playwright test --ui
npx playwright show-report
If you're building a test automation framework for your team and want guidance from someone who's done it at Apple and Fortune 500 companies, I offer consulting and training.
Get notified when I publish something new, and unsubscribe at any time.
· 2 min read
Learn how to structure scalable Playwright tests using the Page Object Model pattern. Includes TypeScript examples, best practices, and real-world architecture patterns.
· 4 min read
A practical, step-by-step guide to migrating your Selenium test suite to Playwright. Includes code comparison, common pitfalls, and a migration strategy that won't disrupt your team.