Reputation: 67
Can somebody please explain how this "fluentwait" works and the structure of it?
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new com.google.common.base.Function<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return null;
}
});
Upvotes: 2
Views: 8369
Reputation: 83
Just to add some changes, since withTimeout(int, TimeUnit)
and pollingEvery(int, TimeUnit)
are deprecated:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(waitTimeout))
.pollingEvery(Duration.ofSeconds(pollingEvery))
.ignoring(NoSuchElementException.class);
Upvotes: 0
Reputation: 8676
Rather old question but nevertheless:
Wait<WebDriver> wait = new FluentWait<>(driver) // <-- Setting object of WebDriver type
.withTimeout(60, TimeUnit.SECONDS) // <-- configuring waiter properties
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wait.until(new com.google.common.base.Function<WebDriver, Boolean>() { // <-- configuring conditional function
@Override
public Boolean apply(WebDriver driver) { // <-- it works with object of WebDriver type because we specified that in the very first line of the snippet
return null;
}
});
This Wait<WebDriver> wait = new FluentWait<>(driver)
means that you create a waiter that will use WebDriver
object that will be using in conditional function passed to its until
method.
Here are some more details on how FluentWait works and how to use it with any type of event (not only Selenium events)
Upvotes: 0
Reputation: 69
I have used fluent wait in my one of the program where i was clicking on a button, then a particular webelement will appear. The webelement can only appear after refreshing the page and not sure when the webelement will appear. So, in the below code, i'm using Fluent wait. Here pollingEvery means my code will check the presence of element in every 10 seconds upto 360 sec (6 mins). It may happens that the element appears at 100 sec, then after 100 sec, the code will move to next execution step. For more details can refer - https://configureselenium.blogspot.com/2019/12/what-is-fluent-wait-in-selenium.html
Wait wait = new FluentWait(driver).withTimeout(Duration.ofSeconds(360)) .pollingEvery(Duration.ofSeconds(10)).ignoring(NoSuchElementException.class);
WebElement LinkAlert= wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
driver.navigate().refresh();
System.out.println("Page is refreshed");
WebElement LinkAlert= driver
.findElement(By.xpath("//span[contains(text(),'alert-visibility')]"));
return LinkAlert;
}
});
Alert1 = LinkAlert.isDisplayed();
Assert.assertTrue(Alert1);
}
Upvotes: 0
Reputation: 871
What you want to understand is explained here in much details
http://toolsqa.com/selenium-webdriver/advance-webdriver-waits/
and
http://toolsqa.com/selenium-webdriver/wait-commands/
More explanation:
com.google.common.base.Function is a generic interface. You have learn Java generics to understand what generic interfaces/classes are from here
From you code, when we say com.google.common.base.Function it means that the implementation of this function will accept WebDriver as input argument and return a Boolean.
WebDriverWait.Until method will keep on calling your Function.apply method again and again till you apply method return true. Once it returns true WebDriverWait will assume that your wait end logic has been successful and it will s top waiting.
At present your wait function doesn't do anything, it just waits for the timeout to happen. This must be throwing a TimeOutException at the end.
What ever wait logic you have to write should be written inside the .apply method. Return true or false for boolean or non null and null values for reference types once you condition is met or not met.
Upvotes: 2
Reputation: 317
FluentWait instance defines the maximum amount of time to wait for a condition. Following statement in your code defines the wait time.
.withTimeout(60, SECONDS)
As well as the frequency with which to check the condition. Following defines the frequency
.pollingEvery(5, TimeUnit.SECONDS)
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. Following is for ignoring "NoSuchElementExceptions"
.ignoring(NoSuchElementException.class);
When to use FluentWait: When you try to test the presence of an element that may appear after every x seconds/minutes
Upvotes: 2