user2256009
user2256009

Reputation: 179

When To Use Implicit wait and where to Use

I am having some confusion regarding Implicit wait method provided by Selenium Webdriver.

  1. When to Use Implicit wait

    a- For Page Load (when Using driver.get) or for Ajax PopUp Load Like let say I am entring something in Edit Box and some Look up or Ajax call is happening.

  2. Where To Use Implicit wait

    Should I use after all the methods wherever Ajax call or Page load happening or only once it is enough (I am just taking the reference from Selenium RC where We can Use Selenium.SetSpeed Method).

Thanks, Arun

Upvotes: 4

Views: 6519

Answers (3)

Vivek Singh
Vivek Singh

Reputation: 3649

For ajax call, I would prefer Explicit wait. But if you can figure out what is the min timestamp of your ajax calls you can provide in implicitlyWait.

Implicitwait is enforced on the driver permanently. So u need not declare again and again. It would affect the driver to wait for a particular time until it throws NoSuchElementException. But if you are using xpaths more, then it would be better you provide greater timeouts in implicitly wait.

Another thing to add, implictlyWait affects only findElement and findElements functions. Other functions are unaffected of it.

Upvotes: 0

Rupesh Shinde
Rupesh Shinde

Reputation: 1956

  • Implicit wait and Explicit wait are related to the driver instances which we are using in our program. First about explicit wait we can use explicit wait for particular condition/Web-element to happen/click and its life depends on that Wait object created.

Example:

         WebDriverWait  explicit_wait_Example = new WebDriverWait(driver, 10);
         explicit_wait_Example.until(ExpectedConditions.elementToBeClickable(By_Locator)).click();

Above is the example of using explicit wait with until which is one of the most efficient and effective ways to use such kind of wait.

  • Second, when it comes to Implicit wait, this wait goes with the Life of the driver instance. Just declare it for one time and you can use it wherever driver instance is invoked.
  • About the question you mentioned, Whether after Ajax call or page load, so I would suggest to check how many instances you have created of driver class. because once you declare implicit wait for one driver instance then you don't have to declare again for that particulate driver instance.
  • Implicit wait is used in the program when you are sure about the time taken by all web-elements on the web-page to load/visible and for certain Web-elements which you find time as one the varying factor in it's loading then you can use explicit wait.

Upvotes: 0

Saifur
Saifur

Reputation: 16201

  1. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.Example is as follows:

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

However, depending on the language implementation varies a little bit. See here more about ExpectedCondition

  1. An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. Below is an implementation of implicit wait:

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    

Both of these definitions are from seleniumhq and most perfect definition out there.

There is a great explanation in toolsQA how and when to use them. Plus a comparison between implicit, explicit and FLUENT waits which are worth taking a look.

Upvotes: 6

Related Questions