UI automation · Write code
Review a Selenium test full of sleeps
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe 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
- What's the difference between implicit, explicit and fluent waits?
- What happens when you mix a 10-second implicit wait with a 15-second explicit wait?
- Why is an absolute XPath a problem, and what would you use instead?
- If the assertion fails, what happens to the browser?
- When nothing matches, does findElements return an empty list straight away with this setup?
- How would you rewrite this with a page object?
- 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/bodybreaks with any layout change. Use stable attributes:id,name, adata-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() > 0proves little; check that results match "shoes", or the count shown on the page. - With a 10 s implicit wait,
findElementswaits 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
SearchPageclass; 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 voicePractise 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.