WebDriverWait Example

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

1. How To Use WebDriverWait Class To Perform Explicit Wait Actions.

  1. WebDriverWait class is always used together with the 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));
  2. 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.
  3. TestWebDriverWaitExample.html: This is the example Html file and the below source code is it’s content.
    <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>
  4. There are the following form web elements on the above Html page.
  5. One hidden input field with id userId.
  6. Visible input text filed username and password.
  7. One submit button which is disabled.
    example-input-form-web-page

1.1 ExpectedConditions Class Methods.

  1. presenceOfElementLocated(locator): Check whether the located web element is presented in the DOM tree or not. This method will return the located web element when it has been found. But the presented web elements may be 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 the 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 are present and visible on the current web page or not.
  4. invisibilityOfElementLocated(locator): Check whether the located web element is invisible or not. The usage is the same as the 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.

1.2 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.

1.3 The Main Method To Call Above Example Method.

  1. Below is the java main method that is used to call the above example method.
    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();
    
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x