Getting Started with Playwright π
The Story of Your New Superpower
Imagine youβre a detective. Your job? Make sure websites work perfectly before real people use them. But checking every button, every form, every pageβ¦ by hand? That would take forever!
Enter Playwright β your robot assistant that clicks, types, and checks websites automatically. Itβs like having a tireless helper who never sleeps and never makes mistakes.
What is Playwright?
Think of Playwright like a remote-controlled robot that can browse websites just like you do.
graph TD A[You Write Instructions] --> B[Playwright Reads Them] B --> C[Robot Opens Browser] C --> D[Robot Clicks & Types] D --> E[Robot Checks Everything] E --> F[Robot Reports Back]
Simple Example:
You tell Playwright: βGo to Google, type βpuppiesβ, click search.β
Playwright does exactly that β in Chrome, Firefox, AND Safari β all at once!
Real Life Uses:
- π Check if βAdd to Cartβ button works
- π Test if login page accepts correct passwords
- π± Make sure website looks good on phones
Why βPlaywrightβ?
A playwright writes scripts for actors. You write scripts, browsers act them out!
Playwright vs Selenium π₯
Letβs compare two popular testing tools.
| Feature | Playwright | Selenium |
|---|---|---|
| Speed | π Very Fast | π’ Slower |
| Setup | One command | Many steps |
| Browsers | Chrome, Firefox, Safari | Chrome, Firefox, Safari |
| Auto-Wait | β Built-in | β Manual |
| Made By | Microsoft | Selenium HQ |
The Key Difference
Selenium is like an older, experienced worker β reliable but sometimes slow.
Playwright is the new kid who learned from Seleniumβs mistakes. It waits automatically when pages load. No more βelement not foundβ errors!
// Playwright auto-waits!
await page.click('button');
// Selenium needs manual waits
driver.wait(until.elementIsVisible(button));
driver.click(button);
Playwright vs Cypress π
Both are modern testing tools. Hereβs how they differ:
| Feature | Playwright | Cypress |
|---|---|---|
| Browsers | All 3 major | Chrome mainly |
| Language | JS, TS, Python, .NET | JavaScript only |
| Speed | β‘ Faster | Fast |
| iFrames | β Easy | β Tricky |
| Multiple Tabs | β Yes | β No |
When to Choose What?
Pick Playwright if:
- You need all browsers
- You use Python or .NET
- You test apps with popups/tabs
Pick Cypress if:
- You only test in Chrome
- You want built-in dashboard
- Your team knows only JavaScript
Installing Playwright π¦
This is the fun part! Just ONE command does everything.
Step 1: Make Sure You Have Node.js
Open your terminal and type:
node --version
You should see something like v18.0.0. If not, download Node.js first!
Step 2: Create Your Project
mkdir my-first-tests
cd my-first-tests
npm init -y
Step 3: Install Playwright
npm init playwright@latest
Magic happens! This command:
- β Downloads Playwright
- β Downloads browsers (Chrome, Firefox, Safari)
- β Creates example tests
- β Sets up config file
What Youβll See
The installer asks a few questions:
? Do you want TypeScript? (Y/n)
? Where to put tests? (tests)
? Add GitHub Actions? (y/N)
Just press Enter to accept defaults!
Playwright CLI Commands β¨οΈ
The CLI (Command Line Interface) is your control panel. Here are the essential commands:
π Running Tests
# Run all tests
npx playwright test
# Run one test file
npx playwright test login.spec.ts
# Run in headed mode (see browser)
npx playwright test --headed
π Browser Selection
# Only Chrome
npx playwright test --project=chromium
# Only Firefox
npx playwright test --project=firefox
# Only Safari
npx playwright test --project=webkit
π οΈ Helpful Tools
# Open Test Generator (records clicks!)
npx playwright codegen google.com
# Show test report
npx playwright show-report
# Update browsers
npx playwright install
π Debugging
# Debug mode (step through tests)
npx playwright test --debug
# See trace of failed test
npx playwright show-trace trace.zip
Project Structure Setup π
After installation, your folder looks like this:
my-first-tests/
βββ tests/
β βββ example.spec.ts β Your tests live here
βββ tests-examples/
β βββ demo-todo-app.spec.ts
βββ playwright.config.ts β Settings file
βββ package.json
βββ package-lock.json
graph TD A[my-first-tests] --> B[tests/] A --> C[playwright.config.ts] A --> D[package.json] B --> E[example.spec.ts] B --> F[login.spec.ts] B --> G[cart.spec.ts]
Organizing Tests
As your project grows, organize like this:
tests/
βββ auth/
β βββ login.spec.ts
β βββ signup.spec.ts
βββ shopping/
β βββ cart.spec.ts
β βββ checkout.spec.ts
βββ common/
βββ helpers.ts
Pro Tip: Keep related tests together in folders!
playwright.config.ts File βοΈ
This file is the brain of your testing setup. It tells Playwright HOW to run tests.
Basic Structure
import { defineConfig } from '@playwright/test';
export default defineConfig({
// Where are your tests?
testDir: './tests',
// Run tests in parallel
fullyParallel: true,
// Retry failed tests
retries: 2,
// How many tests at once
workers: 4,
});
Important Settings Explained
| Setting | What It Does | Example |
|---|---|---|
testDir |
Where tests live | './tests' |
timeout |
Max time per test | 30000 (30s) |
retries |
Retry on fail | 2 |
workers |
Parallel tests | 4 |
Browser Configuration
export default defineConfig({
projects: [
{
name: 'chromium',
use: { browserName: 'chromium' },
},
{
name: 'firefox',
use: { browserName: 'firefox' },
},
{
name: 'webkit',
use: { browserName: 'webkit' },
},
],
});
Adding Base URL
export default defineConfig({
use: {
baseURL: 'https://mywebsite.com',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
});
Now page.goto('/') goes to https://mywebsite.com/!
Your First Test π
Letβs write a real test! Create tests/first.spec.ts:
import { test, expect } from '@playwright/test';
test('homepage has title', async ({ page }) => {
// Go to website
await page.goto('https://playwright.dev');
// Check the title
await expect(page).toHaveTitle(/Playwright/);
});
Run it:
npx playwright test first.spec.ts
β Congratulations! You just ran your first Playwright test!
Quick Recap π―
| Topic | Key Takeaway |
|---|---|
| What is Playwright | Robot that tests websites automatically |
| vs Selenium | Playwright is faster, auto-waits |
| vs Cypress | Playwright supports all browsers & languages |
| Installing | npm init playwright@latest |
| CLI Commands | npx playwright test, --headed, --debug |
| Project Structure | tests/ folder, playwright.config.ts |
| Config File | Controls browsers, timeouts, retries |
Whatβs Next? π
You now have Playwright installed and understand the basics. Next, youβll learn:
- Writing more complex tests
- Finding elements on pages
- Handling forms and buttons
- Taking screenshots
Remember: Every expert was once a beginner. Youβve taken the first step! π