I am writing a UI test using UI automator and have run into an issue where I am attempting to scroll to the beginning of a NumberPicker, however when using either ScrollToBeginning or FlingToBeginning with varying numbers of steps, the NumberPicker only scrolls up two elements and then stops scrolling. Is there any reason that this behavior is happening consistently and is there any way to either fix or work around it?
Yes, This is a known Bug
of UI Automator
and below is the workaround for this:
Snippet from above article:
Bug filed: https://groups.google.com/forum/?fromgroups=#!topic/adt-dev/TjeewtpNWf8
Workaround, using a helper method, below:
/**
* Launches an app by it's name.
*
* @param nameOfAppToLaunch the localized name, an exact match is required to launch it.
*/
protected static void launchAppCalled(String nameOfAppToLaunch) throws UiObjectNotFoundException {
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
// Set the swiping mode to horizontal (the default is vertical)
appViews.setAsHorizontalList();
appViews.scrollToBeginning(10); // Otherwise the Apps may be on a later page of apps.
int maxSearchSwipes = appViews.getMaxSearchSwipes();
UiSelector selector;
selector = new UiSelector().className(android.widget.TextView.class.getName());
UiObject appToLaunch;
// The following loop is to workaround a bug in Android 4.2.2 which
// fails to scroll more than once into view.
for (int i = 0; i < maxSearchSwipes; i++) {
try {
appToLaunch = appViews.getChildByText(selector, nameOfAppToLaunch);
if (appToLaunch != null) {
// Create a UiSelector to find the Settings app and simulate
// a user click to launch the app.
appToLaunch.clickAndWaitForNewWindow();
break;
}
} catch (UiObjectNotFoundException e) {
System.out.println("Did not find match for " + e.getLocalizedMessage());
}
for (int j = 0; j < i; j++) {
appViews.scrollForward();
System.out.println("scrolling forward 1 page of apps.");
}
}
}