WebDriver Wait Commands
RSSS

Wait commands in WebDriver

Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() comamnds

After few searches and digging into the WebDriver Java doc, I managed to design a mindmap of the different WebDriver commands available

webdriver mindmap

WebDriver.manage().timeouts()

implicitlyWait

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));


The ImplicitWait will tell the webDriver to poll the DOM for a certain duration when trying to find the element, this will be useful when certain elements on the webpage will not be available immediately and needs some time to load.
By default it ill take the value to 0, for the life of the WebDriver object instance through out the test script.

pageLoadTimeout


driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

setScriptTimeout

driver.manage().timeouts().setScriptTimeout(100,SECONDS);

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.


Support.ui

FluentWait


// 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(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

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

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.

ExpectedConditions


WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));

Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.
Examples : Would include determining if a web page has loaded or that an element is visible.
Note that it is expected that ExpectedConditions are idempotent. They will be called in a loop by the WebDriverWait and any modification of the state of the application under test may have unexpected side-effects.


WebDriverWait will be used as we used in the Expected conditions code snippet as above.


sleeper is something same as the Thread.sleep() method, but this with an Abstraction around the thread.sleep() for better testability

21 thoughts on “WebDriver Wait Commands

  1. January 29, 2013 at 11:00 am

    +1

    1. kiran
      February 7, 2013 at 12:38 pm

      nice information thank u

      1. February 11, 2013 at 8:09 pm

        I’ve reposted this in my blog :)

        1. February 11, 2013 at 8:13 pm
          1. February 12, 2013 at 10:23 am

            Yep!

          2. February 12, 2013 at 12:00 pm

            I do saw them earlier, as there was few traffic referred from your blog so I just visited it some time ago.
            Thanks for mentioning it.

  2. Jason
    February 22, 2013 at 7:59 am

    I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again!

  3. jitendra
    April 18, 2013 at 12:58 pm

    Awesome post

  4. May 21, 2013 at 8:30 pm

    Hi!
    I would like to recommend you Selenide – a library providing much easier way to wait for events.

    With Selenide you just need to write one line:
    $(“#myDynamicElement”).shouldBe(visible);

    More examples in Wiki: https://github.com/codeborne/selenide/wiki/Selenide-vs-Selenium#5-ajax-support-waiting-for-some-event-to-happen

  5. Siva
    June 24, 2013 at 12:45 pm

    How to wait till a new window is displayed.

    My scenario is like this

    I have 3 IE windows opened(not in tabs).
    In the third window I have a submit button.
    If I click that submit button a new window i.e 4th one opens.

    Right now I am using Thread.sleep() to wait after clicking submit button which is not a good practice.

    Is there a way to wait using WebDriver wait.

    Thanks in adavance

  6. Pandarinath Siddineni
    July 17, 2013 at 5:34 am

    Great article, thanks for posting it.

  7. shankar sudini
    September 25, 2013 at 1:47 pm

    Great nice ……..

  8. Maha
    October 28, 2013 at 7:29 am

    The above content was very useful..But it is really includes the human knowledge to use the appropriate command according to situation…

    Thanks!

    1. October 28, 2013 at 3:45 pm

      Absolutely….

  9. seshank
    December 7, 2013 at 8:20 am

    I’m using thread.sleep(), in my automation tests. but most of my colleagues say’s that this shouldn’t be used and it is not a good practise. which one is suggestable either implicit waits or explicit wait.can any one help me out on this.

  10. Pranjal R Nigam
    December 20, 2013 at 10:14 am

    Do you have the commands for c# as well.

    Please let me know if you have it.

    Thanks

    1. December 21, 2013 at 6:05 am

      Nope, But it should be pretty much same.

      If you got an idea after seeing this, pls implement the same in your language.

      Dont expect any ready code..

  11. Amit Kumar
    January 23, 2014 at 2:02 pm

    Nice fruitful info, it help me a lot to understand WAIT() in selenium.

    1. January 23, 2014 at 5:22 pm

      I am glad to hear that. Keep learning..!!

  12. Sharad
    February 13, 2014 at 8:43 pm

    wait was working for me until I have to integrate my code with Maven and then ExpectedConditions class doesn’t appear under UI in the selenium-java jar 2.2.4 version.

    I only see ExpectedCondition class which is not same as ExpectedConditions.

    Any thoughts? Am I not including nay jars, or any workaournds to use the same code?

    Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *