Lesson 6 of 8
Playwright - E2E Testing
Playwright runs real tests in real browsers (Chromium, Firefox, WebKit). For React apps, it's most valuable for critical user flows: authentication, checkout, key user journeys - anything where integration tests aren't enough because they don't test the full HTTP + DOM stack.
Playwright Setup & Test Patterns
# Install Playwright
npm init playwright@latest
# Creates playwright.config.ts and tests/ directory
# playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [['html', { open: 'never' }], ['github']],
use: {
baseURL: 'http://localhost:3000',
trace: 'on-first-retry', // trace recording on failure
screenshot: 'only-on-failure',
video: 'on-first-retry',
},
projects: [
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
{ name: 'firefox', use: { ...devices['Desktop Firefox'] } },
{ name: 'webkit', use: { ...devices['Desktop Safari'] } },
{ name: 'mobile-chrome', use: { ...devices['Pixel 5'] } },
],
// Start dev server before tests
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});
// e2e/auth.spec.ts - authentication flow
import { test, expect } from '@playwright/test';
test.describe('Authentication', () => {
test('user can sign in and see dashboard', async ({ page }) => {
await page.goto('/login');
// Fill form
await page.getByLabel('Email').fill('test@example.com');
await page.getByLabel('Password').fill('password123');
await page.getByRole('button', { name: 'Sign In' }).click();
// Redirected to dashboard
await expect(page).toHaveURL('/dashboard');
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});
test('shows error for invalid credentials', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill('wrong@example.com');
await page.getByLabel('Password').fill('wrongpass');
await page.getByRole('button', { name: 'Sign In' }).click();
await expect(page.getByRole('alert')).toContainText('Invalid credentials');
await expect(page).toHaveURL('/login'); // stayed on login page
});
});
// e2e/cart.spec.ts - shopping flow
test.describe('Shopping Cart', () => {
test.beforeEach(async ({ page }) => {
// Sign in before each test using saved auth state
await page.goto('/login');
await page.getByLabel('Email').fill('test@example.com');
await page.getByLabel('Password').fill('password123');
await page.getByRole('button', { name: 'Sign In' }).click();
await page.waitForURL('/dashboard');
});
test('add product to cart and checkout', async ({ page }) => {
await page.goto('/products');
// Add first product
await page.getByRole('button', { name: 'Add to Cart' }).first().click();
await expect(page.getByTestId('cart-count')).toHaveText('1');
// Navigate to cart
await page.getByRole('link', { name: /cart/i }).click();
await expect(page).toHaveURL('/cart');
// Confirm item is in cart
await expect(page.getByRole('list')).toContainText('Product');
// Proceed to checkout
await page.getByRole('button', { name: 'Checkout' }).click();
await expect(page).toHaveURL('/checkout');
});
});
// Page Object Model (POM) - for complex flows
class LoginPage {
constructor(private page: Page) {}
async goto() { await this.page.goto('/login'); }
async login(email: string, password: string) {
await this.page.getByLabel('Email').fill(email);
await this.page.getByLabel('Password').fill(password);
await this.page.getByRole('button', { name: 'Sign In' }).click();
}
async getError() { return this.page.getByRole('alert').textContent(); }
}
// Usage
test('full auth flow', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('user@example.com', 'password');
await expect(page).toHaveURL('/dashboard');
});
// Saving auth state (avoid logging in on every test)
// e2e/global.setup.ts
import { chromium } from '@playwright/test';
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('http://localhost:3000/login');
await page.getByLabel('Email').fill('test@example.com');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign In' }).click();
await page.context().storageState({ path: 'e2e/auth.json' }); // save cookies + localStorage
await browser.close();
// playwright.config.ts - use saved state
{
projects: [{
name: 'authenticated',
use: { storageState: 'e2e/auth.json' }, // replays auth state
}]
}Tip
Tip
Practice Playwright E2E Testing in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Playwright - E2E Testing
Practice Task
Note
Practice Task - (1) Write a working example of Playwright E2E Testing from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Playwright E2E Testing is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Finished this lesson?