概要
UICollection(收集)类介绍
从集合中查找对象
获取某种搜索条件组件的数量
UiCollection类介绍
1.UiCollection类说明
1)UiCollection是UIObject的子类
2)UiCollection代表元素条目集合
2.UiCollection功能说明
1)先按照一定的条件枚举出容器类界面所有
符合条件的子元素。
2)在从符合条件的元素再次通过一定的条件
最终定位需要的组件。
3.UiCollection使用场景
1)一般使用容器类组件作为父类(父类一般
Layout)
2)一般使用在需要找子类且子类由于某些
因素不好定位
3)获取某一类的数量,如获取联系人列表下当
前视图下的联系人的数量
从集合中查找对象
演示代码
//collection 演示代码
/**
* 目标任务
* 在文件管理器下,通过文件的父类 listview 获取到它所有的子类,
* 对它的子类做相应的操作
* @throws UiObjectNotFoundException
* */
//通过text找到指定的子类
public void testText() throws UiObjectNotFoundException{
//获取到父类
UiCollection uic = new UiCollection(new UiSelector().className("android.widget.ListView"));
/*通过类名获取
* */
UiSelector childPattern = new UiSelector().className("android.widget.TextView");
String text = "root";
/**
* 通过text找到指定的子类
* childPattern ,是一个UiSelect
* text 是要查找的子类的text内容
* */
UiObject root = uic.getChildByText(childPattern, text);
//测试是否找到
root.click();
}
/**
* 通过描述文本(content-desc)找到指定的子对象
* @throws UiObjectNotFoundException
*
* */
public void testDesc() throws UiObjectNotFoundException{
//获取到父类
UiCollection uic = new UiCollection(new UiSelector().className("android.widget.ListView"));
/*通过类名获取
* */
UiSelector childPattern = new UiSelector().className("android.widget.TextView");
String text = "root";
/**
* 通过content-desc找到指定的子类
* childPattern ,是一个UiSelect
* text 是要查找的子类的content-desc内容
* */
UiObject root = uic.getChildByDescription(childPattern, text);
//测试是否找到
root.click();
}
/**
* 1.找到父类
* 2.通过classname找到父类下的子类
* 3.通过下标分别获取指定的子类的对象
* @throws UiObjectNotFoundException
* */
public void testInstance() throws UiObjectNotFoundException{
UiCollection uic = new UiCollection(new UiSelector().className
("android.widget.ListView"));
/*通过类名获取
* */
UiSelector childPattern = new UiSelector().className
("android.widget.TextView");
UiObject child1 = uic.getChildByInstance(childPattern, 1);
UiObject child11 = uic.getChildByInstance(childPattern, 1);
UiObject child0 = uic.getChildByInstance(childPattern, 0);
//点击测试
child1.click();
sleep(500);
child11.click();
sleep(500);
child0.click();
sleep(500);
}
获取某种搜索条件组件的数量
演示代码
/**
* 获取某种搜索条件组件的数量
* @throws UiObjectNotFoundException
* */
public void testCount() throws UiObjectNotFoundException{
/**
* 获取当前页面的最顶级父类的方法
* index(0);或通过包名
* */
UiCollection uic = new UiCollection(new UiSelector().index(0));
/**
* getChildCount(),带参数的:会递归一直找到最低部,统计所有子类,包括后代
* getChildCount(),不带参数:只会统计子类的数量,不包括后代
* */
int textViewCount = uic.getChildCount(new UiSelector()
.className("android.widget.TextView"));
int childCount = uic.getChildCount();
System.out.println("textViewCount:"+textViewCount);
}