Explicit Wait- Synchronization in Selenium Part 2

Till now we have very well understood the concept and importance of Synchronization in Selenium WebDriver. It keeps the program up and running until the Expected and Actual results are same. We knew how WebDriver Wait is helping to establish the synchronization between Selenium testing tool and Application under test. The real use of WebDriver wait is seen in Explicit wait.

What is an Explicit wait?

An explicit wait is a command which inculcates WebDriver to wait for the specific elements to appear for the certain condition within the defined time frame. Thread.sleep() is the worst case of the Explicit wait, which incorporates the condition to wait for the exactly defined time.

It returns its result in Boolean. Once wait time expires then it throws Exceptions like TimeOutException, ElementNotVisibleException, etc.

Earlier we have seen in Implicit wait that it didn’t have any conditions over wait time, however, we find the conditions over wait time in Explicit wait. Hence, it makes “Explicit wait” a kind of intelligent wait.

It has the best use where the entire application under test is filled with dynamic WebElements as well as Elements which appears after AJAX loading.

Syntax of Explicit wait

Here first we create the instance of WebDriverWait and we pass driver as well as time duration as arguments. After this declaration, we define failure condition beyond the defined time. So here is the generalized command of Explicit wait:

WebDriverWait wait = new WebDriverWait(driver, timeOut);

wait.until(ExpectedConditions.predefined_conditions(By.locator(locator_expression)));

 

explicit wait

Pre-defined Expected conditions for Explicit wait

In above syntax, we have seen the place to put pre-defined conditions. This is the condition for which WebDriver will wait for the defined time duration. If the condition becomes false then it will throw an exception.

List of pre-defined conditions are:

  • titles()
  • titleContans()
  • visibilityOfElementLocated()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • elementToBeSelected()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • textToBePresentInElement()
  • textToBePresentInElementValue()
  • textToBePresentInElementLocated()
  • presenceOfElementLocated()
  • presenceOfAllElementsLocatedBy()
  • alertIsPresent()
  • invisibilityOfElementWithText()
  • invisibilityOfTheElementLocated()

Let me teach you the utilization of above conditions in your program.

Let us consider a scenario, where we are expecting some text in the title of the web page and it takes time for the title to appear. We can use Thread.sleep() or Implicit wait, but that is not a perfect choice because we have a condition available as title text. So let’s frame the command.

WebDriverWait wait = new WebDriverWait(driver, 20);

wait.until(ExpectedConditions. titleContans(“selenium”));

This program has set the failure condition as for the title, hence, it will wait till 20 seconds and beyond that, it will invoke an exception.

You can use any of the conditions from above list based on the scenarios or business requirement of placement of dynamic element.

Here is the sample code which uses explicit wait:

package SeleniumTester;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class LocatorDemo {
  @Test
  public void locatorDemo() throws Exception {
	  System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
	  
	  WebDriver driver = new ChromeDriver();
	  
	  //Implicit Wait
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  
	  driver.get("http://www.inviul.com");
	  
	  //Explicit Wait Declaration
	  WebDriverWait wait = new WebDriverWait(driver, 20);

	  wait.until(ExpectedConditions.titleContains("Selenium"));
	  
	  System.out.println("Explicit wait result- " +wait);
	  
	  driver.close();
	  
	  driver.quit();
  
  }
}

 

Leave a Reply