UI Autoamtar
2021-03-03 18:13:16 0 举报
AI智能生成
安卓UI自动化全部API
作者其他创作
大纲/内容
特点
优点
API丰富
无Activity限制,支持跨应用
几乎所以的人工操作都可以模拟
缺点
Viewer说明
index
索引付
test
文本
resource-Id
唯一ID
claa
路径
panckage
包名
content-desc
描述
checkable
是否可被选中
checked
是否已经被选中
clickadle
是否可点击
enadled
是否可用
focusable
是否光标聚焦
focused
是否光标所在
scrollable
是否支持可滚动
long-clickable
是否可长按
password
文本框时是否密码呈现
selected
是否可选择
bounds
坐标
Uidevice
By定位
resource-id 唯一表示符
mUidevice.findObject(By.res("com.android.calculator2:id/digit_7")).click();
text 文本
mUidevice.findObject(By.text("7")).click();
clazz 路径
mUidevice.findObject(By.clazz("android.widget.TextView")).click();
desc 描述
mUidevice.findObject(By.desc("减")).click();
checked+id 选择属性+id
mUidevice.findObject(By.checked(true).res("android:id/switch_widget")).click();
focused+id 焦点属性+id
mUidevice.findObject(By.focused(true).res("com.android.calculator2:id/digit_7")).setText("23434");
启动关闭app
启动
Context context = InstrumentationRegistry.getInstrumentation().getContext();
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yangcong345.android.phone");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yangcong345.android.phone");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
启动方式2
mUidevice.executeShellCommand("am start -n com.yangcong345.android.phone/.splash.SplashAvtivity");
关闭
mUidevice.executeShellCommand("am force-stop com.yangcong345.android.phone");
手势操作
点击
.click();
坐标点击
mUidevice.click(402,1269);
长按
.longClick();
滑动
mUidevice.swipe(535,1430,535,432,10);
两个X坐标、两个Y坐标、步长
拖拽滑动
mUidevice.findObject(new UiSelector().text("电话")).dragTo(1131,1170,10);
坐标滑动
mUidevice.drag(177,2353,452,767,50);
输入
.setText("23434");
清除输入
.clear();
坐标点击
mUidevice.click(402,234);
延时
直接等待
Thread.sleep(2000);
常用按键
后退
mUidevice.pressBack();
HOME
mUidevice.pressHome();
多任务
mUiDevice.pressRecentApps();
锁屏
mUidevice.sleep();
唤醒
mUidevice.wakeUp();
通知栏
mUidevice.openNotification();
快速设置
mUidevice.openQuickSettings();
截图
mUidevice.takeScreenshot(new File("sdcard/a.png"));
保存到手机SD卡中
不要用先放着
检查屏幕唤醒状态
if (mUiDevice.isScreenOn()){
mUiDevice.pressRecentApps();
}
mUiDevice.pressRecentApps();
}
键盘映射
Home
mUiDevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
后退
mUiDevice.pressKeyCode(KeyEvent.KEYCODE_BACK);
输入字符
mUiDevice.pressKeyCode(KeyEvent.KEYCODE_A);
UiSelector
new UiSelector()定位
text 文本
mUidevice.findObject(new UiSelector().text("Chrome")).click();
desc 描述
mUidevice.findObject(new UiSelector().description("Chrome")).click();
package+text 包+文本
mUidevice.findObject(new UiSelector().packageName("com.google.android.apps.nexuslauncher").text("Chrome")).click();
class+index 路径+索引
mUidevice.findObject(new UiSelector().className("android.widget.TextView").index(1)).click();
class+instance 路劲+实例
mUidevice.findObject(new UiSelector().className("android.widget.TextView").instance(0)).click();
当前结构下相同路径指定编号定位,从上之下
resourceId id
mUidevice.findObject(new UiSelector().resourceId("com.google.android.apps.nexuslauncher:id/clock")).click();
focused
mUidevice.findObject(new UiSelector().focused(true)).setText("232323");
父子关系 childSelector
mUidevice.findObject(new UiSelector().className("android.widget.FrameLayout").index(1).childSelector(new UiSelector().className("android.view.VewGroup").index(0).childSelector(new UiSelector().className("android.widget.FrameLayout").index(0)))).click();
手势操作
点击
普通点击
click();
等待新窗口完成加载
clickAndWaitForNewWindow();
点击后加载超时
clickAndWaitForNewWindow(2000);
点击右下角
clickBottomRight();
点击左上角
clickTopLeft();
长按
longClick();
拖动
将元素拖动到坐标
mUidevice.findObject(new UiSelector().text("Phone")).dragTo(712,877,30);
元素拖动到元素
mUidevice.findObject(new UiSelector().text("Phone")).dragTo(new UiObject(new UiSelector().text("Chrome")),30);
滑动
元素向下
mUidevice.findObject(new UiSelector().text("Phone")).swipeDown(30);
向上
swipeUp(30);
向左
swipeLeft(30);
向右
swipeRight(30);
if判断控件是否存在
UiObject ob2 = new UiObject(new UiSelector().text("Phone"));
ob2.swipeLeft(20);
if (ob2.exists()){
ob2.swipeRight(20);
}
ob2.swipeLeft(20);
if (ob2.exists()){
ob2.swipeRight(20);
}
输入清除
输入
setText("10086");
清除
clearTextField();
获取元素属性
获取class
String as = mUidevice.findObject(new UiSelector().text("Search or type web address")).getClassName();
System.out.println("class:-------"+as);
System.out.println("class:-------"+as);
UiCollection
对象集合
从集合中进一步查询需要的对象
从集合中进一步查询需要的对象
//获取大控件ID,通过getText指定控件
UiCollection ed = new UiCollection(new UiSelector().resourceId("com.android.calculator2:id/pad_numeric"));
UiObject zf5 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "5");
UiObject zf6 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "6");
UiObject zf7 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "7");
UiObject zfequals = ed.getChildByText(new UiSelector().className("android.widget.Button"), "=");
//获取大控件ID,通过getdesc指定控件
UiCollection er = new UiCollection(new UiSelector().resourceId("com.android.calculator2:id/pad_operator"));
UiObject zfplus = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "plus");
UiObject zfmultiply = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "multiply");
UiObject zfdelete = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "delete");
//操作控件
zf5.click();
zfplus.click();
zf6.click();
zfmultiply.click();
zf7.click();
zfequals.click();
zfdelete.click();
UiCollection ed = new UiCollection(new UiSelector().resourceId("com.android.calculator2:id/pad_numeric"));
UiObject zf5 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "5");
UiObject zf6 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "6");
UiObject zf7 = ed.getChildByText(new UiSelector().className("android.widget.Button"), "7");
UiObject zfequals = ed.getChildByText(new UiSelector().className("android.widget.Button"), "=");
//获取大控件ID,通过getdesc指定控件
UiCollection er = new UiCollection(new UiSelector().resourceId("com.android.calculator2:id/pad_operator"));
UiObject zfplus = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "plus");
UiObject zfmultiply = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "multiply");
UiObject zfdelete = er.getChildByDescription(new UiSelector().className("android.widget.Button"), "delete");
//操作控件
zf5.click();
zfplus.click();
zf6.click();
zfmultiply.click();
zf7.click();
zfequals.click();
zfdelete.click();
UiScrollable
处理可滚动控件的滚动操作
向下滚动
iScrollable scroll = new UiScrollable(new UiSelector().className("android.widget.ListView"));
scroll.flingForward();
scroll.flingForward();
获取界面总list
向上滚动
flingBackward();
快速滚动自定义步长
UiScrollable china = new UiScrollable(new UiSelector().className("android.widget.ListView"));
china.flingToEnd(50);
china.flingToEnd(50);
滚动到对应元素后点击
UiScrollable china = new UiScrollable(new UiSelector().className("android.widget.ListView"));
china.scrollTextIntoView("简体中文");
mUidevice.findObject(new UiSelector().text("简体中文")).clickAndWaitForNewWindow();
china.scrollTextIntoView("简体中文");
mUidevice.findObject(new UiSelector().text("简体中文")).clickAndWaitForNewWindow();
断言
文本
UiObject2 ac = mUidevice.findObject(By.res("com.yangcong345.android.phone:id/btn_subject_learn_tab2").text("开始学习"));
String Up = ac.getText();
Assert.assertEquals("开始学习",Up);
String Up = ac.getText();
Assert.assertEquals("开始学习",Up);
断言种类
期望(expected)和实际(actual)相等则通过
Assert.assertEquals();
条件(condition)为False则通过
Assert.assertFalse();
条件(condition)为True则通过
Assert.assertTrue();
条件(condition)为非空则通过
Assert.assertNotEquals();
条件(condition)为空则通过
Assert.assertNull();
期望(expected)和实际(actual)引用不同的内存对象则通过
Assert.assertNotSame();
期望(expected)和实际(actual)引用相同的内存对象对象则通过
Assert.assertSame();
失败抛出异常
Assert.fail("Case2执行失败");
0 条评论
下一页