Search code examples
androidunit-testingtestingui-automationandroid-uiautomator

Android UIautomator swiping ViewAnimator


I'm testing my app but have a problem with the DatePicker. All I need is to swipe down until another month appears (searching for a specific date).

Screenshot

The structure is a bit tricky but I made it work using

new UiScrollable(new UiSelector().className(android.widget.ViewAnimator.class.getName())).scrollTextIntoView("November")

Problem is, even though the view is scrolling, it is

1) scrolling in the wrong direction (up)

2) not stopping even though a November has already passed.(even November 2017 or November 2015...)

How can I create this condition. After all the views are named in a unique way so checking the structure would be possible finding "01 November 2016".

Structure


Solution

  • Okay, looks like I found a solution that works more or less fine:

    At first I tried to use the sub conditions since UiSelector might have a child definition. But that proved hard since inside of the ViewAnimator is a (single) child object ListView containing the "calendar month" view. So finding a childview in there with the description "15 December..." was tricky.

    The new solution does this.

    1. while (tries < MAX_TRIES)
    2. if element with description "15 December ..." exists -> click and exit loop
    3. scroll down 1 element (next month) -> tries++

    I repeat this until the element is found (and clicked) or the maximum scroll tries are exhausted. After scrolling I let the device wait for 1 second. This is useful since swiping is called asynchronously and would continue swiping for a moment while processing the click. This did not show as a problem (since the view is already clicked) but still might be confusing.

    I hope this helps! Feel free to post a better solution if you find one.

    int tries = 0;
    while (tries < MAX_TRIES) {
       UiObject2 dateField = mDevice.findObject(
               By.descStartsWith(SEARCH_DATE));
           if (dateField != null) {
               dateField.click();
               break;
           } else {
               tries ++;
               new UiScrollable(new UiSelector().
                       className(android.widget.ViewAnimator.class.getName())).
                       scrollToEnd(1);
               mDevice.wait(Until.findObject(By.descStartsWith(SEARCH_DATE)), 1000);
           }
    }