Saturday, 22 October 2016

Implicit Wait , Explicit Wait and Fluent Wait in Selenium



As we have all faced that we need to wait for loading in web application.This is where our selenium tests fail if  the web element test trying to interact with is not visible on the screen. In this case If the Web element is not found then it throws the exception "ElementNotVisibleException".We can solve this issue of unpredictable test case failure in Selenium using "Waits".

Video Tutorial -

 

In this post we will see 3 types of Waits which are widely used in Selenium -

1.Implicit Wait.
2.Explicit Wait
3.Fluent Wait

1. Implicit Wait - 

Implicit Wait means informing selenium web driver to wait for specific amount of time and if the web element is not visible after waiting for that specific point then throw "No such Element found Exception". 

Check out the Example given below to understand how to use Implicit wait in Selenium Test Case.In below Test case we will add a wait for 30 seconds after opening the browser so that that the page is gets loaded completely.As we implicitly specify the time and Time Unit this is called as implicit wait.
method implicitlywait() accepts 2 parameters first one is time value and second one is time unit like days,hours,minutes,seconds,milliseconds etc.

package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
  System.setProperty("webdriver.chrome.driver", 
 "C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  driver.get("http://qaautomated.blogspot.in");
  driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
  driver.quit();
 }

}
 

2. Explicit Wait -

In Explicit wait along with wait time we also provide the wait condition. It will wait till the condition or the maximum wait time provided before throwing the Exception"ElementNotVisibleException".

We can say that Explicit wait is intelligent wait as it waits dynamically for specified condition. Check out the below test case example for using Selenium Explicit wait.First We need to instantiate WebDriverWait  wait object with Web Driver Reference and time frame in seconds.

Then we can use until() method for passing Expected Wait Condition and use it in our test case for waiting until the element is visible on the screen.Here we are using  visibilityofElementLocated() condition.To find out about other conditions type ExpectedConditions in eclipse IDE and press cltr+space bar and you will get the list of conditions which you can use in with Explicit Wait.

package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
 System.setProperty("webdriver.chrome.driver", 
 "C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
  driver.get("http://qaautomated.blogspot.in");
  WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));      
aboutMe.click();
  driver.quit();
 }

}
3. Fluent Wait -

Fluent wait is when we can setup a repeat time cycle to verify the condition is met or not. Fluent wait allows use to setup frequency with a time frame to verify the given expected condition at regular time interval. This wait is used in test scenario where element loading time is variable (it can be 10,15 or 20 secs) In this case we can use frequency to handle "ElementNotVisibleException".

Check the below example we are defining the wait time as 20 seconds and then 5 seconds for frequency. This means that the to wait for 20 secs to meet the condition after each 5 seconds till the condition is met.




package com.selenium.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; 
import java.util.concurrent.TimeUnit; 
 public class BrowserTests {

 public static void main(String[] args) throws Exception {
 System.setProperty("webdriver.chrome.driver",  
"C:\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)       
.withTimeout(20, TimeUnit.SECONDS)    
.pollingEvery(5, TimeUnit.SECONDS)    
.ignoring(NoSuchElementException.class); 
  driver.get("http://qaautomated.blogspot.in");
  WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
public WebElement apply(WebDriver driver) { 
return driver.findElement(By.id("about_me"));     
 }  
});  
  
aboutMe.click(); 

  driver.quit();
 }

}

27 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. An implicit wait is to tell Web Driver to poll the DOM for a certain amount of time when trying to find an element in selenium or elements if they are not immediately available.
    Software testing selenium training in Chennai

    ReplyDelete
  3. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete

  4. Everyone loves what you guys tend to be up too. Such clever work and coverage! Keep up the awesome works guys I've added you guys to our blogroll. apple itunes login

    ReplyDelete
  5. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    python training in chennai
    python course institute in chennai

    ReplyDelete
  6. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    excel advanced excel training in bangalore | Devops Training in Chennai

    ReplyDelete
  7. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    angularjs interview questions and answers

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in chennai

    automation anywhere online Training

    ReplyDelete
  8. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    Advanced AWS Interview Questions And Answers, Top 250+AWS Interviews Questions and Answers 2018
    Advanced AWS Interview questions and answers | Top 110 AWS Interview Question and Answers
    Advanced AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore
    Advanced AWS Training in Pune | Best Amazon Web Services Training in Pune
    Advanced AWS Online Training | Best Online AWS Certification Course in india

    ReplyDelete
  9. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
    Advanced AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    Amazon Web Services Training in Pune | Best AWS Training in Pune
    AWS Online Training | Online AWS Certification Course - Gangboard
    Top 110 AWS Interview Question and Answers | Advanced AWS interview Questions and Answers – AWS jobs

    ReplyDelete
  10. From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
    python course in pune
    python course in chennai
    python Training in Bangalore

    ReplyDelete
  11. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  12. Good Job, nice work and efforts. Your content is very interesting and i like that. I want a lot of info from your post....
    Automation Courses in Bangalore
    RPA Courses in Bangalore
    Robotics Classes in Bangalore
    Robotics Courses in Bangalore
    RPA Training in Bangalore

    ReplyDelete
  13. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    Devops Training in bangalore
    Digital Marketing Training in bangalore
    Data Science Training in bangalore
    Java Training in bangalore

    ReplyDelete
  14. This is a great post and enlightening post compassionately check this post.
    android app development

    ReplyDelete