Testing web applications

Why tests fail intermittently and how to stabilise them

A test fails, then passes on the next run without a code change. Calling it flaky or unstable describes the symptom, but does not explain the cause. The defect may be in the test, application or environment. A genuine problem with concurrent order processing can also appear only occasionally.

To stabilise the test, you need to find out what happened differently in the failed run. Increasing every wait or rerunning until the result is green can remove the visible symptom while leaving the defect. Start with evidence from the original failure, then investigate one specific hypothesis.

Is the application, test or environment at fault?

Compare the expectation with what the application actually did. If it was supposed to confirm an order and stored the wrong price, that is a product defect. If it stored the correct price but the test checked a different table row, fix the test. If the order could not be created because the test database was unavailable, investigate the environment.

These categories can overlap. A slow service can expose an application defect that loses data when a response is delayed. An overloaded environment can reveal an incorrect wait in the test. The Google Testing Blog describes causes in tests, applications, dependencies and infrastructure. A successful retry therefore does not justify closing a product defect.

For an incorrect payment, missing permissions or data loss, assess the impact immediately. You do not need to wait for the failure to become frequent before investigating it. If you do not yet know the cause, record it as unresolved instead of automatically assigning every intermittent failure to the tests.

Preserve the failed attempt first

Record the test name and step, expected and actual result, and versions of the application, tests and browser. Include the environment, number of concurrent processes, time, time zone and whether this was the initial attempt or a retry. For an order, you also need its identifier and the corresponding request or server log entry.

Separate a data preparation failure from a failure of the scenario itself. Check the starting state before the first user action. Otherwise, a missing order can look like a broken confirmation step.

A screenshot shows the screen state, a video shows the sequence, and a trace provides a timeline of actions with network activity. Availability depends on configuration. In Playwright, on-first-retry records the first retry, not automatically the original failure. To diagnose the original attempt, select suitable recording, such as retain-on-failure, according to your project’s version. The recording configuration explains the differences.

Do not include authentication tokens or personal data in shared attachments. Diagnostic records should let you connect an action to a specific request without exposing a password. For tracking recurring failures, see how to measure flaky tests.

Model incident: confirming the wrong order

Imagine a test that creates an order, clicks confirm and looks for the text “Confirmed” in the first row of the list. It passes when run alone. In the full suite it sometimes fails, then passes on retry. A concurrent test uses the same account and adds another order.

First connect the click to the identifier of the order you created. Check the server record to establish whether that order was confirmed. If it was, but a newer order appeared at the top of the list, the test checked the wrong row. The fix is to locate its own order and separate the data used by concurrent tests. A longer pause would not fix the row selection.

If the server confirmed a different order despite receiving the correct identifier in the request, investigate a product defect. If no request was sent, examine the click, form readiness and browser errors. This is a model example, not an account of a measured incident. It shows why the same error message can have different causes.

What to try for each symptom

The table contains hypotheses to investigate. A single successful experiment does not confirm the diagnosis.

Symptom Possible cause Experiment and evidence needed
The result has not appeared yet The test checks state before the operation finishes Connect the response and result to a specific identifier. Compare their timing with the test’s check.
An element is found only sometimes The selector depends on HTML structure, a generated class or ordering Check how many elements match in the failed state. Try an unambiguous role and name or an agreed identifier.
The test fails only during concurrent runs Tests modify a shared account, order or setting Keep concurrency and separate the conflicting data. Compare which test changed it.
A response is late or missing A slow application, external service or insufficient resources Inspect the request, logs and load. Use a controlled service substitute only outside a test of the real integration.
Failure depends on the date or a previous test Time zone, token expiry or an unprepared state Prepare the starting state explicitly. Recreate the same time case or ordering and compare the result.

Reducing concurrency can help identify a line of investigation. It also reduces load and the number of collisions, so it does not by itself distinguish overload from shared data. Change one condition at a time and retain the results of experiments that did not support the hypothesis.

Waiting for state and retrying a test serve different purposes

Distinguish three mechanisms:

When confirming an order, expect a result for its identifier rather than any successful response. Register the response wait before the action so you do not miss a fast response. Check the relevant method, URL and request or response data. The network events documentation shows how to register the wait.

Even a successful HTTP status code does not necessarily mean processing is complete. If the system merely accepts an operation and processes it later, verify the agreed final state. Choose the timeout according to the test’s purpose and expected application behaviour. If the product exceeds its agreed response time, increasing the test timeout does not fix that problem.

A fixed pause waits just as long when the result is ready sooner, and is insufficient when it arrives later. Selenium explains conditional waiting and the difference between document loading and readiness of a dynamic interface.

Separate data, time and external dependencies

A separate browser context isolates cookies and browser storage. It does not isolate a server account, stock item or order. Tests that modify shared settings need their own accounts or another way of isolating the relevant state. The authentication recommendations also explain this distinction.

Keep data unique across concurrent jobs in continuous integration (CI), which checks code changes automatically. A process number in one run may not be unique in another. During cleanup, delete only your own records so a test does not remove another scenario’s order. Prepare the starting state before every test instead of relying on a previous test passing.

Locate elements by role and name or an agreed test identifier. In a list, also identify the specific record. A Page Object Model brings interface interactions together, but does not fix an incorrect selector or shared data.

For time-related cases, specify the time zone and relevant instant, such as expiry or a month boundary. Playwright clock controls time within the page. It does not automatically change server time or the validity of a server token. Those require their own preparation and verification.

Replace an external service with a controlled response when testing your own application’s reaction to success, failure or delay. Keep separate coverage of the real integration. If the test checks a payment gateway, its outage is a relevant result. Using a substitute does not mean that outage has been resolved.

Verify the fix and manage the interim risk

Test the fix under the conditions that triggered the problem, including concurrency, data and load. First establish a reproducible case if possible, then change the confirmed cause. Compare initial attempts as well as retries. The Chromium diagnostic guide describes how to investigate shared state.

Also check whether the test still detects an incorrect result. If it starts accepting any order state, it has become stable by losing its check. Repeated passes support the conclusion that the fix worked, but do not guarantee the absence of every intermittent defect.

If you temporarily move a test out of the checks that block a release, assign an owner, a repair task, a review deadline and a condition for reinstatement. Keep it running separately and monitor its results. Arrange alternative verification for significant risks, such as a specific manual scenario. An API test can replace a server check, but will not verify the browser step you left out.

Start with one failure that has a significant impact and available evidence. Record the confirmed cause, change and verification method. That record will help you decide whether the next similar report concerns the same defect or merely the same symptom.

Related topics

You might also be interested in

Catch bugs before they reach users

End-to-end tests of key scenarios can run automatically in CI/CD to catch bugs before they reach customers.