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.


10 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
  4. An implicit wait tells WebDriver or Selenium Scripts to poll the DOM for a certain amount of time when Selenium Scripts trying to find an Object/Element or Object/Elements if they are not visible or Interactable.
    xplicit wait tell the WebDriver to wait for certain time on basis of certain Expected conditions before throwing an “ElementNotVisibleException” exception ,Explicit wait is specific wait applied only for specified elements. Explicit wait have better flexibility then Implicit wait.
    For Detail With Best Example Link :

    http://www.qamantra.com/2018/01/how-to-use-explicit-and-implicit-wait.html

    ReplyDelete
  5. Hi,

    I am facing an issue with the wait.until(ExpectedConditions.elementToBeClickable(by));

    My function is defined as

    public static MobileElement waitForMobileElementToBeClickable(By by) {
    try {
    return (MobileElement) wait.until(ExpectedConditions.elementToBeClickable(by));
    }
    catch(NoSuchElementException nse) {
    System.out.println("No such element found !");
    return null;
    }
    catch(TimeoutException toe) {
    System.out.println("Timeout exception cought !");
    return null;
    }
    }


    The error I am getting is this

    WARNING: WebDriverException thrown by findElement(By.name: login settings)
    [testng] org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {ELEMENT=CAE0CBA1-8A14-4BBA-86BA-EBAF83895B41}

    ReplyDelete
  6. What string should be used in place of "about me"

    ReplyDelete
  7. Thanks for the information. Very helpful. How do I follow more of your work?

    ReplyDelete