How to control time in appium when find element

I am using Appium and selenium web driver for Android application automation testing , Since last 3 weeks I am noticing that when I try to find elements with array , Appium find it continue at least for 2 minutes always and I want to make that time like 30 seconds.

My Code is here :

List uploaderror = driver.findElements(By.xpath(AppConstants.uploaderror));

if(uploaderror.size()<=0)
{

       //Do Something

}

In above code I am trying to find multiple elements and depends on element size I apply further action. Issue is appium takes very long time to find element , I want to control that time. I have tried all types of waits but no success.

Please let me know if anyone already faced this and resolved.

We had a lot of problems getting the timing for searching for elements under control. We decided to use the Selenium wait object to limit the amount of time that we search. To wit,

wait = Selenium::WebDriver::Wait.new(timeout: 30, interval: 1)
begin
wait.until
# findElement…
end
rescue Selenium::WebDriver::Error::TimeOutError

report failure

end

1 Like

There are implicit and explicit waits. Explicit waits allow you to define maximum wait time and as the condition (e.g., visibility of element) is met the wait will stop and next statement will be executed.

Here is the sample method in java:

public void waitForScreenToLoad(AppiumDriver lDriver, WebElement element, int seconds){

          WebDriverWait wait = new WebDriverWait(lDriver,seconds);
          wait.until(ExpectedConditions.visibilityOf(element));

}

4 Likes

Okay , I followed this and now calling function like (waitForElementToLoad(driver, count ,60) , but now how can I integrate it with my condition? can you please explain in short that? My code is there in question for reference.

What is ‘count’ that you are passing in the function as parameter? is it WebElement?

Hey,

It is solved… thanks a lot for support.

Glad. Happy automation

1 Like

Hi . refer this tutorial to work with implicit and explicit wait with Appium
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“about_me”)));

4 Likes

If you are using PageFactory model with Appium, then you can add implicit wait in InitElements() method as below -

PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS), this);

I have tried this with Appium 1.6 and it works fine.

@anish10110 can you please clarify if driver is of type WebDriver or AppiumDriver as in both case this is not working with latest java client

Thanks,
Vikram

For the benefit of those who come to this post looking for answer like me, please find below info how I got it working

1> Sample code

2> Refer to post for better info

3> Found an issue wrt this

Regards,
Vikram

Thanks …worked for me

It worked with latest jars:

Appium 1.7.1
Java-client 5.0.4
Selenium-java 3.7.1

AppiumDriver androidDriver will work

I am using the WebDriverWait ExpectedConditions.presenseOfElement. But I find that if the element is not there the driver fails and the tests stops at that point. How can I wait for presence but if it doesn’t exists it should not fail but go to the next line of code.

You don’t say what language, but all have some kind of try/catch block for error handling.

Here’s an example in Java:

https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html