E2E tests: few, but trustworthy
The usual e2e story ends like this: a team writes a dozen or more tests, they run slowly and fail occasionally for no visible reason. A few months later somebody disables them in CI. A few months after that, nobody runs them at all.
That follows from two problems — slowness and instability — and both are solvable.
In this article I want to describe how I approach e2e tests. The central idea is one thing: few tests, but ones you can trust.
How many e2e tests do you need
The answer is smaller than most people expect. Around ten, in my view.
The reason is that an e2e test is expensive. It is slow, it breaks easily, and it has to be maintained. So each one has to earn its cost.
Which ones earn it? Only the flows that lead to losing money or trust:
- Registration and sign-in
- Payment or placing an order
- Your core data-creation flow, whatever that is in your product
- Permissions: not being able to reach another user's data
Everything else belongs at a lower level. Using an e2e test to check that a form shows the right validation message is spending an expensive tool on cheap work.
The test question is simple: if this flow breaks, do I need to know within the hour? If not, you do not need an e2e test.
Selectors are the foundation of stability
The most common cause of flaky tests is the wrong selector.
The worst option is leaning on CSS classes:
await page.click('.btn-primary.mt-4')That breaks the moment the design changes. A class name is appearance, not behaviour.
The second bad option is binding tightly to text, especially on a multilingual site. Switch language and the test dies.
I order my preferences like this.
First, by role and name — the way a user perceives the element:
await page.getByRole('button', { name: /kirish|войти|sign in/i }).click()This has a bonus: it checks accessibility along the way. If the element cannot be found, it is unclear to a screen reader too.
Second, a dedicated test attribute:
<button data-testid="checkout-submit">Place order</button>await page.getByTestId('checkout-submit').click()This is the most stable option. Its one condition is that the attribute survives design changes. The team needs one rule: nobody deletes data-testid during a cleanup.
The real cause of flaky tests
Unstable tests usually get called "random". In practice they almost always come from one thing: the test waits for the wrong thing, or fails to wait for the right thing.
The most common mistake is a hard pause:
await page.waitForTimeout(2000) // neverThis is bad in both directions. On a slow machine two seconds is not enough and the test fails. On a fast one you waste two seconds.
The correct approach is to wait for state, not for time:
await expect(page.getByText('Order received')).toBeVisible()Playwright waits automatically in assertions like this and continues the instant the condition holds.
The second common cause is shared state between tests. Data created by one test affects another, and everything collapses when the order changes.
The rule: each test creates the data it needs and cleans it up. Tests must be independent — and then you can run them in parallel too.
The third cause is external services. If a test hits a payment provider, its stability is not yours to control. Intercept those boundaries:
await page.route('**/api/payment/**', (route) =>
route.fulfill({ json: { status: 'ok' } })
)What to do about data
The most time-consuming question in e2e testing is what data the test works with.
The worst answer is a shared test database. The data drifts over time and the tests quietly become liars.
Two approaches served me well.
The first is that each test creates its own data, with a random identifier:
const email = `test-${crypto.randomUUID()}@example.com`That isolates tests and lets them run in parallel.
The second is authenticating once. Logging in inside every test is slow. Playwright lets you save session state:
// once, in a setup file
await page.context().storageState({ path: 'auth.json' })
// then in every test
test.use({ storageState: 'auth.json' })This is usually the single change that speeds a suite up the most.
Running them in CI
The first rule: do not run e2e on every commit. They are slow, and waiting five minutes on every push teaches people to hate tests.
I split it like this: unit and integration tests on every commit; e2e when a pull request opens and before merging to the main branch.
The second rule: keep the artefacts. When a test fails in CI and all you have is the error text, finding the cause is hard. Playwright can save video, screenshots and traces automatically:
export default defineConfig({
use: {
trace: 'retain-on-failure',
video: 'retain-on-failure',
},
})The trace is especially valuable — it lets you step back through the test. That cuts the time spent investigating CI failures several times over.
The third rule: be careful with retries. One retry in CI is acceptable, because it absorbs network blips. But if a test consistently passes only on the second attempt, that is a problem to fix, not to hide.
I use a simple rule for it: a test that passes only on retry twice in one week must be fixed or deleted. A test you cannot trust is worse than no test, because it hands you false confidence.
Where to start
If you have no e2e tests today, start like this:
- Pick one most important flow. Usually registration or payment.
- Write it simply, with
data-testid. - Run it in CI at the pull request stage.
- Live with that for a month. If the test never fails, that is a good sign — it is watching your back.
- Add the second flow.
When you reach ten tests, stop and ask: which of these caught a real bug in the last six months? Delete the ones with no answer.
The goal of an e2e suite is not coverage. The goal is to shorten the manual checking you do before a release. If the suite does not give you confidence that the important things work, it is not doing its job — regardless of how many tests it contains.