0
0
Selenium Javatesting~10 mins

Thread.sleep vs proper waits in Selenium Java - Test Execution Compared

Choose your learning style9 modes available
Test Overview

This test demonstrates the difference between using Thread.sleep and proper Selenium waits (WebDriverWait) to wait for an element to appear before clicking it. It verifies that the element is clickable and the click action succeeds.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;

public class WaitComparisonTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/delayed-button");
    }

    @Test
    public void testUsingThreadSleep() throws InterruptedException {
        // Wait fixed 5 seconds
        Thread.sleep(5000);
        WebElement button = driver.findElement(By.id("delayedButton"));
        button.click();
        WebElement message = driver.findElement(By.id("message"));
        assertTrue(message.isDisplayed(), "Message should be displayed after click");
    }

    @Test
    public void testUsingWebDriverWait() {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("delayedButton")));
        button.click();
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("message")));
        assertTrue(message.isDisplayed(), "Message should be displayed after click");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 11 Steps
StepActionSystem StateAssertionResult
1Test starts - Chrome browser opens and navigates to https://example.com/delayed-buttonBrowser shows page with a button that appears after delay-PASS
2testUsingThreadSleep: Thread.sleep(5000) pauses test for 5 secondsBrowser waits with page loaded, button expected to appear after delay-PASS
3Find element with id 'delayedButton' using driver.findElementButton is present and visible after sleepElement found successfullyPASS
4Click the 'delayedButton'Button clicked, page updates to show message-PASS
5Find element with id 'message' and check if displayedMessage element visible on pageassertTrue(message.isDisplayed()) verifies message is visiblePASS
6TestUsingWebDriverWait: Create WebDriverWait with 10 seconds timeoutBrowser ready on same page-PASS
7Wait until 'delayedButton' is clickable using ExpectedConditionsWaits dynamically until button appears and is clickableWait confirms button is clickable before proceedingPASS
8Click the 'delayedButton'Button clicked, page updates to show message-PASS
9Wait until 'message' element is visibleWaits dynamically until message appearsWait confirms message is visible before assertionPASS
10Assert message is displayedMessage visible on pageassertTrue(message.isDisplayed()) verifies message is visiblePASS
11Test ends - Browser closesBrowser closed, resources freed-PASS
Failure Scenario
Failing Condition: Using Thread.sleep with insufficient wait time causes element not to be found
Execution Trace Quiz - 3 Questions
Test your understanding
Why is using WebDriverWait better than Thread.sleep in Selenium tests?
AThread.sleep is faster because it waits a fixed time
BWebDriverWait waits only as long as needed for a condition, making tests faster and more reliable
CWebDriverWait does not wait at all
DThread.sleep automatically retries finding elements
Key Result
Always prefer explicit waits like WebDriverWait over fixed waits like Thread.sleep to make tests more reliable and faster by waiting only as long as needed.