UIautomator如何通过索引或实例获取子项

时间:2014-10-16 09:00:50

标签: android uiautomator

我使用以下代码获取uiautomator的子代,但没有工作

    UiObject my = new UiObject(new UiSelector().className("android.widget.LinearLayout").instance(2));

    int cound = my.getChildCount();
    for(int i = cound - 1; i >= 0; i--) {
        UiObject childmy2 = my.getChild(my.getSelector().childSelector(new UiSelector().instance(i)));
        Log.e("xface", "childmy2=" + childmy2.getClassName());
        Log.e("xface", "childmy2=" + childmy2.getBounds().toString());
    }

任何人都可以帮助我吗?

简单地问我的问题:如何实现这个功能:

ArrayList getAllChild(UiObject节点)

输入:给定节点你想得到孩子

return:给定节点的所有子节点

1 个答案:

答案 0 :(得分:7)

假设你有:

<parent>
    <child1>
          <child1.1></child1.1>
          <child1.2></child1.2>
    </child1>
    <child2>
          <child2.1></child2.1>
          <child2.2></child2.2>
    </child2>
    <child3>
          <child3.1></child3.1>
          <child3.2></child3.2>
    </child3>
</parent>

Bellow解决方案将返回结果Child 1,Child 2,Child 3

UiObject object = new UiObject(<UiSelector for Parent>);
int cnt = object.getChildCount();
for(int i = 0; i < cnt; i++) {
   UiObject eachItem = object.getChild(new UiSelector().index(i));
}

如果你想要孩子1.1,孩子1.2和孩子2.1

UiObject object = new UiObject(<UiSelector for Parent>);
int cnt = object.getChildCount();
for(int i = 0; i < cnt; i++) {
    UiObject eachItem = object.getChild(new UiSelector().index(i))
                              .getChild(<UiSelector for child 1.1 or child 1.2 ...>);
// Note: Before getting this you should check whether it has any child or not.
}