UIAutomator ScrollToBeginning不会一直滚动到开头

时间:2014-07-29 15:17:48

标签: android uiautomator android-uiautomator

我正在使用UI自动机编写UI测试并遇到了一个问题,我试图滚动到NumberPicker的开头,但是当使用ScrollToBeginning或FlingToBeginning执行不同数量的步骤时,NumberPicker只会向上滚动两个元素,然后停止滚动。有没有理由这种行为一直在发生,有没有办法解决或解决它?

1 个答案:

答案 0 :(得分:1)

是的,这是Bug已知的UI Automator,以下是解决方法:

Link

以上文章的片段:

提交的错误: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.");
          }
      }
}