UiScrollable not work in Android uiautomator

byndhorizon picture byndhorizon · Apr 7, 2013 · Viewed 9.6k times · Source

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?

Answer

mmm111mmm picture mmm111mmm · Dec 4, 2013

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.