Android自动化测试
2018-10-25 本文已影响407人
XINHAO_HAN
记录 2018-08-08 该东西只是记录,方便你我他
UiDevice 此类介绍:
打开某个APP
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
try {
UiObject x = mDevice.findObject(new UiSelector().text("中国农业银行"));
x.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
//UiDevice 方法大致介绍
mDevice.pressBack();//模拟手机回退键
mDevice.pressHome();//模拟手机Home键
mDevice.pressMenu();//模拟手机菜单键
mDevice.pressRecentApps();//模拟手机任务栏
... //刻根据业务需求来加自己业务上的东西
工具介绍
uiautomatorviewer.bat
在其目录为:
D:\Android\SDK\tools\bin
下
页面为这个样子的:
![](https://img.haomeiwen.com/i5337239/c0958a96dca10c69.png)
UiObject uiObject = new UiObject(new UiSelector().resourceId("com.android.systemui:id/recent_app_clear"));
if (uiObject.exists()) {
uiObject.click();
}
![](https://img.haomeiwen.com/i5337239/ac1784e0ad286aa5.png)
// 在该页面查找ID为:com.android.systemui:id/task_view_thumbnail 的控件
UiObject uiObject = new UiObject(new UiSelector().resourceId("com.android.systemui:id/recent_app_clear"));
//该控件是否存在
if (uiObject.exists()) {
//模拟点击该控件
uiObject.click();
}
如果控件没有ID该咋办?那就取它的【内容】和【类】
![](https://img.haomeiwen.com/i5337239/8313fca9246b4a3c.png)
在TEXT里边这样写
UiObject weixin= new UiObject(new UiSelector().text("微信").className("android.widget.TextView"));
//判断是否存在
if (weixin.exists()) {
//模拟点击
weixin.click();
break;
}
如果我想在文本框输入东西该咋办,我这里用的是ID,没有ID用类名 + 内容
![](https://img.haomeiwen.com/i5337239/27539bd126fb75b6.png)
UiObject duanxin= new UiObject(new UiSelector().resourceId("com.zui.mms:id/recipient_text_view"));
//判断是否存在
if (duanxin.exists()) {
//模拟点击
duanxin.setText("你要输入的Text");
break;
}
如果我又没有 [ID] 也没有【内容】 该咋查找
第一种方法
![](https://img.haomeiwen.com/i5337239/4dc6df3a7905de0d.png)
步骤1.我们先找到上层树有ID得控件
![](https://img.haomeiwen.com/i5337239/16a28173d4bbc744.png)
步骤2.找到子类控件开始写代码
![](https://img.haomeiwen.com/i5337239/3690fa40217cf902.png)
我们找到第一个子类控件为:android.widget.LinearLayout Index为:1
![](https://img.haomeiwen.com/i5337239/4f8f9f81f2c6a4c8.png)
![](https://img.haomeiwen.com/i5337239/fd7c1a63f10833f6.png)
我们找到第二个子类控件为:android.widget.LinearLayout Index为:1
![](https://img.haomeiwen.com/i5337239/8abaf06ae38aa71c.png)
![](https://img.haomeiwen.com/i5337239/fd7c1a63f10833f6.png)
我们找到第三个子类控件为:android.widget.LinearLayout Index为:0
![](https://img.haomeiwen.com/i5337239/c1ca8f83152ca611.png)
![](https://img.haomeiwen.com/i5337239/4e1cec4720cb8f41.png)
我们找到第四个子类控件为:android.widget.TextView Index为:0
![](https://img.haomeiwen.com/i5337239/2b878126390ec130.png)
![](https://img.haomeiwen.com/i5337239/4e1cec4720cb8f41.png)
找到了!
代码是这样的
![](https://img.haomeiwen.com/i5337239/59bcc85933aa1b33.png)
![](https://img.haomeiwen.com/i5337239/1d19a81ad0d9b72b.png)
UiObject uiObject = new UiObject(new UiSelector().resourceId("com.zui.filemanager:id/fileListView"));
UiObject child = uiObject.getChild(new UiSelector().className("android.widget.LinearLayout").resourceId("com.zui.filemanager:id/background").index(1));
UiObject child1 = uiObject.getChild(new UiSelector().className("android.widget.LinearLayout").index(1));
UiObject child2 = child1.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
UiObject child3 = child2.getChild(new UiSelector().className("android.widget.TextView").index(0).resourceId("com.zui.filemanager:id/file_name"));
if (child3.exists()) {
child3.click();
}
第二种
![](https://img.haomeiwen.com/i5337239/7db6d0f5448b30bb.png)
正确的数为层叠数,明白吧?就是树状往下数,别一股脑往下数
代码为:
UiObject view1 = new UiObject(new UiSelector().className("android.view.View").instance(24));//数字表示你数的位置
UiObject view2 = new UiObject(new UiSelector().className("android.view.View").instance(25));//数字表示你数的位置
如果不会数,或者嫌麻烦又或者说太辣眼睛,就直接for循环
for循环中的100你自己摸索一下看看页面元素多不多,估算一下,也可能会有200多个
for(int i = 0;i<100;i++){
String test = "";
UiObject view1 = new UiObject(new UiSelector().className("android.view.View").instance(24));
UiObject view2 = new UiObject(new UiSelector().className("android.view.View").instance(25));
try {
test = view1.getText();
if(test == null || test.isEmpty()){
test = view1.getContentDescription();
}
}catch(Exception e){
test = view1.getContentDescription();
}
Log.e("输出", "auditOrder: " + "该元素名称为:" + test + "该元素位置为:" + i );
}
既没有className也没有index咋办?
直接模拟点击屏幕
![](https://img.haomeiwen.com/i5337239/941162738d6877fb.png)
也可以在开发者模式中,选择显示触摸位置
UiDevice.getInstance().click(x,y)
读不到页面怎么办,报错为:
![](https://img.haomeiwen.com/i5337239/5af90df1c5f0c662.png)
Error taking device screenshot: EOF
Error taking device screenshot: EOF
这个只能用adb命令来完成了,这样能获取到,只不过有点麻烦
执行该命令获取页面元素为xml,那个工具就是uiautomatorviewer获取的也是xml
这个是没办法中的办法,所以就没有那么优越的窗口供我们使用了,只能用txt文本在里边找
//执行该命令获取页面元素为xml
adb shell /system/bin/uiautomator dump /data/local/tmp/liukangUi.xml
//返回该提示代表成功:dump: creates an XML dump of current UI hierarchy
//推送到电脑
adb pull /data/local/tmp/liukangUi.xml D:\Xml