UIScrollable getChildByText 在放弃之前滚动不够

UIScrollable getChildByText doesnt scroll enough before it gives up

我正在使用 uiautomator 工具为我的应用编写一些自动化测试。

这是有问题的代码:

UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
appViews.setAsHorizontalList(); // works on API 17+
UiObject settingsApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Settings");
        settingsApp.clickAndWaitForNewWindow();

当我在 KitKat phone 上 运行 时,phone 进入主屏幕,然后单击 "Apps" 然后选择 "Apps tab"(这部分为了清楚起见,此 post 中省略了代码)然后它开始寻找 "Settings" 图标 - 它向左水平滚动一次,选择 "Downloaded" 选项卡,然后滚动回到右边,没有设置所以测试失败。

然后我拿到 phone 并再次向左滚动,"Settings" 就出现了。

我的问题是为什么它没有滚动浏览所有页面直到找到 "Settings" 所在的位置?

我在这里查看了 UIScrollable 的文档:

https://android.googlesource.com/platform/frameworks/testing/+/f612e6a05f47b28ae0f5715545658c08dd759dd7/uiautomator/library/src/com/android/uiautomator/core/UiScrollable.java

而且我找到了这个方法:

scrollBackward()

所以我决定自己解决这个问题:

    boolean settingsFound = false;
    while(!settingsFound) {
        appViews.scrollBackward();
        try {
            UiObject settingsApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Settings", true);
            settingsApp.clickAndWaitForNewWindow();
            settingsFound = true;
        } catch (Exception e){  
            e.printStackTrace();
        }   
    }

此代码一直向右滚动,直到 "Settings" 可见,然后点击它。

您可以通过 UiScrollable.setMaxSearchSwipes() 设置适合您需要的最大滑动次数,这会调整执行滚动操作以搜索子元素时允许的滚动次数。