Unit tests tell you that functions work. End-to-end (E2E) tests tell you that users can actually do things: log in, submit a form, pay, download a report. For React and Django projects, Playwright has become our default E2E tool. Here's how we set it up.
Why Playwright
- Auto-waiting. It waits for elements to be visible and ready before acting, which removes most of the flakiness older tools were known for.
- All major browsers (Chromium, Firefox, WebKit) from one API.
- Parallel runs and isolated browser contexts out of the box.
- Great debugging: trace viewer, screenshots, videos and a code generator.
- Official bindings for TypeScript/JavaScript and Python, so frontend and backend developers can both write tests.
Write tests the way users see the page
Use role- and label-based locators instead of brittle CSS selectors:
import { test, expect } from '@playwright/test';
test('user can submit an application', async ({ page }) => {
await page.goto('/applications/new');
await page.getByLabel('Full name').fill('Test User');
await page.getByLabel('Qualification').selectOption('Engineering');
await page.getByRole('button', { name: 'Submit' }).click();
await expect(page.getByText('Application submitted')).toBeVisible();
});
These tests survive styling changes and double as an accessibility check. If Playwright can't find a field by its label, neither can a screen reader.
Test data: the key to stable tests
Most flaky E2E suites are really data problems. Our rules:
- Each test creates the data it needs through an API or a test-only endpoint, not by clicking through the UI.
- Use a dedicated test database, reset between runs (Django fixtures or factory_boy work well).
- Never depend on test order. Every test must pass when run alone.
Handle login once
Logging in through the UI in every test is slow. Log in once in a setup project, save the browser storage state, and reuse it:
// auth.setup.ts
test('authenticate', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email').fill(process.env.E2E_USER!);
await page.getByLabel('Password').fill(process.env.E2E_PASS!);
await page.getByRole('button', { name: 'Sign in' }).click();
await page.context().storageState({ path: '.auth/user.json' });
});
This works for both Django session auth and JWT-based APIs.
Mock what you don't own
Payment gateways, SMS providers and third-party APIs should be mocked in most E2E runs. Playwright's page.route() can intercept requests and return canned responses. Keep a small number of tests that hit real sandbox environments, and run them less often.
Running in CI
- Start Django (with a test database) and the React build in containers.
- Run Playwright in its official Docker image so browsers and dependencies are consistent.
- Upload traces and screenshots of failed tests as artifacts.
- Run a fast smoke suite on every pull request and the full suite nightly.
What to cover first
Don't aim for 100% E2E coverage. Cover the critical user journeys, the five to fifteen flows that would cost you money or reputation if they broke: sign-up, login, the main create/submit flow, payment, and key reports. Leave the rest to unit and API tests.
Want a reliable regression suite for your product? Our QA testing team can set it up and hand it over to your developers.