效果:
打開MyTest.apk,點擊run uiautomator,就能直接運行你的腳本。
方案概述:
新建一個Android app工程MyTest,在Activity中添加Button,用于啟動腳本
給這個app添加系統(tǒng)簽名
在MyTest中新建一個module,命名為MyTestCase,用于編寫腳本
使用
am instrument命令實現(xiàn)腳本的運行
1. 新建一個Android應(yīng)用,命名為MyTest
選擇Android的版本,并且選擇empty Activity,一路next到Finish
2. 修改activity_main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cxq.com.mytest.MainActivity">
<Button
android:onClick="runMyUiautomator"
android:id="@+id/runBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="run" />
</RelativeLayout>
3. 修改MainActivity文件如下:
package cxq.com.mytest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Button runBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runBtn= (Button) findViewById(R.id.runBtn);
}
/**
* 點擊按鈕對應(yīng)的方法
* @param v
*/
public void runMyUiautomator(View v){
Log.i(TAG, "runMyUiautomator: ");
new UiautomatorThread().start();
}
/**
* 運行uiautomator是個費時的操作,不應(yīng)該放在主線程,因此另起一個線程運行
*/
class UiautomatorThread extends Thread {
@Override
public void run() {
super.run();
String command=generateCommand("cxq.com.testcase", "TestDemo_1", "demo");
CMDUtils.CMD_Result rs= CMDUtils.runCMD(command,true,true);
Log.e(TAG, "run: " + rs.error + "-------" + rs.success);
}
/**
* 生成命令
* @param pkgName 包名
* @param clsName 類名
* @param mtdName 方法名
* @return
*/
public String generateCommand(String pkgName, String clsName, String mtdName) {
String command = "am instrument --user 0 -w -r -e debug false -e class "
+ pkgName + "." + clsName + "#" + mtdName + " "
+ pkgName + ".test/android.support.test.runner.AndroidJUnitRunner";
Log.e("test1: ", command);
return command;
}
}
}
其中CMDUtils.java內(nèi)容如下:
package cxq.com.mytest;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 執(zhí)行命令
*/
public class CMDUtils {
private static final String TAG = "CMDUtils";
public static class CMD_Result {
public int resultCode;
public String error;
public String success;
public CMD_Result(int resultCode, String error, String success) {
this.resultCode = resultCode;
this.error = error;
this.success = success;
}
}
/**
* 執(zhí)行命令
*
* @param command 命令
* @param isShowCommand 是否顯示執(zhí)行的命令
* @param isNeedResultMsg 是否反饋執(zhí)行的結(jié)果
* @retrun CMD_Result
*/
public static CMD_Result runCMD(String command, boolean isShowCommand,
boolean isNeedResultMsg) {
if (isShowCommand)
Log.i(TAG, "runCMD:" + command);
CMD_Result cmdRsult = null;
int result;
try {
Process process = Runtime.getRuntime().exec(command);
result = process.waitFor();
if (isNeedResultMsg) {
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
BufferedReader successResult = new BufferedReader(
new InputStreamReader(process.getInputStream()));
BufferedReader errorResult = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
cmdRsult = new CMD_Result(result, errorMsg.toString(),
successMsg.toString());
}
} catch (Exception e) {
Log.e(TAG, "run CMD:" + command + " failed");
e.printStackTrace();
}
return cmdRsult;
}
}
4. 新建一個Module,用于寫你的測試腳本
- 在Android Studio的左側(cè)欄,右擊“New -- Module -- Phone&Table Module”,并填寫如下信息
next之后,選擇add no activity -- finish
5. 修改mytestcase模塊的gradle文件
- 修改默認(rèn)的runner,在defaultConfig中添加runner
defaultConfig {
applicationId "cxq.com.mytestcast"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
- 在dependencies中添加依賴
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'junit:junit:4.12'
compile 'com.android.support.test:runner:0.4.1'
compile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
6. 新建你的測試用例TestOne

代碼如下:
package cxq.com.mytestcast;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class TestOne {
private UiDevice mDevice;
@Test
public void demo() throws UiObjectNotFoundException {
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressHome();
mDevice.pressHome();
UiObject x=mDevice.findObject(new UiSelector().text("聯(lián)系人"));
x.click();
}
}
Case說明:
@RunWith注解,代表使用什么runner
@Test注解,表示當(dāng)前的方法為測試方法,同理還有其他的@Before等注解,具體case的寫法本文不贅述
運行一下你的case,測試是否有效,運行方法:點擊帶有Test注解的方法左側(cè)綠色三角形

此時,run窗口會有如下信息:

如果你只是急于達(dá)到效果,而不注重原理,可以跳過原理分析的閱讀
原理分析:
可以看到,窗口中其實運行了3條命令
adb push D:\DEVl\MyTest\mytestcast\build\outputs\apk\mytestcast-debug.apk /data/local/tmp/cxq.com.mytestcast
在我們點擊運行之后,AS會自動將用例打包成apk文件,路徑為
$工程目錄\build\outputs\apk\工程名-debug.apk
這個apk文件中就包含著我們的測試用例adb shell pm install -r "/data/local/tmp/cxq.com.mytestcast
安裝這個apk到手機(jī)adb shell am instrument -w -r -e debug false -e class cxq.com.mytestcast.TestOne#demo cxq.com.mytestcast.test/android.support.test.runner.AndroidJUnitRunner
使用AndroidJunitRunner啟動你的用例
通過分析這個過程,就知道AS是怎么把用例跑起來的,仿照這個原理就可以自己實現(xiàn)通過apk調(diào)用uiautomator用例,只要讓app中的button的響應(yīng)事件去執(zhí)行am instrument命令即可,但是由于執(zhí)行這個命令也是需要權(quán)限的,因此需要給app添加系統(tǒng)簽名
7. 給APP添加系統(tǒng)簽名
大致過程如下(具體的細(xì)節(jié)可以參照我之前寫過的文章Android Studio自動生成帶系統(tǒng)簽名的apk):
a. 修改App中的manifest.xml文件,添加android:sharedUserId="android.uid.system"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system"
package="cxq.com.mytest">
b. 生成js文件
c. 使用keytool-importkeypair對jks文件引入系統(tǒng)簽名
d. 配置gradle(app)
8. 通過app啟動uiautomator
運行MyTest下的app,界面如下:
點擊RUN,則回到桌面,并且打開桌面上的聯(lián)系人,至此,通過apk啟動uiautomator教程完畢。