·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android 自动化测试经验分享 深入UiScrollable

Android 自动化测试经验分享 深入UiScrollable

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

UiScrollable is a UiCollection and provides support for searching for items in a scrollable user interface (UI) elements. This class can be used with horizontally or vertically scrollable controls.

UiScrollable是一个UiCollection(这东西还没搞懂),我们可以使用它,在可滑动的页面(水平滑动或上下滑动都可以)上查找我们想要的控件(item)。

例1:下面的这个scrollClickObject方法就是使用UiScrollable,来自动选择我们想要点击的那个控件。
复制代码 代码如下:
public void scrollClickObject(String targetClassName,String targetName) throws UiObjectNotFoundException {
    UiScrollable collectionObject = new UiScrollable(new UiSelector().scrollable(true));
    if(collectionObject.exists()) {
        UiObject scrollableObject = collectionObject.getChildByText(new UiSelector().className(targetClassName), targetName);
        scrollableObject.clickAndWaitForNewWindow();
    } else {
        UiObject targetObject = new UiObject(new UiSelector().className(targetClassName).text(targetName));
        targetObject.clickAndWaitForNewWindow();
    }
}

首先定义一个UiScrollable对象,识别这个对象的唯一条件就是,屏幕上有可滑动的控件。(这里有一个问题,就是如果屏幕上同时存在2个可滑动的控件,就会报错了)

然后我们判断这个可滑动对象是否存在

•存在,则使用getChildByText方法,获取我们想要点击的那个控件,然后点击它
•不存在,则说明页面不可滑动,也就是所有控件均已显示在界面上。那我们就可以直接使用UiObject获取控件并操作它。