0
0
Selenium Javatesting~15 mins

Thread.sleep vs proper waits in Selenium Java - Automation Approaches Compared

Choose your learning style9 modes available
Verify login functionality using explicit wait instead of Thread.sleep
Preconditions (2)
Step 1: Open the login page URL
Step 2: Enter 'testuser@example.com' in the email input field with id 'email'
Step 3: Enter 'Test@1234' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait until the dashboard page loads by checking the presence of element with id 'dashboardHeader'
Step 6: Verify that the current URL is 'http://example.com/dashboard'
✅ Expected Result: User is successfully logged in and dashboard page is displayed with correct URL
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify presence of dashboard header element after login
Verify current URL is the dashboard URL
Best Practices:
Use explicit waits (WebDriverWait) instead of Thread.sleep
Use By.id locator strategy for stable element identification
Use try-catch or timeout handling for waits
Keep code readable and maintainable
Automated Solution
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 java.time.Duration;

public class LoginTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("http://example.com/login");

            // Enter email
            WebElement emailInput = driver.findElement(By.id("email"));
            emailInput.sendKeys("testuser@example.com");

            // Enter password
            WebElement passwordInput = driver.findElement(By.id("password"));
            passwordInput.sendKeys("Test@1234");

            // Click login button
            WebElement loginButton = driver.findElement(By.id("loginBtn"));
            loginButton.click();

            // Explicit wait for dashboard header
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            WebElement dashboardHeader = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.id("dashboardHeader"))
            );

            // Assertion: dashboard header is displayed
            if (dashboardHeader.isDisplayed()) {
                System.out.println("Dashboard header is visible: PASS");
            } else {
                System.out.println("Dashboard header is not visible: FAIL");
            }

            // Assertion: URL is dashboard URL
            String currentUrl = driver.getCurrentUrl();
            if ("http://example.com/dashboard".equals(currentUrl)) {
                System.out.println("URL is correct: PASS");
            } else {
                System.out.println("URL is incorrect: FAIL");
            }

        } finally {
            driver.quit();
        }
    }
}

This code uses Selenium WebDriver with Java to automate the login test.

We open the login page, enter the email and password, and click the login button.

Instead of using Thread.sleep, we use WebDriverWait with ExpectedConditions.visibilityOfElementLocated to wait until the dashboard header is visible. This is more reliable and faster because it waits only as long as needed.

We then check if the dashboard header is displayed and if the URL is correct, printing pass/fail messages accordingly.

Finally, the browser is closed in the finally block to ensure cleanup.

Common Mistakes - 3 Pitfalls
Using Thread.sleep to wait for page elements
Using brittle XPath locators instead of stable IDs
Not handling exceptions or timeouts in waits
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login success and failure scenarios.

Show Hint