0
0
Selenium Javatesting~20 mins

Thread.sleep vs proper waits in Selenium Java - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Wait Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why avoid Thread.sleep in Selenium tests?

In Selenium test automation, why is using Thread.sleep() generally discouraged compared to proper waits?

AThread.sleep pauses the test for a fixed time, which can cause unnecessary delays or flaky tests if the element loads faster or slower.
BThread.sleep automatically waits only until the element is visible, making tests faster and more reliable.
CThread.sleep retries finding elements multiple times until timeout, improving test stability.
DThread.sleep dynamically adjusts wait time based on network speed, optimizing test execution.
Attempts:
2 left
💡 Hint

Think about what happens if the page loads faster or slower than the fixed sleep time.

Predict Output
intermediate
2:00remaining
Output of Selenium wait code snippet

What will be the output of the following Selenium Java code snippet?

Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");

try {
    Thread.sleep(3000);
    System.out.println("Slept for 3 seconds");
} catch (InterruptedException e) {
    System.out.println("Interrupted");
}

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nonexistent")));
System.out.println("Element found");
A
Interrupted
Exception thrown: NoSuchElementException
B
Slept for 3 seconds
Exception thrown: TimeoutException before printing 'Element found'
C
Slept for 3 seconds
Element found
D
Slept for 3 seconds
No output after because of infinite wait
Attempts:
2 left
💡 Hint

Consider what happens when waiting for an element that does not exist with explicit wait.

assertion
advanced
1:30remaining
Correct assertion for element visibility after wait

Which assertion correctly verifies that an element is visible after using an explicit wait in Selenium Java?

Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitBtn")));
AassertTrue(element.isDisplayed());
BassertNull(element);
CassertFalse(element.isEnabled());
DassertEquals(element.getText(), null);
Attempts:
2 left
💡 Hint

Think about what visibilityOfElementLocated guarantees about the element.

🔧 Debug
advanced
1:30remaining
Identify the problem with this wait usage

What is the main problem with the following Selenium Java code snippet?

Selenium Java
driver.findElement(By.id("loginBtn")).click();
Thread.sleep(5000);
WebElement message = driver.findElement(By.id("welcomeMsg"));
System.out.println(message.getText());
AThread.sleep automatically waits for the message element, so no problem here.
BThe click action is missing a wait, so it never triggers the message to appear.
CUsing Thread.sleep causes fixed wait; if the message loads slower than 5 seconds, test fails with NoSuchElementException.
DThe message element is located before the click, causing stale element reference error.
Attempts:
2 left
💡 Hint

Consider what happens if the welcome message takes longer than 5 seconds to appear.

framework
expert
2:00remaining
Best practice for waiting in Selenium test framework design

In designing a Selenium test framework, which approach best balances test speed and reliability regarding waits?

AMix Thread.sleep and implicit waits randomly to cover all cases.
BUse Thread.sleep everywhere to ensure fixed delays and avoid flaky tests.
CUse implicit waits only and never explicit waits or Thread.sleep.
DUse explicit waits with reasonable timeouts for expected conditions and avoid Thread.sleep entirely.
Attempts:
2 left
💡 Hint

Think about how explicit waits work compared to fixed sleeps and implicit waits.