WebDriverWait Example

In many selenium test cases you need to use WebDriverWait class. For example when you need to check whether a web element is visible or not, clickable or not, enabled or disabled before perform any actions. This article will show you how to use WebDriverWait class to achieve above tasks with examples.

WebDriverWait class is always used together with ExpectedConditions class like below to perform explicit wait actions.

// Initiate FirefoxDriver object. 
WebDriver ffDriver = new FirefoxDriver();
ffDriver.get("http://www.amazon.com");

// Declare WebDriverWait object.
WebDriverWait waitObj = new WebDriverWait(ffDriver, 2);

// Declare the locator condition.
By byId = By.id("submitBtn");

// Wait for the web element to be clickable until timeout.
waitObj.until(ExpectedConditions.elementToBeClickable(byId));

ExpectedConditions class has a lot of methods that can be used to check different web element’s status. We will introduce them one by one. Before introduce, we will show you the example html file content.

TestWebDriverWaitExample.html

<form>
<input type=hidden id="userId" />
UserName : <input type=text id="userName" /><br/>
Password : <input type=password id="passwd" /><br/>
<input type="submit" value="Submit" disabled=true/>
</form>

There are following form web elements in it.

  1. One hidden input field with id userId.
  2. Visible input text filed username and password.
  3. One submit button which is disabled.
    example input form web page

ExpectedConditions Class Methods 

  1. presenceOfElementLocated(locator) : Check whether the located web element presented in the DOM tree or not. This method will return the located web element when it has been found. But presented web element maybe invisible.
    private static boolean checkWebElementPresent(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;	
         try	
         {
    	// Declare WebDriverWait object.
    	WebDriverWait waitObj = new WebDriverWait(ffDriver, 2);
    	
    	// Wait for the web element to present until timeout.
    	waitObj.until(ExpectedConditions.presenceOfElementLocated(byLocator));
         }catch(Exception ex)
         {
    	ex.printStackTrace();
    	ret = false;
         }
    		
         return ret;
    }

    Alternative Method

    private static boolean checkWebElementPresentByList(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;		
         List<WebElement> elementList = ffDriver.findElements(byLocator);
         if(elementList.size()==0)
         {
            ret = false;
         }
         return ret;
    }
  2. visibilityOfElementLocated(locator) : Check whether the located web element in web page is visible or not.
    private static boolean checkWebElementVisible(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;
    		
         try
         {
    	// Declare WebDriverWait object.
    	WebDriverWait waitObj = new WebDriverWait(ffDriver, 2);
    	
    	// Wait for the web element to present until timeout.
    	waitObj.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(byLocator));
         }catch(Exception ex){
    	ex.printStackTrace();
    	ret = false;
         }
    		
         return ret;
    }
  3. visibilityOfAllElements(List<WebElement> elements) : Check whether all the elements in the list is present and visible in current web page or not.
  4. invisibilityOfElementLocated(locator) : Check whether the located web element is invisible or not. The usage is same as above example java code.
  5. elementToBeClickable(locator) : Wait for the web element to be clickable until timeout.
    private static boolean checkWebElementClickeable(WebDriver ffDriver, By byLocator)
    {
       boolean ret = true;	
       try{
    	// Declare WebDriverWait object.
    	WebDriverWait waitObj = new WebDriverWait(ffDriver, 2);
    	
    	// Wait for the web element to present until timeout.
    	waitObj.until(ExpectedConditions.elementToBeClickable(byLocator));
       }catch(Exception ex)
       {
    	ex.printStackTrace();
    	ret = false;
       }
    		
       return ret;
    }
  6. You can click here to read all ExpectedConditions class’s methods descriptions.
READ :   How to run test cases using Internet Explorer in selenium automation script

Use WebElement Class’s Method To Check Element Status

  1. Check element enabled.
    private static boolean checkWebElementEnabled(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;
         try{
    	WebElement we = ffDriver.findElement(byLocator);
    	// Use WebElement's isEnabled() method.
    	ret = we.isEnabled();
         }catch(Exception ex)
         {
    	ex.printStackTrace();
    	ret = false;
         }
    		
         return ret;
    }
  2. Check element displayed.
    private static boolean checkWebElementDisplayed(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;	
         try{
    	WebElement we = ffDriver.findElement(byLocator);
    	// Use WebElement's isDisplayed() method.
    	ret = we.isDisplayed();
         }catch(Exception ex)
         {
    	ex.printStackTrace();
    	ret = false;
         }
    		
         return ret;
    }
  3. Check element selected.
    private static boolean checkWebElementSelected(WebDriver ffDriver, By byLocator)
    {
         boolean ret = true;
         try{
    	WebElement we = ffDriver.findElement(byLocator);
    	we.sendKeys("Jerry");
    			
    	// Use WebElement's isSelected() method.
    	ret = we.isSelected();
         }catch(Exception ex)
         {
    	ex.printStackTrace();
    	ret = false;
         }
    		
         return ret;
    }
  4. Click here to learn more about WebElement class’s methods.

Main Method To Call Above Example Mehtod

public static void main(String[] args) throws InterruptedException {
		
     // Initiate FirefoxDriver object. 
     WebDriver ffDriver = new FirefoxDriver();
     ffDriver.get("file://C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/webdriver/TestWebDriverWaitExample.html");

     // userId is a hidden form element.
     By byUserId = By.id("userId");
		
     // userName is a visible form element.
     By byUserName = By.id("userName");
		
     // Submit button is disabled form element.
     By bySubmitBtn = By.id("submitBtn");

     boolean presentUserId = TestWebDriverWaitExample.checkWebElementPresent(ffDriver, byUserId);
     if(presentUserId)
     {
	System.out.println("User id hidden field is present.");
     }
		
     boolean presentUserName = TestWebDriverWaitExample.checkWebElementPresentByList(ffDriver, byUserName);
     if(presentUserName)
     {
	System.out.println("User name input text box is present.");
     }
     boolean visiable = TestWebDriverWaitExample.checkWebElementVisible(ffDriver, byUserName);
     if(visiable)
     {
	System.out.println("User name input text box is visible.");
     }
		
     boolean clickable = TestWebDriverWaitExample.checkWebElementClickeable(ffDriver, bySubmitBtn);
     if(!clickable)
     {
	System.out.println("Submit button is not clickable.");
     }
		
     boolean enabled = TestWebDriverWaitExample.checkWebElementEnabled(ffDriver, bySubmitBtn);
     if(!enabled)
     {
	System.out.println("Submit button is disabled.");
     }
		
     boolean displayed = TestWebDriverWaitExample.checkWebElementDisplayed(ffDriver, bySubmitBtn);
     if(displayed)
     {
	System.out.println("Submit button is displayed.");
     }
		
     boolean selected = TestWebDriverWaitExample.checkWebElementSelected(ffDriver, byUserName);
     if(!selected)
     {
	System.out.println("UserName is not selected.");
     }
		
     Thread.sleep(10000);
		
     ffDriver.quit();

}

Download “WebDriverWaitExample.zip” WebDriverWaitExample.zip – Downloaded 52 times – 2 KB

(Visited 336 times, 2 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.