UIAutomator快速上手
UIAutomator快速上手
一、环境准备:
1、win7
2、安装带有android sdk的eclipse,即adt-bundle。我用的这个:adt-bundle-windows-x86_64-20140702
3、安装ant,并把ant的bin目录配置到系统变量path。如下图:
二、编写写自动化用例
1、用adt-bundle的eclipse新建一个java项目:UIAutomatorDemo
2、添加jar包:eclipse中选中项目,右键-Build Path-Configure Build Path,添加android.jar和uiautomator.jar(注意这里添加的android-19里的):
3、创建包:com.uiautomator.test,新建一个类:
UiAutomatorDemo.class
4、在\adt-bundle-windows-x86_64-20140702\sdk\tools目录下,打开UIautomatorviewer定位元素
如定位蓝牙:
5、根据UIautomatorviewer找到测试元素的属性写测试代码
测试代码如下:功能:点击设置-点击蓝牙-开启蓝牙
package com.uiautomator.test; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.os.SystemClock; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.core.UiObject; import com.android.uiautomator.core.UiObjectNotFoundException; import com.android.uiautomator.core.UiScrollable; import com.android.uiautomator.core.UiSelector; import com.android.uiautomator.testrunner.UiAutomatorTestCase; /* *包名:com.uiautomator.test *作者:Adien_cui *时间:2016-12-8 上午8:16:00 *描述:UIAutomator快速上手, 用例:打开蓝牙 **/ public class UiAutomatorDemo extends UiAutomatorTestCase{ public void testSetScreenOffTime() throws Exception{ //打开系统设置页面 Runtime.getRuntime().exec("monkey -p com.android.settings -v 1"); //找到滚动列表 UiScrollable mSettingList = new UiScrollable(new UiSelector().packageName("com.android.settings").resourceId("android:id/list")); //点击“蓝牙” UiObject mBlueTooth = mSettingList.getChildByText(new UiSelector().resourceIdMatches(".*title"), "蓝牙"); mBlueTooth.clickAndWaitForNewWindow(); //开启蓝牙 UiObject mOpenBlueTooth = new UiObject(new UiSelector().resourceId("android:id/checkbox")); mOpenBlueTooth.click(); } }
6、此时eclipse中该项目的目录结构如下:
三、代码写好后开始编译项目
1、查找需要编译Sdk的id,命令行输入:
android list
2、UIAutomator自动化测试项目编译使用的是ant编译方式。此前创建的只是java项目,这时需要为项目添加ant编译脚本。在命令行输入:
android create uitest-project -n projectName -t targetID -p projectPath
其中:targetID就是上一步android list 命令查到的相应id;projectName是编译目标项目的名称,也是对应生成jar包的名称;projectPath是编译目标项目根目录路径。
所以此项目的编译的截图如下:
android create uitest-project -n UIAutomatorDemo -t 4 -p E:\workspace\UIAutomatorDemo
3、执行上面的命令后会在该项目的根目录生成三个文件:
说明:
build.xml: ant编译脚本
local.properties:配置本机SDK路径
project.properties:存储编译使用的APL Level。本项目是target=android-19。
4、开始编译,切到该项目的根目录(主要是build.xml所在的目录)输入命令
ant build
此时会在该项目的bin目录下生成一个jar包。这就是用UIAutomator测试所需的最终产物:
四、测试用例执行
1、将生成的jar包推送的手机的/data/local/tmp目录下。注意:此时要连接测试手机,并打开USB调试。
命令行切换到项目的bin目录下输入:
adb push UIAutomatorDemo.jar /data/local/tmp
如下图:
2、最后一步,执行用例。
输入:
adb shell uiautomator runtest <jar文件名>-c <包名.类名>
如:
adb shell uiautomator runtest UIAutomatorDemo.jar -c com.uiautomator.test.UiAutomatorDemo
完….
还没有评论,来说两句吧...