- UiSelector 类介绍
功能:通过各种属性与节点关系定位组件
操作步骤:找到对象->操作对象 - Android 的布局与组件属性介绍
布局
(1)线性布局:LinearLayout
(2)表格布局: TableLayout
(3)相对布局: RelativeLayout
(4)帧布局: FrameLayout
(5)网格布局: GridLayout
(6)绝对布局: AbsoluteLayout
组件
(1)文本框: TextView
(2)编辑框: EditText
(3)按钮: Button
(4)单选按钮: RadioButton
(5)复选框: CheckBox
(6)状态按钮开关: ToggleButton
(7)开关: Switch
(8)拖动条: SeekBar
(9)时钟: AnalogClock DigitalClock
(10)计时器: Chronometer
(11)列表视图: ListView
(12)网格视图: GridView
(13)进度条: ProcessBar
(14)星际评分条: RatingBar
(15)提示信息框: Toast
(16)滚动视图: ScrollView
属性介绍
3. 四中匹配关系的介绍
(1)完全匹配
(2)包含匹配
(3)正则匹配
(4)起始匹配
public void testDemo() throws UiObjectNotFoundException{
//找到对象---点击对象
UiSelector l=new UiSelector().text("联系人");
UiObject object=new UiObject(l);
object.click();
}
public void testMatches() throws UiObjectNotFoundException{
//联系人
//完全匹配:联系人
//包含匹配:系人
//正则匹配:.*系.*
//起始匹配:联系
//UiSelector l=new UiSelector().textContains("系人");
//UiSelector l=new UiSelector().textMatches(".*系.*");
UiSelector l=new UiSelector().textStartsWith("联系");
UiObject object=new UiObject(l);
object.click();
}
public void testDesc() throws UiObjectNotFoundException{
//完全匹配:应用
//UiSelector selector=new UiSelector().description("应用");
//包含匹配:应
//UiSelector selector=new UiSelector().descriptionContains("应");
//正则匹配
//UiSelector selector=new UiSelector().descriptionMatches("应.*");
//起始匹配
UiSelector selector=new UiSelector().descriptionStartsWith("应");
UiObject object=new UiObject(selector);
object.click();
TextView.class.getName();
}
4.节点关系介绍
父:Parent
子: Children
同胞: Sibling
先辈: Ancestor
后代:Descendant
5.对象搜索-文本与描述
(1)文本属性定位对象
描述属性定位对象
6.对象搜索-类名与包名
(1)类名属性定位对象
(2)包名属性定位对象
public void testClassAndPackage() throws UiObjectNotFoundException{
//完全匹配
//UiSelector selector=new UiSelector().className("android.view.View").instance(5);
//正则方式
//UiSelector selector=new UiSelector().classNameMatches(".*View").instance(7);
//class.getName
// UiSelector selector=new UiSelector().className(View.class.getName()).instance(5);
// UiObject object=new UiObject(selector);
// object.click();
UiSelector selector=new UiSelector().packageName("com.android.deskclock");
}
7.对象搜索-索引与实例
(1)索引与实例属性定位对象
public void testIndex() throws UiObjectNotFoundException{
//城市按钮
UiSelector selector=new UiSelector().className("android.widget.ImageButton").index(0);
UiObject object=new UiObject(selector);
object.click();
sleep(2000);
UiDevice.getInstance().pressBack();
sleep(2000);
//更多选项
UiSelector more=new UiSelector().className("android.widget.ImageButton").instance(1);
UiObject objectMore=new UiObject(more);
objectMore.click();
}
8.对象搜索-特殊属性与节点
(1)特殊属性定位对象
public void testProperty() throws UiObjectNotFoundException{
// UiObject swtichObj=new UiObject(new UiSelector().checkable(true));
// swtichObj.click();
// UiObject swtichObj=new UiObject(new UiSelector().checked(true));
// swtichObj.click();
//enable
// UiObject send=new UiObject(new UiSelector().enabled(false));
// System.out.println("CLASS NAME:"+send.getClassName());
//focused
// UiObject focusedObj=new UiObject(new UiSelector().focused(true));
// focusedObj.setText("abcd");
//longclick
// UiObject longclick=new UiObject(new UiSelector().longClickable(true).index(4));
// longclick.click();
//scrollable
UiScrollable scrollable=new UiScrollable(new UiSelector().scrollable(true));
scrollable.scrollForward();
sleep(1000);
scrollable.scrollForward();
sleep(1000);
scrollable.scrollForward();
sleep(1000);
}
(2)节点属性定位对象
public void testNode() throws UiObjectNotFoundException{
//childselector
// UiScrollable scrollable=new UiScrollable(new UiSelector()
// .scrollable(true).childSelector(new UiSelector().text("Android")));
// scrollable.click();
//formParent
UiObject parentObj=new UiObject(new UiSelector()
.resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item")
.fromParent(new UiSelector().className("android.widget.LinearLayout").index(1)));
parentObj.click();
}
9.对象搜索-资源 ID
public void testId() throws UiObjectNotFoundException{
// UiObject home=new UiObject(new UiSelector().resourceId("android:id/home"));
// home.click();
// UiObject download=new UiObject(new UiSelector()
// .resourceId("com.cyanogenmod.filemanager:id/navigation_view_details_item")
// .index(3));
//
// download.click();
UiObject download=new UiObject(new UiSelector()
.resourceIdMatches(".*navigation_view_details_item")
.index(3));
download.click();
}