Android UIAutomator 测试 : Count of all elements present in the list view

标签 android android-uiautomator

在使用 Android UIAutomator 进行移动 UI 自动化测试时,我需要找出 ListView 中存在的所有元素。

通过使用如下所示的“getChildCount()”方法,我得到了当前可见元素的计数,但是更多元素出现在 ListView 中但不可见。

示例代码如下:

    //Created UI Object for list view
UiObject listview_elements = new UiObject(new UiSelector().className("android.widget.ListView"));


//Printing the numbmer of child ements present in the List View by using getchildCount() method
System.out.println("List view elements : "+listview_elements.getChildCount());*

任何人都可以帮助获得所有 ListView 元素的计数,包括不可见元素(即当前未显示在屏幕上)。

注意: 请注意,我在这里没有实现 android UI,而只是使用 Android 的 UIAutomator 测试第三方 android 应用程序的 UI。

最佳答案

有点晚了,但这里有一些建议。

请记住,您可以使用 UI Automator 查看器来识别资源 ID 等。 AndroidSDKPath\tools\uiautomatorviewer

一些考虑因素改变了我们处理这个问题的方式。

  • 列表中的项目是否可以点击?
  • 列表中的项目大小相同吗?
  • 项目是否包含不同的文本字段?

假设列表中的项目是可点击的,您可以使用这样的选择器:

UiSelector selector = new UiSelector().clickable(true);

如果项目确实包含不同的文本字段,那么您也许可以使用 Java Set 来跟踪表示每个项目的字符串。

如果列表中的项目大小相同,那么您可以使用循环并每次向上滚动相同的量。否则,您可以查看列表中的下一项,获取其边界,获取顶部坐标,然后向上滚动直到到达列表顶部(这可能是工具栏的底部)。您可以通过查看边界来检查是否达到顶部。

在编写循环时,想法是仅在到达列表末尾时递增索引,因为当您向上滚动时,最上面的项目将变为位置 0。

你的循环看起来像这样:

//UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());;
UiObject list = device.findObject(new UiSelector().resourceId(PACKAGE + ":id/" + resourceId));

int index = 0;
int count = 0;

while (index < list.getChildCount()) {
    UiObject listItem = list.getChild(selector.index(index));

    Set<String> examinedItems = new LinkedHashSet<>();

    //if first item is a bit out of view (under toolbar) then skip it
    if (listItem.getBounds().top < TOOLBAR_BOTTOM_Y) {
        index++;
        continue;
        //this will only ever happen once = reached end of list
    }

    //get unique details from each item
    //for example, you might get a text field for that list item list this
    //UiObject textField = listItem.getChild(new UiSelector().resourceId(PACKAGE + ":id/" + childResourceId));
    String itemDetails = ...;

    //this would be relevent if the list was perfectly scrolled to the top and we don't know we are at the end of the list
    if (examinedItems.contains(itemDetails)) {
        index++;
        continue;
    }

    //do any actual testing on the item here..
    count++;

    //if index > 0 we have reached the end of the list
    if (index == 0) {
        //you'll need to inherit from InstrumentationTestCase so you can pass an instance to this method
        TouchUtils.drag(this, CENTER_X, CENTER_X, START_SCROLL_Y, START_SCROLL_Y - ITEM_HEIGHT, 150);
    }

    examinedItems.add(itemDetails);
}

//maybe return count here

关于Android UIAutomator 测试 : Count of all elements present in the list view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594916/

相关文章:

java - android 将数据库从 Assets 复制到 sdcard - 打开失败 : EISDIR (Is a directory)

Android map 设置多个标记不可见

android - 从对象 URL 播放声音

android - 如何获取 android.app.UiAutomation 的实例

android - UiAutomator -- 将小部件添加到主屏幕

android - 使用 Android GPS 检测并连接其他手机

java - Java 中是否可以替换数组?

android - 定位除边界外具有相同属性值的 UI 对象

android - 在 uiautomator 中滑动到下一页

android-studio - uiautomatorviewer 是否取代了 Android 设备监视器中的 HierarchyViewer?