- 1.新建JavaProject工程:CmbcTest
- 2.ProPerties>Java Build Path>Libraries>Add Library>JUnit>JUnit4,点击Finish
-
3.ProPerties>Java Build Path>Libraries>Add External JARS,添加..sdk/platforms/android-xx/android.jar、uiautomator.jar,点击OK,配置如下图
- 4.新建类Main,继承UiAutomatorTestCase,代码如下
public class Main extends UiAutomatorTestCase {
public void testDemo() throws Exception {
//测试1000次
for (int i = 0; i < 10000; i++) {
//进入APP
UiObject cmbc = new UiObject(new UiSelector().text("应用名"));
cmbc.click();
//睡7s
sleep(7000);
//点击返回键
getUiDevice().pressBack();
//点击退出按钮
UiObject exit = new UiObject(new UiSelector().resourceId("android:id/button1"));
exit.click();
//主页面睡7s
sleep(7000);
}
}
}
-
5.生成build.xml
进入sdk/tools/,执行命令android create uitest-project -n auto -t 5 -p /Users/kevin/AndroidEclipseProjects/standard/CmbcTest-n auto (生成的jar名)
-p /Users/kevin/Android... (工程路径)
-t 5 (android list,可查看对应的android-id)
-
6.进入工程下,执行命令: ant build
7.工程bin目录下会生成auto.jar,执行命令:adb push auto.jar data/local/tmp/
8.执行命令:adb shell uiautomator runtest auto.jar -c com.gmrz.test.Main,开启自动化测试
-c com.gmrz.test.Main (包名+类名)