1.
adb shell uiautomator runtest <JARS> -c <CLASSES>[options]
-e 传入一个键值对到测试程序中,可用于一些需要变化的参数传入
2.
package com.text;
import android.os.RemoteException;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class Test extends UiAutomatorTestCase {
public void testDemo(){
// home 键
UiDevice.getInstance().pressHome();
sleep(2000);
}
// Menu 键
public void testMenu(){
UiDevice.getInstance().pressMenu();
sleep(2000);
}
public void testRecent() throws RemoteException{
UiDevice.getInstance().pressRecentApps();
sleep(2000);
}
}
3.运行全部方法
首先 编译build.xml文件
ant -buildfile build.xml
然后 push
adb push /Users/cjy/Documents/workspace/TestDemo/bin/demotest.jar /data/local/tmp
```
运行全部方法
`adb shell uiautomator runtest demotest.jar -c com.text.Test#testRecent -c com.text.Test#testDemo -c com.text.Test#testMenu
`
4.-e参数
package com.text;
import android.os.Bundle;
import android.os.RemoteException;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class Test extends UiAutomatorTestCase {
public void testDemo(){
//按 home 键
UiDevice.getInstance().pressHome();
sleep(2000);
Bundle bundle = getParams();
String phone = bundle.getString("phone");
System.out.println("PHONE:"+phone);
}
public void testMenu(){
UiDevice.getInstance().pressMenu();
sleep(2000);
}
public void testRecent() throws RemoteException{
UiDevice.getInstance().pressRecentApps();
sleep(2000);
}
}
编译build.xml 文件
push
运行第一个方法
adb shell uiautomator runtest demotest.jar -c com.text.Test#testDemo -e phone 18888888888
结果:
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: class=com.text.Test
INSTRUMENTATION_STATUS: stream=
com.text.Test:
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: test=testDemo
INSTRUMENTATION_STATUS_CODE: 1
PHONE:18888888888
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: class=com.text.Test
INSTRUMENTATION_STATUS: stream=.
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: test=testDemo
INSTRUMENTATION_STATUS_CODE: 0
INSTRUMENTATION_STATUS: stream=
Test results for UiAutomatorTestRunner=.
Time: 2.531
OK (1 test)
INSTRUMENTATION_STATUS_CODE: -1
快速调试
无法再 Eclipse 直接调试,命令调试繁琐效率低下,将调试步骤转换成脚本
1.创建 build 文件
2.修改 build 文件
3.开始编译
4.push 文件
5.运行测试