Friday, May 9, 2014

Sychronizing the WebDriver (Implicit wait and Explicitly wait)

Implicit wait-


Syntax- driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Here it will wait for 10sec if while execution driver did not find the element in the page immediately. This code will attach with each and every line of the script automatically. It is not required to write everytime. Just write it once after opening the browser. If element found before assigned value, it will continue with the script, it will not wait for completely 10sec.

Explicit wait-
Syntax- Thread.sleep or use WebDriverWait wait = new WebDriverWait(driver, 10);//here 10 is 10sec
ex- wait.until(ExpectedConditions.elementToBeClickable(By.id("login")));
In this case wherever we want to wait we have to write the explicit wait command and it will wait for that particular time whether element found or not.
 

4 comments:

  1. Hi sanjay,
    I could not understand in explicit wait "if while execution driver did not find the element in the page immediately. This code will attach with each and every line of the script automatically". can you please tell me with some example.

    Thanks.

    ReplyDelete
    Replies
    1. This "if while execution driver did not find the element in the page immediately. This code will attach with each and every line of the script automatically" is for implicit wait not for explicit wait.
      In all examples here in my blog i have used this. Please refer any of them.

      http://selenium-makeiteasy.blogspot.in/2014/06/drag-and-drop-or-how-to-use-draganddrop.html

      Delete
    2. Thanks Sanjay for fast reply.

      Plz rectify me if i am wrong,
      If the implicit wait(Ex:river.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);) fails then the
      this piece of code will add to each and every line of code, that means after every line of execution the same implicit wait will also execute/perform.

      Delete
    3. Your statement is not very clear.
      Implicit wait command will execute only when element is not found in default wait time from selenium end.And if element not found in default time then implicit wait command will execute and if the element found in before implicit wait time say implicit wait time is 10sec and element found in 5sec then it will continue with the script , it will not wait for another 5mins. And if the element not found in 10sec also then it will throw exception element not found. Thanks!!

      Delete