1.在被测程序中加入server-standalone.jar包
2.在被测程序的AndroidManifest中加入代码:
<instrumentation
android:name="com.google.android.testing.nativedriver.server.ServerInstrumentation"
android:targetPackage="com.console" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3.创建本地测试程序
4.在本地测试程序中加入client-standalone.jar包
5.在本地测试程序中加入testng6.8.jar包
6.在BeforeClass中加入如下代码:
@BeforeClass
public void beforeClass() {
// 这里把之前的adb命令放入到beforeclass中,如果有beforesuite的话也可以放到beforesuite中
try {
String cmd = "cmd.exe /k start adb shell am instrument "
+ "com.console/com.google.android.testing.nativedriver.server.ServerInstrumentation";
Runtime.getRuntime().exec(cmd);
Thread.sleep(10000);
String cmd2 = "cmd.exe /k start "
+ "adb forward tcp:54129 tcp:54129";
Runtime.getRuntime().exec(cmd2);
Thread.sleep(2000);
driver = getDriver();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 统一设置通过find方法查找元素等待超时的时间,这里有时候发现等待超时的时间远远大于这个设置的,原因还未知
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
public void login() {
startLoginActivity();
WebElement et = driver.findElementById("et_input");
et.sendKeys("ps");
WebElement btn = driver.findElementById("btn_ok");
btn.click();
}