Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "wait to activity" using Appium, on begin and during test itself?

I'm starting an already installed app using appium.

After my driver is initialized. How do I make it poll-wait till certain activity is displayed?

I saw only this way to wait for activity when starting up

cap.setCapability("app-wait-activity", "activity-to-wait-for");

Is there any other way? How do I wait to another specific activity when not initializing. Say after a button click?

just sleep x seconds ?

like image 708
Elad Benda2 Avatar asked Mar 01 '15 21:03

Elad Benda2


People also ask

How do I start a new activity in Appium?

This can be done using the startActivity command: driver. startActivity(new Activity("com. example", "ActivityName"));

How do I refresh my screen in Appium?

Using Refresh command- driver. navigate(). refresh(); 2.


4 Answers

Specific activity means some specific element is being displayed. I use the following code to wait until some certain element on the screen:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By
        .xpath("//android.widget.Button[contains(@text, 'Log In')]")));

or:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By
            .xpath("//android.widget.TextView[contains(@resource-id, 'action_bar_title')]")));
like image 186
Alex Avatar answered Oct 10 '22 19:10

Alex


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

If you wish to know in detail how implicit and explicit wait can be used in Appium then visit this TUTORIAL

like image 41
anuja jain Avatar answered Oct 10 '22 18:10

anuja jain


You can use the following code to poll the current activity every second. If you want to reduce polling time you can reduce sleep time to 500 and wait*2 :

public void waitForActivity(String desiredActivity, int wait) throws InterruptedException
{
    int counter = 0;
    do {
        Thread.sleep(1000);
        counter++;
    } while(driver.currentActivity().contains(desiredActivity) && (counter<=wait));

    log("Activity appeared :" + driver.currentActivity(), true);
}
like image 4
shiv Avatar answered Oct 10 '22 19:10

shiv


long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() - startTime < Time_Out)
    if (getDriver().currentActivity().equals(activity))
        break;
like image 3
sameeksha sahib Avatar answered Oct 10 '22 18:10

sameeksha sahib