1

When using FluentWait in Selenium, it is possible to configure the polling duration of the FluentWait

As far as I know, that is the frequency of checking if the element exists, for example

So, if the timeout is 3 seconds, and the polling duration set is 250 milliseconds, the driver will check for element 12 times before ultimately throwing an exception when it checks for the element the 13th time.

To my understanding, and by that logic, decreasing the polling duration makes the script perform better because it will check for the element more times

Below you can find my implementation of the FluentWait

public static void waitForElementWithDuration(By locator, Duration duration) {
        FluentWait<org.openqa.selenium.WebDriver> wait = new FluentWait<>(Base.driver);
        wait.withTimeout(duration)
            .pollingEvery(ConfigurationManager.DEFAULT_POLLING_DURATION)
            .ignoring(NoSuchElementException.class, ElementClickInterceptedException.class)
            .until(ExpectedConditions.and(
                   ExpectedConditions.presenceOfElementLocated(locator),
                   ExpectedConditions.elementToBeClickable(locator)));
}

I am thinking about making the default polling duration in my configuration manager class shorter (right now it is at 250ms) Would there be any potential drawbacks of me setting the polling duration to 10ms? As the flow I am automating is rather long, and needs to be repeated for many times, implementing a shorter polling duration would make the total time needed to run the tests much shorter

I have been working as a Test Automation Engineer for 1.5 years, so I am still new, and would appreciate some help from more experienced Test Automation Engineers

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1/2 second is already pretty short. Remember that it will take a certain amount of time to check the DOM, for the thrown exception to happen, for the driver to check the exception and ignore it... there is also travel time between browser->driver->your code and back again.. I don't know how long that is, but at a certain point you won't have a speed increase at all, but maybe even a slowdown. – pcalkins Jan 24 '23 at 20:11
  • Thank you very much for your response! Can you expand on (or provide some resources I could analyze and read) regarding the potential slowdown happening? I am aware that there is a time needed to perform the checks and that it is not possible to cut that time, which I am okay with. The framework I am developing is data-driven, so there might be up to 1000 combinations in 1 flow, and about 50 locators per flow, so by my calculations this change (from 250ms to 10ms) would make each run of 1000 combinations 2.8h faster. – Bozidar Kostic Jan 24 '23 at 21:24
  • each wait is only going to take as long as it needs to meet the expected condition. Only if the element is never found would it take the full timeout period. So you might be shaving some milliseconds but it wouldn't be predictable... those are sleeps inside a loop but the actual polling takes a certain amount of time.... I don't have any resources to cite, you should just do your own speed checks/benchmarks to see at what point you have diminishing returns. – pcalkins Jan 24 '23 at 21:49
  • if the main concern is speed you might consider threading browser/driver pairs. Or, if you can get way with it, using something more lightweight like HTMLUnit and thread that. – pcalkins Jan 24 '23 at 22:01
  • Multithreading is unfortunately not an option since this is an E2E integration testing framework, and some of the flows need to be done in order, due to the specific product needs. That is the main reason I am considering other avenues to shave off some time. I appreciate the time spent answering my question and replying to my comments! – Bozidar Kostic Jan 24 '23 at 22:21

1 Answers1

0

FluentWait

As per the documentation, FluentWait is the 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.

Sample Usage:

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

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

The documentation also mention of a possible tradeoff as:

This class makes no thread safety guarantees.


pollingEvery

Again, as per the dococumentation, pollingEvery sets how often the condition should be evaluated. In realtime usecases, the interval may be greater as the cost of actually evaluating a condition function is not factored in. However the default polling interval is DEFAULT_WAIT_DURATION which is defined as 500L:

DEFAULT_SLEEP_TIMEOUT


This usecase

Considering the above mentioned aspects, I don't see any issues reducing the polling duration from it's default value to 10ms or even 5ms which would reduce the framework execution time. The only concern being WebDriver is not thread-safe.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • [Thread Safety](https://www.geeksforgeeks.org/thread-safety-and-how-to-achieve-it-in-java/) is a wide and a vast topic which is out of scope for this question. However I can explain more in the [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Jan 25 '23 at 12:08
  • 1
    I've deleted my previous comments so I don't cause confusion, because the problem I encountered was not because of lowering the polling duration, it works perfectly now! Thank you for your help! – Bozidar Kostic Jan 25 '23 at 13:08