2

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;
}
});
2

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm entirely new to this.... .withTimeout , .pollingEvery , .ignoring are java commands? .... can you explain with a practical scenario?....with the code....
They are java methods defined in org.openqa.selenium.support.ui.WebDriverWait. We use FluentWait where the element gets loaded sometimes in 1sec and sometime in 30sec, so we give the total time in .withTimeout(30, TimeUnit.Seconds) for how much the driver should be waiting for the element to be loaded, in this case for 30sec. .pollingEvery(5, TimeUnit.SECONDS) is for looking for the element to be loaded every 5 seconds. So in case if the element is loaded let's say in 7sec so you dont have to wait for all the 30sec to action the element.
2

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.

4 Comments

That is the link I'm using it as a reference.... They didn't explain the code that much
I think you haven't taken a look at the first link properly. That is one of the most elaborate explanation. Can you be more specific in what you were not able to understand?
wait.until(new com.google.common.base.Function<WebDriver, Boolean>() { @Override public Boolean apply(WebDriver driver) { return null; } please explain this part.
Update the answer. Hope it helps you
0

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);

}

Comments

0

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)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.