Skip to the page

UI automation · Write code

Review a Selenium test full of sleeps

The interviewer asks

This test passes on my machine but fails often in CI. What problems do you see, and how would you fix them?

public class SearchTest {
    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get("https://staging.example.com");
    }

    @Test
    public void searchShowsResults() throws InterruptedException {
        driver.findElement(By.xpath("/html/body/div[2]/div/header/div[3]/input")).sendKeys("shoes");
        driver.findElement(By.xpath("/html/body/div[2]/div/header/div[3]/button")).click();
        Thread.sleep(5000);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("result-card")));
        List<WebElement> results = driver.findElements(By.className("result-card"));
        Assert.assertTrue(results.size() > 0);
        driver.quit();
    }
}

Then

Follow-ups the interviewer asks next

  1. What's the difference between implicit, explicit and fluent waits?
  2. What happens when you mix a 10-second implicit wait with a 15-second explicit wait?
  3. Why is an absolute XPath a problem, and what would you use instead?
  4. If the assertion fails, what happens to the browser?
  5. When nothing matches, does findElements return an empty list straight away with this setup?
  6. How would you rewrite this with a page object?
  7. What's different about CI that you'd check?

The answer

What a strong answer covers

  • Thread.sleep(5000) always waits five seconds: too long when the app is fast, too short when it's slow. Replace it with an explicit wait for the real condition.
  • Mixed waits: Selenium's docs say not to mix implicit and explicit waits because wait times become unpredictable (their example: 10 s implicit and 15 s explicit can time out after 20 s). Use explicit waits and leave the implicit wait at 0.
  • The absolute XPath from /html/body breaks with any layout change. Use stable attributes: id, name, a data-testid, or a short relative locator tied to meaning (input[name='q'], //button[@aria-label='Search']).
  • driver.quit() inside the test: if the assertion fails first, the browser is never closed and CI machines fill with Chrome processes. Move it to @AfterMethod(alwaysRun = true).
  • The assertion is weak: after waiting for one card, size() > 0 proves little; check that results match "shoes", or the count shown on the page.
  • With a 10 s implicit wait, findElements waits the full 10 s before returning an empty list.
  • Hard-coded URL and browser: read them from config; set a window size, since headless Chrome defaults to 800x600 and the layout may change.
  • Page object: locators and actions in a SearchPage class; the test reads as steps and assertions.

The red pen

Common traps

Avoid:"increase the sleep"; a try/catch around the click that retries; "implicit waits are best practice for every element"; keeping the absolute XPath because the browser generated it.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview