uiautomator跑安卓端UI testing

uiautomator跑安卓端UI testing

 用uiautomator做安卓的app端的UI testing的环境搭建及编jar包和运行case的步骤如下:

1、新建java工程

2、右键properties,

uiautomator跑安卓端UI testing

添加junit4的library,并添加android.jar和uiautomator的jar包依赖

3、在工程下新建package,之后新建类,代码如下:

(备注:extends的类需要写清楚是UiAutomatorTestCase ,具体的方法需要以test开头,并且throws UiObjectNotFoundException )

  1 package com.ganji.loginhelper;
  2 
  3  
  4 
  5 import com.android.uiautomator.core.UiObject;
  6 
  7 import com.android.uiautomator.core.UiObjectNotFoundException;
  8 
  9 import com.android.uiautomator.core.UiScrollable;
 10 
 11 import com.android.uiautomator.core.UiSelector;
 12 
 13 import com.android.uiautomator.testrunner.UiAutomatorTestCase;
 14 
 15 public class Runner extends UiAutomatorTestCase {  
 16 
 17    public void testDemo() throws UiObjectNotFoundException {  
 18 
 19   // Simulate a short press on the HOME button.
 20 
 21   getUiDevice().pressHome();
 22 
 23   // We’re now in the home screen. Next, we want to simulate
 24 
 25   // a user bringing up the All Apps screen.
 26 
 27   // If you use the uiautomatorviewer tool to capture a snapshot
 28 
 29   // of the Home screen, notice that the All Apps button’s
 30 
 31   // content-description property has the value “Apps”.  We can
 32 
 33   // use this property to create a UiSelector to find the button.
 34 
 35   UiObject allAppsButton = new UiObject(new UiSelector()
 36 
 37      .description("Apps"));
 38 
 39   // Simulate a click to bring up the All Apps screen.
 40 
 41   allAppsButton.clickAndWaitForNewWindow();
 42 
 43   // In the All Apps screen, the Settings app is located in
 44 
 45   // the Apps tab. To simulate the user bringing up the Apps tab,
 46 
 47   // we create a UiSelector to find a tab with the text
 48 
 49   // label “Apps”.
 50 
 51   UiObject appsTab = new UiObject(new UiSelector()
 52 
 53      .text("Apps"));
 54 
 55   // Simulate a click to enter the Apps tab.
 56 
 57   appsTab.click();
 58 
 59   // Next, in the apps tabs, we can simulate a user swiping until
 60 
 61   // they come to the Settings app icon.  Since the container view
 62 
 63   // is scrollable, we can use a UiScrollable object.
 64 
 65   UiScrollable appViews = new UiScrollable(new UiSelector()
 66 
 67      .scrollable(true));
 68 
 69   // Set the swiping mode to horizontal (the default is vertical)
 70 
 71   appViews.setAsHorizontalList();
 72 
 73   // Create a UiSelector to find the Settings app and simulate        
 74 
 75   // a user click to launch the app.
 76 
 77   UiObject settingsApp = appViews.getChildByText(new UiSelector()
 78 
 79      .className(android.widget.TextView.class.getName()),
 80 
 81      "Settings");
 82 
 83   settingsApp.clickAndWaitForNewWindow();
 84 
 85   // Validate that the package name is the expected one
 86 
 87   UiObject settingsValidation = new UiObject(new UiSelector()
 88 
 89      .packageName("com.android.settings"));
 90 
 91   assertTrue("Unable to detect Settings",
 92 
 93      settingsValidation.exists()); 
 94 
 95   UiObject reportBug = new UiObject(new UiSelector().text("Sound"));
 96 
 97   reportBug.clickAndWaitForNewWindow();
 98 
 99   UiObject soundValidation = new UiObject(new UiSelector()
100 
101   .text("Volumes"));
102 
103    assertTrue("Unable to detect Sound",
104 
105        soundValidation.exists()); 
106 
107   getUiDevice().pressHome();
108 
109   }  
110 
111 }

4、之后编译和发布uiautomator测试:

创建一个编译文件:

C:Users58>android create uitest-project -n autoLogin -t 15 -p E:Debin2autoLogin

Added file E:Debin2autoLoginuild.xml

以上是创建成功了,叫做build.xml

这个命令是:

Android create uitest-project -n xxx -t xxx -p xxx

其中-n 后面的xxx代表要project的name

-t 的15是这样获取的:通过android list,能够得到一个列表,因为我用的android.jar和uiautomator.jar是19的,所以查找到对应的是:

uiautomator跑安卓端UI testing

所以我的-t 就写成了15

然后就是-p 后面的参数,是工程所在的位置,因为这个build.xml的生成目录就是这个工程目录

命令行创建提示创建成功之后,即added file xxx这句话出现之后,在工程目录下能够看到这个文件,通过eclipse目录刷新也可以看到:(多了三个文件)

 uiautomator跑安卓端UI testing

之后如果eclipse中直接有ant的这个打包设置,就可以通过在build.xml上右键Run As选择ant build,然而我的eclipse中没有配置成功,但是没关系,只要在命令行里面能运行成功就可以。

build.xml的文件打开后,能看到default后面本来是"help"的,改成build

 uiautomator跑安卓端UI testing

然后我的机器中没有安装ant,去官网下载ant的文件,之后安装成功,需要进行环境变量的配置,主要包含三个地方:

ANT_HOME  D:Program Filesapache-ant-1.9.7-binapache-ant-1.9.7

path  D:Program Filesapache-ant-1.9.7-binapache-ant-1.9.7in

classpath D:Program Filesapache-ant-1.9.7-binapache-ant-1.9.7lib

然后在cmd下直接运行ant 和ant -version,如果提示正确,不是出现ant不是可用的命令这句话,就说明环境变量配置成功了

接下来是比较容易出现问题的地方:

进入到工程目录下,

命令行输入:ant + build.xml

uiautomator跑安卓端UI testing

应该是没有后面的后缀名,直接ant build

 uiautomator跑安卓端UI testing

uiautomator跑安卓端UI testing

然后能看到已经build成功了,并且在工程目录的bin文件夹下,文件名叫做:autoLogin.jar

5、接下来就是将jar包push到手机中,然后运行程序了

push进入的手机的目录是/data/local/tmp,

执行case的命令是:adb shell uiautomator runtest autoLogin.jar -c com.ganji.loginhelper.Runner

runtest后面是之前push进去的jar包,-c后面是要执行的class(class前面要加包名的,即xx.xx.package.classname)

uiautomator跑安卓端UI testing

然后就会有跑case之后的信息打出来,完成,后面就是怎么写case实现自己想要的测试效果啦~~~

针对部分元素需要通过参数直接传递进去进行设置的需求,可以通过如下方式调用:

E:Debin2>adb shell uiautomator runtest cheshangtongLogin.jar -e username xxx
-e passwd yyyy -c com.cheshangtong.loginhelper.LoginTest