Friday, 18 November 2016

Implicit & Explicit Wait with Appium

As we have all faced that we need to wait for loading in mobile application.Mostly we use Thread.sleep() but it is not advised to use this as we can predict the exact time to wait. Appium provides Implicit wait and Explicit wait feature. We can use implicit wait when we know the exact time to wait and Explicit wait when we don't know the exact time to wait but we need to wait until any mobile element is visible.
 

In this post we will see two types of Waits which are widely used in Appium-

Video -  

 

1.Implicit Wait.
2.Explicit Wait.

Implicit & Explicit Wait with Appium
Wait with Appium

1.Implicit Wait

Implicit Wait means informing appium web driver to wait for specific amount of time and if the web element is not visible after waiting for that specific point then throw "No such Element found Exception". 
Check out the Example given below to understand how to use Implicit wait in Appium Test Case.In below Test case we will add a wait for 30 seconds after opening the browser so that that the page is gets loaded completely.As we implicitly specify the time and Time Unit this is called as implicit wait.
method implicitlywait() accepts 2 parameters first one is time value and second one is time unit like days,hours,minutes,seconds,milliseconds etc.

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

2. Explicit Wait -

In Explicit wait along with wait time we also provide the wait condition. It will wait till the condition or the maximum wait time provided before throwing the Exception"ElementNotVisibleException".
We can say that Explicit wait is intelligent wait as it waits dynamically for specified condition. Check out the below test case example for using Appium Explicit wait.First We need to instantiate WebDriverWait  wait object with Web Driver Reference and time frame in seconds.
Then we can use until() method for passing Expected Wait Condition and use it in our test case for waiting until the element is visible on the screen.Here we are using  visibilityofElementLocated() condition.To find out about other conditions type ExpectedConditions in eclipse IDE and press cltr+space bar and you will get the list of conditions which you can use in with Explicit Wait.


WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));      


With this you can get rid of test case failure due to wait issues.

I hope you find this post useful. fallow me on facebook or google plus to get latest post updates. Happy Testing.


5 comments:

  1. Thanks for your all the posts on Appium :) Much appreciated

    ReplyDelete
    Replies
    1. Thanks for reading .many more posts are coming soon on Appium. Stay connected via Facebook and Google+

      Delete
  2. Can both of these Implicit wait and explicit wait be used for native apps too right? and can they be used together? when I already set implicit wait at @beforeClass, can I use explicit wait in any other @test ? then which was has the priority?

    ReplyDelete
    Replies
    1. Answer to both of your question is "YES" you can use it for native apps and you can use both types of wait together.

      Thanks for visiting ,
      Anuja

      Delete
  3. Explicitly wait is not working for me for appium

    ReplyDelete