UiSelectorAPI
一、UiSelectorAPI介绍通过一定查询方式,定位到所要操作的元素。相关API返回值API说明UiSelectortext(String text)文本UiSelectortextContains(String text)...
一、UiSelectorAPI介绍
通过一定查询方式,定位到所要操作的元素。
返回值 | API | 说明 |
UiSelector | text(String text) | 文本 |
UiSelector | textContains(String text) | 文本包含 |
UiSelector | textMatches(String regex) | 文本正则 |
UiSelector | textStartsWith(String text) | 文本起始匹配 |
UiSelector | description(String desc) | 描述 |
UiSelector | descriptionContains(String desc) | 描述包含 |
UiSelector | descriptionMatches(String regex) | 描述正则 |
UiSelector | descriptionStartsWith(String desc) | 描述开始字符匹配 |
UiSelector | className(String className) | 类名 |
UiSelector | classNameMatches(String regex) | 正则类名 |
UiSelector | packageName(String name) | 包名 |
UiSelector | packageNameMatches(String regex) | 包名正则 |
UiSelector | index(int index) | 索引 |
UiSelector | instance(int instance) | 实例 |
UiSelector | checked(boolean val) | 选择属性 |
UiSelector | clickable(boolean val) | 可点击属性 |
UiSelector | childSelector(UiSelector selector) | 从子项里开始匹配 |
UiSelector | enabled(boolean val) | 可操作属性 |
UiSelector | focusable(boolean val) | 焦点属性 |
UiSelector | focused(boolean val) | 当前焦点属性 |
UiSelector | longClickable(boolean val) | 长按属性 |
UiSelector | scrollable(boolean val) | 滚动属性 |
UiSelector | selected(boolean val) | 背景选择属性 |
UiSelector | childSelector(UiSelector selector) | 从当前组件往下查找其子子孙孙中符合条件的所有组件 |
UiSelector | fromParent(UiSelector selector) | 从当前组件往上查找其长辈中符合条件的所有组件 |
UiSelector | resourceId(String id) | 资源ID |
UiSelector | resourceIdMatches(String regex) | 资源ID正则 |
三、示例
从list列表获取第一个选项:
new UiObject(new UiSelector().resourceId("com.soooft.android.baicaipos:id/dialog_list_select").instance(0).childSelector(new UiSelector().instance(0))).click();
选择列表里第二列的指定属性:
sUiSelector = new UiSelector().resourceId("com.soooft.android.baicaipos:id/sell_rv_list").instance(0).childSelector(new UiSelector().instance(1).resourceId("com.soooft.android.baicaipos:id/item_product_money"));
获取List列表的count:
new UiObject(new UiSelector().resourceId("com.soooft.android.baicaipos:id/sell_rv_list").instance(0)).getChildCount()
//用下面的getIndexUiSelector方法也可以获取
/**
* 获取List列表的count
*
* @return 失败返回-1
*/
protected int getListCount(String resourceId, int... childInstances) {
getIndexUiSelector(resourceId, childInstances);
try {
return getUiObject().getChildCount();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
return -1;
}
/**
* 点击事件
*
* @param resourceId ID
* @param childInstances 子项索引
*/
protected void onClick(String resourceId, int... childInstances) {
try {
getIndexUiSelector(resourceId, childInstances);
getUiObject().click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
/**
* 返回指定索引UiSelector
*
* @param resourceId ID
* @param childInstances 子项索引
*/
protected void getIndexUiSelector(String resourceId, int... childInstances) {
sUiSelector = childInstances.length > 0 ? new UiSelector().resourceId(resourceId).instance(0) : new UiSelector().resourceId(resourceId);
for (int i = 0; i < childInstances.length; i++) {
sUiSelector = sUiSelector.childSelector(new UiSelector().index(childInstances[i]));
}
// return sUiSelector;
}
本文转载整理自:
更多推荐
所有评论(0)