Part 4: Waits In Selenium: Fluent Wait

Hello Folks,

We have covered below types of waits present in Selenium WebDriver:

  1. Thread.sleep()
  2. Implicit Wait.
  3. Explicit Wait

Consider below scenarios:-

You are testing Flipkart home page. Flipkart lists top items at home page which includes electronics, furniture etc. Now scenario is you need to find first 10 mobile displayed at home page. First of all you need to list all displayed item. It will include all displayed items. Then you need to filter out mobile out of all result. Once you find 10th mobile, you need to stop looking for next and return model names of top 10 mobiles.

You can use sleep, Implicit or explicit wait for above scenario but you can just wait till elements are found. But you need to wait till 10th mobile item is listed at home page. Apart from that Filtering, verification of count and returning model number can not be done along wait. But it can be achieved using fluent wait.

Sample code for above scenario:

FluentWait wait5 = new FluentWait(driver);		
wait5.withTimeout(30, TimeUnit.SECONDS);
wait5.pollingEvery(5, TimeUnit.SECONDS);
wait5.ignoring(NoSuchElementException.class);

Function flag=  new Function() 
		{
				   public Boolean apply(WebDriver driver) 
				   {
			       List allEle= driver.findElements(By.name("foo"));
			       List allText= new ArrayList<>();
			       for(WebElement e: allEle)
			       {
			    	   String textEle= e.getText();
			    	   if(textEle.equals("SomeData"))
			    	   allText.add(textEle);
			    	   if(allText.size()==10)
			    	   {
			    		   return true;
			    	   }
			       }
			       return false;
			     }

		};
wait5.until(flag);

Above code will make webdriver wait till 10th mobile item is displayed or waiting time expires.

Selenium webdriver provides another type of wait which is called Fluent wait. As per my understanding, Fluent wait is just an upgraded version of Explicit wait. In explicit wait, expected conditions are canned(predefined) but by using fluent wait, you can write your own conditions, you can assert and can do whatever you want.

What is FluentWait?

  • FluentWait is class in selenium which implements Wait interface. WebDriverWait is subclass of FluentWait. Now you remember syntax of defining explicit wait. You will be able to find relationship between explicit wait and fluent wait.
  • It  is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
  • Each 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.

From the Selenium Official documentation:-

  // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait wait = new FluentWait(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

Creating an instance of FluentWait can be simplified as:

FluentWait wait5 = new FluentWait(driver);
wait5.withTimeout(30, TimeUnit.SECONDS);
wait5.pollingEvery(5, TimeUnit.SECONDS);
wait5.ignoring(NoSuchElementException.class);

Now see apply method. Don’t worry with the way of defining. It is java concept called Anonymous inner class.  “apply” is a method which gives Fluent wait is a new meaning.

Working with FluentWait has two parts. First defining and customizing wait and second part is for the condition which has to be validated, asserting and performing whatever action you want to do.

Functions is an interface which has an abstract method called “apply”.

There is one more important point. We are passing generic type as WebDriver and WebElement for Function.

WebElement foo = wait.until(new Function() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

It is not fixed. This generic type is used for apply method. First generic type will be argument for apply method and another generic type is return type of apply method. For example:

String text= wait.until(new Function() {
	public String apply(WebDriver driver) {
         return driver.findElement(By.id("foo")).getText();
     }
  });

Now you must have got strong point of fluent wait. You can do whatever you want. You can locate web element and assert its properties as well and many more.

Where we should use FluentWait?

  • When you do not find suitable expected wait condition in explicit wait.
  • To handle dynamic web elements.
  • You need to do more then just waiting.
  • When you need to create your own customized wait conditions.

Fluent wait is most confusing topic while implementing. I tried to explain all basic concepts about fluent wait. You will have a better idea when you will start using it. If you have any doubt or you catch any mistake, please comment in comment section.

If you like my posts, please like, comment and share. Do not forget to subscribe for upcoming posts.

11 thoughts on “Part 4: Waits In Selenium: Fluent Wait”

  1. I am having doubt between Explicit and Fluent Wait:
    (1)Since couldn’t figure out difference between explicit and fluent as both are acheiving the same,so Want an example where Explicit won’t work and Fluent is the only way to handle
    (2) Fluent Wait says it ignores exception but it doesn’t as I tried to do the same by making xpath wrong forcefully so webelement couldnt found even then NosuchElement exception thrown ,also not getting why it will throw exception when it is waiting for that element only.
    (3) Fluent wait says it can have polling , but not getting whats the advantage of polling as element which has to visible and identifiable by webdriver that will happen at its own time, then How polling will save time here.

    1. 1. Explicit wait and fluent wait are similar but fluent wait has fixed number of expected conditions and you can not customise it bur in fluent wait you can write your own conditions and do any validation as required. The same i have explained in my posts.

    2. 2. Fluent wait ignores mentioned exceptions till the time out. If you set fluent wait as 15 seconds and set nosuchelementexception to be ignored, webdriver will ignore this exception till time out. It will throw exception if element is not found within wait time.

    3. 3. Webdriver has there own pilling time means in some definite time intervals it will look for element again. You can customise that using explicit or fluent wait.

  2. Explanation is very good. Keep up the good work.

    Suggestion: Please make a video for the same and show the implementation live. It’s helps more for a better understanding.

    Cheers

Leave a Reply

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