Understand the essentials of synchronization in Selenium, including implicit vs. explicit waits, fluent waits, ExpectedConditions, and handling timeouts effectively for reliable and robust web test automation.
MCQs on WebDriverWait and Synchronization in Selenium
1. Implicit vs. Explicit Wait
What is the primary purpose of using waits in Selenium? a) To slow down the test execution b) To synchronize the script with dynamic elements c) To improve browser performance d) To generate test reports
Which Selenium wait type applies a global timeout for all elements? a) Explicit wait b) Implicit wait c) Fluent wait d) WebDriverWait
How do you set an implicit wait in Selenium? a) driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); b) driver.wait(10); c) driver.manage().implicitTimeout(10); d) driver.setTimeout(10);
What happens if an element is not found within the implicit wait time? a) Selenium throws an exception immediately b) Selenium retries until the timeout is reached c) Selenium skips the test case d) Selenium executes the next command
When would you prefer explicit wait over implicit wait? a) When handling all elements in the DOM b) When dealing with a specific condition for an element c) When testing only static web pages d) When no dynamic content is present
Which of the following is used to define an explicit wait in Selenium? a) FluentWait b) Thread.sleep() c) WebDriverWait d) WaitDriver
How do you configure explicit wait for a button to be clickable? a) WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(button)); b) driver.explicitWait(10).click(button); c) driver.untilElementClickable(button, 10); d) button.waitForClickability(10);
Which command cancels an implicit wait in Selenium? a) driver.manage().timeouts().implicitlyWait(Duration.ZERO); b) driver.removeImplicitWait(); c) driver.manage().timeouts().disable(); d) driver.resetWaits();
How does implicit wait affect explicit waits in the same test? a) It overrides explicit waits b) Both waits are combined c) They work independently d) Implicit wait cancels explicit wait
What is one major drawback of using implicit wait? a) It can lead to longer execution times b) It is only applicable to buttons c) It cannot handle stale element exceptions d) It is no longer supported by Selenium
2. FluentWait and ExpectedConditions
What is FluentWait primarily used for? a) Waiting for an element to disappear b) Defining a custom polling interval and conditions c) Handling browser timeouts d) Managing static waits
Which of these is a key feature of FluentWait? a) It throws an exception immediately if the condition is not met b) It polls at specified intervals before throwing an exception c) It works only with buttons and links d) It disables implicit waits
How do you define a FluentWait with a polling interval? a) new FluentWait(driver).withTimeout(10).pollingEvery(1); b) new FluentWait(driver).withTimeout(Duration.ofSeconds(10)).pollingEvery(Duration.ofSeconds(1)); c) driver.waitFluently(10, 1); d) driver.FluentWait(10, 1);
What does the withMessage() method in FluentWait do? a) Sets a log message for successful waits b) Displays a custom exception message if the wait fails c) Defines the polling interval d) Automatically retries failed actions
Which ExpectedConditions is used to wait for an alert to appear? a) ExpectedConditions.alertPresent() b) ExpectedConditions.alertIsPresent() c) ExpectedConditions.isAlertPresent() d) ExpectedConditions.waitForAlert()
How do you handle a stale element reference exception using FluentWait? a) Catch the exception and retry immediately b) Configure retry logic in until() with a polling interval c) Use implicit wait to handle it automatically d) Selenium does not support stale element handling
Which of these is an example of a valid ExpectedCondition? a) ExpectedConditions.elementToBeVisible(element); b) ExpectedConditions.waitUntilLoaded(element); c) ExpectedConditions.isElementPresent(element); d) ExpectedConditions.waitForElement(element);
How does FluentWait improve upon WebDriverWait? a) It is faster than WebDriverWait b) It provides custom polling intervals and exception handling c) It replaces implicit waits d) It is used only for alerts
Which of the following is an invalid ExpectedConditions method? a) textToBePresentInElementLocated() b) elementToBeSelected() c) visibilityOfElementLocated() d) navigateToFrame()
What happens if FluentWait does not find the element within the timeout? a) The test fails immediately b) It throws a TimeoutException c) It retries indefinitely d) It cancels the test run
3. Handling Timeouts
What is a timeout exception in Selenium? a) An error when the script is paused manually b) An error when a wait condition is not met in time c) An error when the browser crashes d) An error when the WebDriver fails to start
How do you handle timeout exceptions in a Selenium script? a) Use a try-catch block and implement alternative logic b) Increase the system timeout settings c) Restart the browser d) Use Thread.sleep() instead of waits
Which method sets the page load timeout in Selenium? a) driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10)); b) driver.pageLoadTimeout(10); c) driver.waitForPageLoad(10); d) driver.setLoadTimeout(10);
What happens when a page load timeout is exceeded? a) The page continues to load, but the script moves on b) Selenium throws a TimeoutException c) The browser closes automatically d) Selenium retries the page load
How do you set a script timeout in Selenium? a) driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10)); b) driver.scriptTimeout(10); c) driver.manage().timeouts().setScriptTimeout(10); d) driver.setTimeout(Duration.ofSeconds(10));
Which timeout is used for asynchronous JavaScript execution? a) Page load timeout b) Implicit wait c) Script timeout d) Explicit wait
What is the default timeout for WebDriverWait? a) 5 seconds b) 10 seconds c) 15 seconds d) 20 seconds
How do you ensure the browser waits for AJAX calls to complete? a) Use explicit wait with ExpectedConditions.invisibilityOfElementLocated() b) Configure FluentWait with a polling interval c) Use driver.executeScript("return jQuery.active == 0"); d) All of the above
Which exception occurs when a page takes longer than the specified timeout to load? a) TimeoutException b) NoSuchElementException c) StaleElementReferenceException d) ElementNotInteractableException
Why should you avoid overusing Thread.sleep() in Selenium? a) It reduces script readability b) It adds unnecessary delays to execution c) It doesn’t handle dynamic waits effectively d) All of the above
Here are the answers again:
Qno
Answer
1
b) To synchronize the script with dynamic elements
2
b) Implicit wait
3
a) driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
4
b) Selenium retries until the timeout is reached
5
b) When dealing with a specific condition for an element
6
c) WebDriverWait
7
a) WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(button));
8
a) driver.manage().timeouts().implicitlyWait(Duration.ZERO);
9
c) They work independently
10
a) It can lead to longer execution times
11
b) Defining a custom polling interval and conditions
12
b) It polls at specified intervals before throwing an exception
13
b) new FluentWait(driver).withTimeout(Duration.ofSeconds(10)).pollingEvery(Duration.ofSeconds(1));
14
b) Displays a custom exception message if the wait fails
15
b) ExpectedConditions.alertIsPresent()
16
b) Configure retry logic in until() with a polling interval
17
a) ExpectedConditions.elementToBeVisible(element);
18
b) It provides custom polling intervals and exception handling
19
d) ExpectedConditions.waitForElement(element);
20
b) It throws a TimeoutException
21
b) An error when a wait condition is not met in time
22
a) Use a try-catch block and implement alternative logic
23
a) driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
24
b) Selenium throws a TimeoutException
25
a) driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10));