White Box Testing
Test your code
   Home      selenium2WebDriver-Wait
 

WebDriver waits

Now days most of web application using AJAX heavily. When page loaded in browser web element loaded in different time interval. Application Under Test and automation scripts runs at different speed. By the time we locate element and it did not loaded we get element not found exception. Selenium web driver provides various wait mechanism to overcome this issue



ImplicitWait

Implicit wait tells web driver to keep polling DOM for certain amount of time to locate web element. Implicit wait applicable for life of web driver instance and works for FindElement and FindElements


// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();

// Now use the firefox instance driver to open a page URL
driver.get("http://www.whiteboxtest.com/selenium-test1.php");
// Implicit wait for 10 second for all elements locating
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

WebElement someElement;
List<WebElement> elementList;

// Find the web element by name
someElement = driver.findElement(By.name("name-012"));
System.out.println(someElement.toString());

// Find the web element by ID
someElement = driver.findElement(By.id("id-012"));
System.out.println(someElement.toString());

driver.quit();
 

ConditionalWait

Benefit over implicit wait is to save execution time if certain condition met for what we were waiting
Explicit wait tells web driver to keep polling DOM for certain amount of time unless certain condition fulfilled.

// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Now use the firefox instance driver to open a page URL
driver.get("http://www.whiteboxtest.com/selenium-test1.php");
// Explicit wait for an element
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("shape")));

// Find the web element by Class Name
elementList = driver.findElements(By.className("shape"));
for(WebElement e:elementList)
{
System.out.println("" + e.getText());
}

driver.quit();
   

List of expected conditions

title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable - it is Displayed and Enabled.
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present


FluentWait

FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.


// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Now use the firefox instance driver to open a page URL
driver.get("http://www.whiteboxtest.com/selenium-test1.php");
// Fluent wait an element
Wait<WebDriver> wait1 = new FluentWait<WebDriver>(driver)
//Wait for the condition with timeout 30 seconds
.withTimeout(30, TimeUnit.SECONDS)
// poll interval of 5 seconds.
.pollingEvery(5, TimeUnit.SECONDS)
//ignore the NoSuchElementException
.ignoring(NoSuchElementException.class);
wait1.until(ExpectedConditions.titleIs("title"));

// Find the web element by Tag Name
someElement = driver.findElement(By.tagName("title"));
System.out.println(someElement.getText());

// Find the web element by Link Text
someElement = driver.findElement(By.linkText("whiteboxtest"));
System.out.println(someElement.getText());

// Find the web element by Partial Link Text
someElement = driver.findElement(By.partialLinkText("hello"));
System.out.println(someElement.getText());

// Find the web element by CSS
someElement = driver.findElement(By.cssSelector("#food span.dairy.aged"));
System.out.println(someElement.getText());

// Find the web element by XPath
someElement = driver.findElement(By.xpath(".//*[@id='food']/span[1]"));
System.out.println(someElement.getText());

// Close and clean firefox driver instance
driver.quit();
 


Download example source code Selenium2WebDriver-Wait