How to decrease search time for default element in appium

I just noticed that Appium and Selenium take at least 2 minutes to find an element when the element is not there.

I want to shorten the search time.

Code:

 if(!driver.findElements(By.id(AppConstants.notificationcount)).isEmpty())
{

  // DO SOMETHING

}
else
{

   System.out.println("No Element available");    
}

      

Currently my element is not available, so I want appium to check it and quickly redirect the ELSE part, but it takes a long time, any solution?

+3


source to share


2 answers


Have you checked your implicit latency?
The default is 0, but perhaps you are setting it somewhere> 2 minutes:

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

      

If your implicit timeout is greater than 0 and you are looking for an element with

driver.findElements(...);

      

but your element does NOT exist, then Selenium will wait for FUNCTIONAL time!


Selenium just doesn't wait for at least one element to be found. In this case, it will search the page once and immediately return with a list of found items.



So findElements () is not limited to checking for the existence of an element, but is only suitable for checking for non-existence when you have specified a very low implicit timeout (or the default 0).


If you absolutely need an implicit timeout> 0 for any reason, you can create your own method that handles this as in this solution .


In your case, you can set the implicit timeout to 0 right before your posted code:

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
// then follows your code:
if(!driver.findElements(By.id(AppConstants.notificationcount)).isEmpty())
{

  // DO SOMETHING

}
else
{

   System.out.println("No Element available");    
}

      

If you need an implicit timeout other than 0 elsewhere, just revert it back to the original value after your code snippet.

+1


source


A faster way to check is to keep the items in the list and then check if it is empty

List<WebElement> elements = driver.findElements(By.id("AppConstants.notificationcount"));
 if (elements.isEmpty()) {
    System.out.println("No Element available");
        }else{
          elements.get(0).click();//if present click the element
}

      



Hope this helps you.

+1


source







All Articles