UiCollection说明:
UiCollection是UiObject的子类UiCollection代表元素的条目集合Uicollection功能说明:
先按照一定的条件枚举出容器类界面所有符合条件的子元素在从符合条件的元素再次通过一定的条件最终定位需要的组件(先找到一个合集,然后再找我们需要的元素)两大功能:
从集合中获取对象获取某种搜索条件组建的数量使用场景:
一般使用容器类组件作为父类一般使用在需要找子类且子类由于某种原因不好定位的场合获取某一类的数量,如获取联系人列表下当前视图下联系人的数量从集合中查找对象:
第一个搜索:
第一个条件为从子集中搜索的条件一,第二个条件为特定的描述,两次筛选
getChildByText():
public void testText() throws UiObjectNotFoundException { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); UiCollection uiCollection = new UiCollection(new UiSelector().className("android.widget.ListView")); //首先找到TextView的集合 UiSelector childPattern = new UiSelector().className("android.widget.TextView"); //在集合中找到相应的TextView String nameD = "Android"; UiObject Android = uiCollection.getChildByText(childPattern, nameD); Android.click(); } getChildByDescription(): public void testDEscription() throws UiObjectNotFoundException { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); UiCollection uiCollection = new UiCollection(new UiSelector().resourceId("com.android.contacts:id/dialpad_layout")); UiSelector uiSelector = new UiSelector().className("android.widget.RelativeLayout"); String text = "8按钮"; UiObject object = uiCollection.getChildByDescription(uiSelector, text); object.click(); } getInstance(): public void testInstance() throws UiObjectNotFoundException { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); UiCollection uiCollection = new UiCollection(new UiSelector().resourceId("com.android.contacts:id/dialpad_layout")); UiSelector uiSelector = new UiSelector().className("android.widget.RelativeLayout"); UiObject object1 = uiCollection.getChildByInstance(uiSelector, 0); UiObject object4 = uiCollection.getChildByInstance(uiSelector, 10); UiObject object44 = uiCollection.getChildByInstance(uiSelector, 10); UiObject object3 = uiCollection.getChildByInstance(uiSelector, 7); UiObject object5 = uiCollection.getChildByInstance(uiSelector, 11); object1.click(); object0.click(); object00.click(); object8.click(); object6.click(); } 获取某种搜索条件组建的数量 public void testGetChildCount() throws UiObjectNotFoundException { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); //整个界面的最顶级 UiCollection uiCollection = new UiCollection(new UiSelector().index(0)); //获取顶级界面下的TextView包含后代所有的 int textCount = uiCollection.getChildCount(new UiSelector().className("android.widght.TextView")); System.out.println(textCount); UiCollection uiCollection1 = new UiCollection(new UiSelector().resourceId("com.android.contacts:id/dialpad_layout")); //只包含最上级的子类,不会获取后代子类 int tableChildCount = uiCollection1.getChildCount(); System.out.println("Wjx" + tableChildCount); }