End-to-End Testing with Playwright
What you'll build
A simple web page with a form and a Playwright E2E test suite that navigates to the page, fills in the form, submits it, and verifies the result.
Goals
- Install and configure Playwright
- Write E2E tests that navigate and interact with a real page
- Use locators to find elements
- Assert on page content and URL
- Understand E2E test trade-offs
Prerequisites
- Basic HTML and JavaScript
- A simple web page to test (or create one)
- Node.js and npm installed
Steps
- 1
Install Playwright
Run
npm init playwright@latestand follow the setup wizard.Expected result
Playwright is installed with example tests.
- 2
Create a test page
Create a simple HTML page with a login form.
Code<!DOCTYPE html> <html> <body> <form id="loginForm"> <input type="text" id="username" placeholder="Username"> <input type="password" id="password" placeholder="Password"> <button type="submit">Login</button> </form> <div id="message" style="display:none">Welcome!</div> </body> </html>Expected result
HTML page with login form is created.
- 3
Write the E2E test
Create a Playwright test that navigates to the page, fills the form, and submits.
Codeconst { test, expect } = require('@playwright/test'); test('login form works', async ({ page }) => { await page.goto('http://localhost:8080'); await page.fill('#username', 'admin'); await page.fill('#password', 'password'); await page.click('button[type="submit"]'); await expect(page.locator('#message')).toBeVisible(); await expect(page.locator('#message')).toHaveText('Welcome!'); });Expected result
E2E test runs in a real browser and passes.
- 4
Add assertions and error cases
Add a test for invalid login that checks for an error message.
Codetest('invalid login shows error', async ({ page }) => { await page.goto('http://localhost:8080'); await page.fill('#username', 'wrong'); await page.click('button[type="submit"]'); await expect(page.locator('#error')).toHaveText('Invalid credentials'); });Expected result
Error case E2E test passes.
- 5
Add boundary test cases
Add boundary-value cases to the test suite: empty array, zero, maximum value, null/undefined. Boundaries are where bugs most often hide, so they must be explicitly covered.
Codedescribe("boundary cases", () => { it("handles empty array", () => { expect(func([])).toEqual([]); }); it("handles zero", () => { expect(func(0)).toBe(0); }); it("handles null", () => { expect(() => func(null)).toThrow(); }); it("handles max value", () => { expect(func(Number.MAX_SAFE_INTEGER)).toBeDefined(); }); });Expected result
All boundary cases pass with no uncaught exceptions.
Try it yourself
What you learned
You learned to write E2E tests with Playwright that drive a real browser, interact with elements, and assert on page state. E2E tests verify the full user journey but are slower and more brittle than unit tests, so use them sparingly for critical flows.
Related Resources
Related Lessons
- E2E Testing
End-to-end testing overview.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.