0

I have checked Selenium documentation and I can see that there are three different WebDriverWait constructors including this (Java):

public WebDriverWait(WebDriver driver,
                 java.time.Clock clock,
                 Sleeper sleeper,
                 long timeOutInSeconds,
                 long sleepTimeOut)

What is this sleeper for? I have checked and this his how it looks:

public interface Sleeper {
    Sleeper SYSTEM_SLEEPER = duration -> Thread.sleep(duration.toMillis());
    /**
    * Sleeps for the specified duration of time.
    *
    * @param duration How long to sleep.
    * @throws InterruptedException If the thread is interrupted while sleeping.
    */
    void sleep(Duration duration) throws InterruptedException;
}

I was digging for a while but still can't figure out what this parameter does. We already have timeOutInSeconds and sleepTimeOut to handle polling for a WebElement. Can someone help and tell me what is sleeper for?

1

Sleeper is an interface that allows you to implement your own way how to make your code sleep. The default way is to call Thread.sleep(duration) however you might want your sleep method to perform some extra actions (probably prepare your code for sleeping or whatever). So that you can implement your own one which will make your sleeping different from default one.

This is the default implementation:

enter image description here

This is what is happening when you do not use your own sleeper:

enter image description here

3
  • Sooo you're saying this sleeper is used as a mechanism in each sleep method used then by WebDriverWait object?
    – foxy
    Dec 12 '18 at 9:45
  • Yup. Even if you do not pass some specific Sleeper to waiter there is default implementation that is used.
    – Alexey R.
    Dec 12 '18 at 10:03
  • @Ela see my update
    – Alexey R.
    Dec 12 '18 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.