Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UiScrollable not work in Android uiautomator

Does anyone try the android UITesting framework UIAutomator? When I use the class UiScrollabl" to find some objects in a scrollable object, it can't find the object if the the length of the scrollable object is too long(need to swipe twice to find it), like "Developer options" in the "Settings" app. Does anyone have the same issue?

like image 945
byndhorizon Avatar asked Apr 07 '13 04:04

byndhorizon


People also ask

How do you use UiScrollable in Appium?

You can use UiScrollable swipe to search elements in a list or search elements outside of the screen like input field, text or button. ScrollIntoView has UiSelector as a search criteria input that allows it to find elements by text or id. Prerequisites: Appium desktop client.

What is UiScrollable?

UiScrollable is a UiCollection and provides support for searching for items in scrollable layout elements. This class can be used with horizontally or vertically scrollable controls.


2 Answers

I've fixed it by overriding a UiScrollable class.

public class UiScrollable extends com.android.uiautomator.core.UiScrollable {

    public UiScrollable(UiSelector container) {
        super(container);
    }

    @Override
    public boolean scrollIntoView(UiSelector selector) throws UiObjectNotFoundException {
        if (exists(getSelector().childSelector(selector))) {
            return (true);
        } else {
            System.out.println("It doesn't exist on this page");
            // we will need to reset the search from the beginning to start search
            scrollToBeginning(getMaxSearchSwipes());
            if (exists(getSelector().childSelector(selector))) {
                return (true);
            }
            for (int x = 0; x < getMaxSearchSwipes(); x++) {
                System.out.println("I'm going forward a page: " + x);
                if(!scrollForward() && x!=0) { // x!=0 is the hack
                    return false;
                }

                if(exists(getSelector().childSelector(selector))) {
                    return true;
                }
            }
        }
        return false;
    }    

}

I've copied the source from: UiScrollable.java (which may be outdated at some point, beware) and simply changed the if(!scrollForward() && x!=0) line.

From my observations, in the case of the example code on Google's ui testing page that scrolls the apps screen for the settings app, the scrollForwards() method fails on the first attempt. God knows why.

The above simply says if it fails on the first scroll, carry on regardless. If it fails to scroll on the second scroll, then it does in fact return a failure.

like image 120
mmm111mmm Avatar answered Nov 02 '22 12:11

mmm111mmm


To find and any element in a view where scrolling is required I used the below mentioned method and it seems to work fine.

public void searchForText(String searchText)
            throws UiObjectNotFoundException {
        UiScrollable textScroll = null;
        UiObject text = null;
        if (searchText != null) {
            textScroll = new UiScrollable(new UiSelector().scrollable(true));
            textScroll.scrollIntoView(new UiSelector().text(searchText));
            text = new UiObject(new UiSelector().text(searchText));
            text.click();
        }
    }
like image 43
Rajesh Avatar answered Nov 02 '22 12:11

Rajesh