18

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 ?

Emna Ayadi
  • 2,430
  • 8
  • 37
  • 77
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

7 Answers7

13

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')]")));
Alex
  • 362
  • 1
  • 5
  • 14
4
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

Nas
  • 2,158
  • 1
  • 21
  • 38
anuja jain
  • 1,367
  • 13
  • 19
4

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);
}
George Pantazes
  • 1,039
  • 3
  • 12
  • 26
shiv
  • 497
  • 3
  • 17
3

I would suggest you to use WebDriverWait. Thread.sleep() is not a good way to use in your test scripts

Gaurav
  • 1,332
  • 11
  • 22
3
long startTime = System.currentTimeMillis();
while(System.currentTimeMillis() - startTime < Time_Out)
    if (getDriver().currentActivity().equals(activity))
        break;
1

Also you could make use of the following:

getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

or just:

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

or something like the following:

Thread.sleep(5000);
Alex
  • 362
  • 1
  • 5
  • 14
  • Highly recommend avoiding sleeps as this will make your test wait for 5 seconds, even if it doesn't need to. This can add up fast and make your tests very slow, when using an explicit wait instead could make your test more stable and much speedier! – Scott Merritt Feb 11 '19 at 18:33
1

It can be done by different ways using element. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this. ExpectedConditions class provide some set of predefine conditions to wait elements as:

  1. elementSelectionStateToBe: an element state is selection.
  2. elementToBeClickable: an element is present and clickable.
  3. elementToBeSelected: element is selected
  4. frameToBeAvailableAndSwitchToIt: frame is available and frame
  5. selected. invisibilityOfElementLocated: an element is invisible
  6. presenceOfAllElementsLocatedBy: present element located by.
  7. refreshed: wait for a particular condition when page refresh.
  8. textToBePresentInElement: text present on particular an element
  9. textToBePresentInElementValue: and element value present for a
    particular element. and many more You can learn more ways to implement implicit and explicit wait by going through this url: http://roadtoautomation.blogspot.in/2013/10/webdriver-implicit-and-explicit-wait.html

Hope it helps...