Fluent Wait in Selenium - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Monday 24 February 2020

Fluent Wait in Selenium

This kind of wait s generally used when waiting time for Element is not fixed.

For example Some WebElement appears in 1 second but sometime that Element appears in 10 seconds, So there is no fixed wait time for that WebElement.
 In that case it is better to use fluent wait, as this will try to find element again and again until it find it or until the final timer runs out.

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.


Fluent waits are also called smart waits also. They are called smart primarily because they don’t wait for the max time out, specified in the .withTimeout(5000, TimeUnit.MILLISECONDS). Instead, it waits for the time till the condition specified in .until(YourCondition) method becomes true.

Example

package mypackage;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;

import com.google.common.base.Function;

public class myclass {

WebDriver driver = new FirefoxDriver();

public WebElement fluentWait(final By locator) {
System.out.println("In Fluent Wait");
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {

public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return foo;
};

public static void main(String[] args) throws IOException,
InterruptedException {

myclass obj = new myclass();
String baseUrl = "https://www.ebay.in/cat/mobiles/priceRange/0-10000%3B10001-18000";
obj.driver.get(baseUrl);

Thread.sleep(8000);
obj.driver.findElement(By.xpath(".//*[@id='seemore']")).click();
//With Fluent Wait
WebElement element = obj.fluentWait(By.xpath(".//*[@id='itmCont']/div[29]/div[2]/a"));
element.click();

//Without Fluent Wait
//WebElement element=obj.driver.findElement(By.xpath(".//*[@id='itmCont']/div[29]/div[2]/a"));
//element.click();
}
}


If you pay close attention to the .until() method you will realize that is an overloaded function which can take two types of parameters.

A Function()
A Predicate()

A Function here is a generic interface which asks you to implement the following methods
apply(F from)
– equals(Object obj)

Implementation of equals can be ignored and it will be picked from the base implementation.

First Way Of Implementing it  (Boolean Value)

Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()
{

public Boolean apply(WebDriver arg0) {
return null;
}

};



Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>()
{
public Boolean apply(WebDriver arg0) {
WebElement element = arg0.findElement(By.id("colorVar"));
String color = element.getAttribute("color");

if(color.equals("red"))
{
return true;
}
return false;
}
};

Second Way Of Implementing Using WebElement

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {

public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});


What is a Predicate?
A predicate is similar to a function but it always returns a Boolean expression. You can define a predicate like this

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
 wait.pollingEvery(250,  TimeUnit.MILLISECONDS);
 wait.withTimeout(2, TimeUnit.MINUTES);
 wait.ignoring(NoSuchElementException.class); //make sure that this exception is ignored

 Predicate<WebDriver> predicate = new Predicate<WebDriver>()
 {

 public boolean apply(WebDriver arg0) {
 WebElement element = arg0.findElement(By.id("colorVar"));
 String color = element.getAttribute("color");
 System.out.println("The color if the button is " + color);
 if(color.equals("red"))
 {
 return true;
 }
 return false;
 }
 };
 wait.until(predicate);

No comments:

Post a Comment