Quick revision
The short answer
First we measure: every failure gets screenshots, page source, browser logs and video, and is tagged with its cause, timing, test data, environment or real bug. Then we fix the biggest causes: stable data-testid and role-based locators instead of positional XPath, explicit waits on application states in shared helpers instead of sleeps and implicit waits, and tests that create their own data through APIs and run in fresh browsers. Finally we change the process: quarantine known flaky tests with an owner and a deadline, track the flake rate per test, and stop blanket triple reruns, which hide real defects.
What to remember
In this answer, we treated flakiness as a measurable engineering problem. We started by capturing evidence on every failure and tagging causes, then fixed locators with data-testid and roles, replaced sleeps and implicit waits with shared explicit waits on application states, isolated tests with their own API-created data and fresh browsers, and replaced blanket triple reruns with quarantine, ownership, deadlines and a per-test flake rate, because reruns hide real defects.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we measure: every failure gets screenshots, page source, browser logs and video, and is tagged with its cause, timing, test data, environment or real bug. Then we fix the biggest causes: stable data-testid and role-based locators instead of positional XPath, explicit waits on application states in shared helpers instead of sleeps and implicit waits, and tests that create their own data through APIs and run in fresh browsers. Finally we change the process: quarantine known flaky tests with an owner and a deadline, track the flake rate per test, and stop blanket triple reruns, which hide real defects.
Why this question is asked
A flaky suite costs more than its own maintenance. With 900 tests and a 7 percent failure rate, about 63 tests fail on a typical run for no reason anybody understands, and three automatic reruns turn the pipeline green while teaching everyone that red means nothing. Interviewers ask this scenario to see whether we approach flakiness as an engineering problem with data, or as bad luck. In this answer, we will see what the reruns really cost, how to measure causes, and what to change in locators, waits, test isolation and process, in an order that brings the rate down quickly.
1What Three Reruns Really Cost
Flaky means a test sometimes passes and sometimes fails on the same code. Rerunning a failed test three times feels like a safety net, but it has three costs. Run time grows, because 63 failures each retried up to three times can add a large share of a full run. Trust shrinks, because a test that needs three tries is tested only on its lucky attempt. And most importantly, real bugs hide: an intermittent defect in the application, such as a race in the save endpoint, looks exactly like a flaky test, and the rerun makes it disappear.
So the goal is not a green pipeline. The goal is a pipeline in which a red result reliably means something is wrong.
2Measure Before Changing Anything
We cannot fix 63 failures by guessing. So the first change is to the test harness: on every failure, it saves a screenshot, the page source, the browser console log, and ideally a short video, and attaches them to the test report. With Selenium we can do this in a JUnit 5 extension that implements TestWatcher or an AfterTestExecutionCallback, which runs before the driver is quit.
Then, for two or three weeks, someone triages every failure and tags it with a cause. In practice the causes fall into a handful of buckets, and the counts tell us where to spend effort. The numbers in the table below are an example of what such a triage often shows, not a rule.
| Cause | Share | Typical evidence | Fix |
|---|---|---|---|
| Timing | 45% | Interception, element not found, spinner in screenshot | Explicit waits on app states |
| Locators | 15% | Element moved after a markup change | data-testid and roles |
| Test data | 20% | Duplicate user, order already paid | Own data per test via API |
| Environment | 12% | Grid timeout, service 503, slow agent | Infrastructure fixes, health checks |
| Real bug | 8% | Same failure reproducible by hand | Bug ticket, not a rerun |
3Fix the Locators
Locators that depend on layout, such as //div[3]/table/tr[2]/td[5]/button, break every time the page structure changes and sometimes match the wrong element when content shifts. We agree with the front-end developers on data-testid attributes for elements tests use, and on proper accessible roles and labels, so tests can find a button by its role and name. Then we remove positional XPath from the suite, starting with the tests the triage names most often.
This is a shared change: developers add the attributes, test engineers use them. It works best when the attributes are part of the reusable components, so one change covers every page that uses them.
4Replace Sleeps and Implicit Waits With Shared Explicit Waits
Timing is usually the largest bucket. The fix is to wait for states of the application rather than for time: the spinner is gone, the table finished re-rendering, the save request returned and a confirmation appeared. We set the implicit wait to zero, remove Thread.sleep calls, and put the waits in shared helper methods so every test uses the same tested logic.
Notice the data-loading attribute. A small hook like this, set by the application while requests are pending, gives tests a reliable signal. It is often the single most effective change, and it needs cooperation from the front-end team.
5Isolate Every Test
Test data problems come from sharing. If ten tests log in as the same user, one test that changes the user's settings breaks the others, and the result depends on the order and on which tests ran in parallel. So each test creates what it needs through the application's API before it opens the browser: its own user, its own order, its own product. It never depends on another test having run first.
Each test also gets a fresh browser session, so cookies, local storage and open dialogs from an earlier test cannot leak in. Creating data through APIs is also much faster than clicking through setup screens, which usually pays for the extra code.
6Change the Process
With causes understood, we replace blanket reruns with rules. A test known to be flaky is quarantined: it moves to a separate job that still runs and reports, but does not block merges. Every quarantined test has an owner and a deadline, for example two weeks, after which it is fixed or deleted. We track the flake rate per test, how often it failed and then passed on the same commit, and show the worst offenders on a dashboard reviewed every week.
If we keep any automatic retry at all, it is a single retry that is always reported, so a pass on retry still counts as a flake in the numbers. A realistic target is to bring the rate from 7 percent to below 1 percent over a quarter, with the triage data showing which change bought which improvement.
- Test fails on mainartifacts attached: screenshot, DOM, logs, video
- Reproducible on the same commit?YesReal bug: open a defectNoMark as a flake, record cause
- Flake rate above threshold?YesQuarantine with owner and deadlineNoKeep in the blocking suite
- Deadline reached?YesFixed, or deletedNoStays quarantined, still reported
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
How would you decide which tests to delete?
I look at value against cost. A test is a candidate for deletion if it duplicates coverage that an API or component test already gives, if it checks a minor detail rather than a user journey, if it has been quarantined past its deadline without anyone wanting to own it, or if it has never caught a real defect while failing often. Before deleting, I check that the behaviour it protects is covered somewhere cheaper, writing that lower-level test if needed. Deleting a low-value flaky test usually improves quality, because it returns trust to the rest of the suite.
How do you tell a flaky test from a real intermittent bug?
The artifacts are the main tool. If the screenshot shows a spinner still visible, it is a test timing problem. If the browser log shows a 500 from the server, or the same failure can be produced by hand, it is a product problem. That is why we never let reruns decide automatically.
Should flaky tests block merges?
Not while they are known to be flaky, because they teach people to ignore red builds. But they must not disappear: quarantine keeps them visible with an owner and a deadline, and the blocking suite contains only tests we trust.
8Conclusion
In this answer, we treated flakiness as a measurable engineering problem. We started by capturing evidence on every failure and tagging causes, then fixed locators with data-testid and roles, replaced sleeps and implicit waits with shared explicit waits on application states, isolated tests with their own API-created data and fresh browsers, and replaced blanket triple reruns with quarantine, ownership, deadlines and a per-test flake rate, because reruns hide real defects.
Open with
Seven percent of 900 is around 63 failures a run, and three blanket reruns turn that into a green build that nobody can trust and that hides real intermittent bugs.
Then, if they want more
I would start with measurement. I would make the harness capture a screenshot, page source, browser console logs and video on every failure, and for a couple of weeks tag each failure with its cause: timing, locator, test data, environment or a real defect. That tells us where the effort goes. Then I would fix the big buckets.
For locators, I would agree with the front-end developers on data-testid attributes and accessible roles and remove positional XPath. For waits, I would set implicit waits to zero, remove sleeps, and replace them with explicit waits on application states, such as a spinner gone or a loading flag cleared, in shared helper methods.
For isolation, every test creates its own data through APIs, never depends on order, and runs in a fresh browser session. On process, I would stop blanket reruns. Known flaky tests go into quarantine, still running and reporting but not blocking, each with an owner and a deadline to fix or delete.
I would track flake rate per test and review the worst ones weekly, and if I keep a retry it would be a single one that is always reported as a flake. My target would be under one percent within a quarter. To decide what to delete, I look for tests that duplicate cheaper coverage or never find real bugs.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A 900-test Selenium suite has a 7 percent flaky failure rate and the team reruns failed...": https://scrivio-interview-library.netlify.app/java/ui-testing/b30092de2b/