- Implicit Wait :- The Selenium WebDriver provides an implicit wait for synchronizing tests. When an implicit wait is implemented in tests, if WebDriver cannot find an element in the Document Object Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM. In other terms, an implicit wait polls the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object's instance. However, an implicit wait may slow down your tests when an application responds normally, as it will wait for each element appearing in the DOM and increase the overall execution time.In this recipe, we will briefly explore the use of an implicit wait; however, it is recommended to avoid or minimize the use of an implicit wait.
How to do it : Let's create a test on a demo AJAX-enabled application as follows:
public void testWithImplicitWait()
{
//Go to the Demo AJAX Application
WebDriver driver = new FirefoxDriver();
driver.get("http://dl.dropbox.com/u/55228056/AjaxDemo.html");
//Set the Implicit Wait time Out to 10 Seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
//Get link for Page 4 and click on it
WebElement page4button = driver.findElement(By.linkText("Page
4"));
page4button.click();
//Get an element with id page4 and verify it's text
WebElement message = driver.findElement(By.id("page4"));
assertTrue(message.getText().contains("Nunc nibh tortor"));
} catch (NoSuchElementException e) {
fail("Element not found!!");
e.printStackTrace();
} finally {
driver.close();
}
}
How it works : The Selenium WebDriver provides the Timeouts Interface for configuring the implicit wait. The Timeouts Interface provides an implicitlyWait() method, which accepts the time the driver should wait when searching for an element. In this example, a test will wait for an element to appear in DOM for 10 seconds:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Until the end of a test or an implicit wait is set back to 0, every time an element is searched using the findElement() method, the test will wait for 10 seconds for an element to appear.
- Explicit Wait :- The Selenium WebDriver also provides an explicit wait for synchronizing tests, which provides a better control when compared with an implicit wait. Unlike an implicit wait, you can write custom code or conditions for wait before proceeding further in the code. An explicit wait can only be implemented in cases where synchronization is needed and the rest of the script is working fine. The Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for implementing an explicit wait. The ExpectedCondition class provides a set of predefined conditions to wait before proceeding further in the code.
How to do it : Let's implement a test that uses the ExpectedConditions.titleContains() method to
implement an explicit wait as follows:
@Test
public void testExplcitWaitTitleContains()
{
//Go to the Google Home Page
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
//Enter a term to search and submit
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("selenium");
query.click();
//Create Wait using WebDriverWait.
//This will wait for 10 seconds for timeout before title is
//updated with search term
//If title is updated in specified time limit test will move to
//the text step
//instead of waiting for 10 seconds
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.titleContains("selenium"));
//Verify Title
assertTrue(driver.getTitle().toLowerCase().
startsWith("selenium"));
driver.quit();
}
How it works: We can create a wait for a set of common conditions using the ExpectedCondition class. First, we need to create an instance of the WebDriverWait class by passing the driver instance and timeout for a wait as follows:
WebDriverWait wait = new WebDriverWait(driver, 10);
Next, ExpectedCondition is passed to the wait.until() method as follows:
wait.until(ExpectedConditions.titleContains("selenium"));
The WebDriverWait object will call the ExpectedCondition class object every 500 milliseconds until it returns successfully.
Note : Minimize or avoid using an implicit wait in your tests and try to handle synchronization issues with an explicit wait, which provides more control when compared with an implicit wait.
I hope it helped you!