omk
omk

Reputation: 43

How to use built-in ExpectedConditions with FluentWait?

In Selenium (Java), I want to use ExpectedConditions with FluentWait. I am trying out following code which is not working. It is not waiting for the element to appear in the DOM.

Could someone please help here ?

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(10, TimeUnit.SECONDS)
                    .pollingEvery(1, TimeUnit.SECONDS);

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));

Note: I have tried this with WebDriverWait and it is working file. I am trying with FluentWait since I want to control polling timeout.

Upvotes: 1

Views: 2735

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193208

A bit of background :

Fluent Wait

Fluent Wait is the implementation of the Wait interface which an user can configure its timeout and polling interval on the fly. A FluentWait instance defines the maximum amount of time to wait for a condition along with the frequency with which to check the condition. The user can also configure the wait to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

WebDriverWait

WebDriverWait is the tailor-made version of FluentWait that uses WebDriver instances.

You can find a detailed discussion on WebDriverWait and FluentWait in both of these QA Implicit vs Explicit vs Fluent Wait and Differences between impilicit, explicit and fluentwait.

ExpectedConditions

ExpectedConditions are the tailor made canned conditions which are generally useful within webdriver tests.


As per your question, as you are trying with FluentWait since you want to control polling timeout you can still achieve the same through WebDriverWait as follows :

  • WebDriverWait have 3 Constructors and one of them is :

    WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
    
  • Details :

    public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
    
    This wait will ignore instances of NotFoundException that are encountered by default in the `until` condition, and immediately propagate all others. You can also add more to the ignore list by calling ignoring(exceptions to add).
    
    Parameters:
    driver - The WebDriver instance to pass to the expected conditions
    timeOutInSeconds - The timeout in seconds when an expectation is called
    sleepInMillis - The duration in milliseconds to sleep between polls (polling interval).
    

Solution :

You can use the above mentioned Constructor of WebDriverWait and still can control the polling interval.

Note : To keep your program logic simple and understandable use WebDriverWait instead of Fluent Wait untill and unless absolute necessary.

Trivia :

For further understanding of Fluent Wait you can follow the discussion Selenium Webdriver 3.0.1-[Eclipse-Java-Chrome]: Selenium showing error for FluentWait Class

Upvotes: 2

lokeshchitturi
lokeshchitturi

Reputation: 21

Yes what NarendraR said is correct. When you have created object for FluentWait used the same object for write ExpectedConditions.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
wait.unitl(ExpectedConditions.presenceOfElementLocated(By.id("mybutton")));

Upvotes: 0

Related Questions