Waits in WebDriver (Implicit,Explicit and ThreadSleep) - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Monday 7 October 2013

Waits in WebDriver (Implicit,Explicit and ThreadSleep)



Implicit Wait - It's global setting applicable for all elements and if element appear before specified time than script will start executing otherwise script will throw NoSuchElementException.


WebDriver driver = new FirefoxDriver();  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


Implicit Wait: During Implicit wait if the Web Driver cannot find it immediately because of its availability, the WebDriver will wait for mentioned time and it will not try to find the element again during the specified time period. Once the specified time is over, it will try to search the element once again the last time before throwing exception. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.


NOTE:  Implicit wait time is applied to all elements in your script


Explicit wait

WebDriverWait wait=new WebDriverWait(driver,50);

wait.until(ExpectedConditions.textToBePresentInElement(By.id("ID OF TEXT BOX")));

Using explicit waits you are basically telling  WebDriver at the max it is to wait for X units of time before it gives up.

Explicit wait time is applied only for particular specified element.

In Explicit you can configure, how frequently (instead of 2 seconds) you want to check condition
Explicit Wait: There can be instance when a particular element takes more than a minute to load. In that case you definitely not like to set a huge time to Implicit wait, as if you do this your browser will going to wait for the same time for every element.

To avoid that situation you can simply put a separate time on the required element only. By following this your browser implicit wait time would be short for every element and it would be large for specific element.

ThreadSleep

Using Thread.sleep(2000); is an unconditional wait. If your test loads faster you will still have to wait. So in principle using implicitlyWait is the better solution.

2 comments:

  1. Keep on posting this type of snippets.. So we can regularly check and follow.

    ReplyDelete