UIAutomator ScrollToBeginning 不会一直滚动到开头

Jac*_*abe 5 android android-uiautomator

我正在使用 UI automator 编写 UI 测试,并遇到了一个问题,我试图滚动到 NumberPicker 的开头,但是当使用具有不同步数的 ScrollToBeginning 或 FlingToBeginning 时,NumberPicker 仅向上滚动两个元素,然后停止滚动。这种行为持续发生是否有任何原因,是否有任何方法可以修复或解决它?

Anv*_*thy 2

是的,这是已知BugUI Automator,下面是解决方法:

关联

上述文章的片段:

错误提交:https ://groups.google.com/forum/?fromgroups=#!topic/adt-dev/TjeewtpNWf8

解决方法,使用辅助方法,如下:

/**
 * 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.");
          }
      }
}
Run Code Online (Sandbox Code Playgroud)