Prompt
How do I set up end-to-end testing for a web app
Latest observation
End-to-end (E2E) testing checks your app the way a real user would: opening the browser, clicking around, filling forms, and verifying the full flow works.
1) Pick an E2E tool
Popular choices:
- Playwright — modern, fast, great cross-browser support
- Cypress — very popular, easy to start with
- Selenium — older, very broad ecosystem
- WebdriverIO — flexible, good for larger setups
If you’re starting fresh, Playwright is often the best default.
2) Install the test runner
Example with Playwright:
npm init playwright@latest
This sets up:
- test framework
- browser binaries
- sample tests
- config file
3) Make your app testable
Good E2E tests depend on stable selectors and predictable behavior.
Add test-friendly selectors
Use attributes like:
<button data-testid="login-button">Log in</button>
Then target them in tests instead of fragile CSS classes or text that changes often.
Keep environments consistent
Use separate configs for:
- local dev
- staging
- CI
E2E tests should usually run against a clean, dedicated test environment.
4) Create realistic test data
Your tests may need:
- seeded users
- sample orders/posts/projects
- known passwords
- mocked payment or email services
Ways to handle this:
- seed the database before tests
- use API calls to create test fixtures
- reset the database between test runs
5) Write your first test
Example in Playwright:
import { test, expect } from '@playwright/test';
test('user can log in', async ({ page }) => {
await page.goto('http://localhost:3000/login');
await page.getByTestId('email').fill('test@example.com');
await page.getByTestId('password').fill('password123');
await page.getByTestId('login-button').click();
await expect(page).toHaveURL(/dashboard/);
await expect(page.getByText('Welcome')).toBeVisible();
});
6) Run tests locally
Typical workflow:
- start your app
- run the E2E suite
- fix flaky tests before adding more
Example:
npm run dev
npx playwright test
7) Add tests to CI
Run E2E tests automatically on:
- pull requests
- merges to main
- scheduled nightly builds
Example CI steps:
- install dependencies
- start app/backend
- wait until services are ready
- run E2E tests
- collect screenshots/videos on failure
8) Handle external dependencies
E2E tests often fail because of:
- flaky third-party APIs
- email/SMS providers
- payment gateways
- network timing issues
Best practice:
- mock external services in test environments
- use sandbox modes
- isolate what you control
9) Keep tests stable
To reduce flakiness:
- avoid arbitrary
sleepcalls - wait for specific UI conditions
- use reliable locators
- keep tests independent
- don’t test too many things in one test
10) Start small
Begin with a few critical user journeys:
- sign up
- log in
- create/edit/delete a key object
- checkout or submit form
- logout
Then expand over time.
Recommended minimal setup
If you want a simple, practical stack:
- Playwright
- data-testid selectors
- seeded staging/test database
- CI integration with GitHub Actions
If you want, I can give you:
- a Playwright setup for React/Next.js/Vue
- a Cypress setup
- a sample GitHub Actions workflow
- a best-practice folder structure for E2E tests