Thursday, January 16, 2014

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);

In this case, User is telling WebDriver that it should wait 10 seconds in case of specified element is not available on the UI i.e (DOM).
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

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.

No comments:

Post a Comment