UIautomator how to get a child by index or instance

I use the following code to get the uiautomator child, but not working

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()); } 

Can anybody help me?

simplefy my question is: how to implement this function:

ArrayList getAllChild (UiObject node)

input: given node you want getchild

return: all children of this node

+5
source share
1 answer

Suppose you have:

 <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 solution will return the result 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)); } 

If you need baby 1.1, baby 1.2 and baby 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. } 
+7
source

Source: https://habr.com/ru/post/1204810/


All Articles