Prompt
How do I set up browser-based end-to-end testing for my web app?
Latest observation
To set up browser-based end-to-end (E2E) testing for a web app, the most common path is:
- pick a browser automation tool
- install it in your project
- write tests that open your app in a real browser
- run them locally and in CI
A practical modern choice is Playwright. It’s fast, reliable, and supports Chromium, Firefox, and WebKit.
1) Choose a tool
Common options:
- Playwright — recommended for most new projects
- Cypress — also popular, great developer experience
- Selenium/WebDriver — older standard, useful for compatibility or enterprise setups
If you’re starting fresh, use Playwright.
2) Install Playwright
If you use Node.js
npm init playwright@latest
This will:
- install Playwright
- set up example tests
- configure browsers
Or, if you already have a project:
npm install -D @playwright/test
npx playwright install
3) Make your app testable
E2E tests are much easier if your UI has stable selectors.
Good practices
- Use
data-testidattributes - Prefer accessible roles and labels
- Avoid brittle CSS selectors or text that changes often
Example:
<button data-testid="login-button">Log in</button>
Then in tests:
await page.getByTestId('login-button').click();
Even better when possible:
await page.getByRole('button', { name: 'Log in' }).click();
4) Write a simple test
Example Playwright test:
import { test, expect } from '@playwright/test';
test('homepage has title and can navigate', async ({ page }) => {
await page.goto('http://localhost:3000');
await expect(page).toHaveTitle(/My App/);
await page.getByRole('link', { name: 'Sign in' }).click();
await expect(page).toHaveURL(/\/login/);
});
5) Run your app and tests
Usually you’ll need:
- your web app running locally
- the test runner executing against it
For example:
npm run dev
npx playwright test
If your app starts on a different port, update the test URL.
6) Configure base URL
In playwright.config.ts:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
baseURL: 'http://localhost:3000',
},
});
Then tests can do:
await page.goto('/');
7) Add setup for auth and test data
For real-world E2E testing, you’ll want:
- seeded test data
- a dedicated test database
- a test login flow or storage state reuse
Example: reuse signed-in state
You can log in once and save session state:
import { test as setup, expect } from '@playwright/test';
setup('authenticate', async ({ page }) => {
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 expect(page).toHaveURL('/');
await page.context().storageState({ path: 'storageState.json' });
});
Then use it in tests:
use: {
storageState: 'storageState.json',
}
8) Run tests in CI
In GitHub Actions, for example:
name: e2e
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npm run test:e2e
If your app must be started first, use a tool like:
start-server-and-test- a custom CI step that runs the app in the background
9) Keep tests stable
E2E tests can be flaky if they depend on timing or unstable state. To reduce flakiness:
- wait for UI state instead of hardcoded sleeps
- use Playwright’s built-in assertions
- isolate test data
- make tests independent
- avoid relying on animation timing
- retry only as a last resort
Bad:
await page.waitForTimeout(5000);
Better:
await expect(page.getByText('Dashboard loaded')).toBeVisible();
10) Suggested structure
A typical setup:
tests/
e2e/
login.spec.ts
checkout.spec.ts
playwright.config.ts
Recommended baseline setup
If you want the simplest robust setup:
- use Playwright
- add
data-testidor accessible labels - run your app on
localhost - configure
baseURL - use a test database or mocked backend where appropriate
- run in CI on every pull request
If you want, I can also give you:
- a Playwright setup for React / Next.js / Vue / Angular
- a Cypress setup
- a full GitHub Actions CI example
- a sample login flow test