Fluent wait

A fluent wait is the implementation of the wait interface in which we can configure the timeout and the polling interval dynamically. As well as this, it enables us to add exception exclusions, such as the following example, where we ignore the NoSuchElement exception. Each instance of the fluent wait defines the timeout value and the polling time or interval. The polling interval states the frequency with which to check for the presence of the element under consideration.

Let's take the example HTML we created and try to find out whether we are able to locate the element under consideration using fluent wait, using the following code:

long startTime = 0L;
long endTime = 0L;
WebElement elem = null;
boolean status = false;
System.setProperty("webdriver.chrome.driver","C:\SeleniumWD\src\main\
resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Wait<WebDriver> wdWait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
driver.manage().window().maximize();
driver.navigate().to("C:\Users\Bhagyashree\Desktop\Documents\Timer.html");
wdWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(
"//*[text()='Timer Start']"))).click();
startTime = System.currentTimeMillis();
try {
elem = wdWait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
WebElement text1 = driver.findElement(By
.xpath("//*[@id='demoText']"));
String value = text1.getAttribute("innerHTML");
if (value.equalsIgnoreCase("Silk Test")) {
return text1;
} else {
System.out.println("Text on screen: " + value);
return null;
}
}
});
} catch (NoSuchElementException exception) {
System.out.println(exception.getMessage());
}
if (elem.isDisplayed()) {
System.out.println("Element is displayed");
} else {
System.out.println("Element is not displayed");
}
endTime = System.currentTimeMillis();
timeDiff = endTime - startTime;
timeDiff = timeDiff / 1000;
System.out.println("Start Time: " + startTime);
System.out.println("End Time: " + endTime);
System.out.println("Difference in Time: " + timeDiff);

The output from this program is as follows:
Text on screen: Click on Timer Start
Text on screen: Click on Timer Start
Text on screen: Click on Timer Start
Text on screen: Selenium WebDriver
Text on screen: Selenium WebDriver
Text on screen: Selenium WebDriver
Text on screen: Unified Functional Test
Text on screen: Unified Functional Test
Text on screen: Unified Functional Test
Text on screen: Appium
Text on screen: Appium
Text on screen: Appium
Element is displayed
Start Time: 1535286265688
End Time: 1535286278250
Difference in Time: 12

This output clearly shows that the DOM is polled every second and each piece of text appears three times. The reason for this is that in the JavaScript, the various text items are displayed after a delay of three seconds.

The polling stops when the required text is found and we get the exact time as 12 seconds.

Let's look at two important concepts to bear in mind when using fluent waits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset