首页 > 代码库 > [uiautomator篇] apk 允许uiautomator用例
[uiautomator篇] apk 允许uiautomator用例
http://blog.csdn.net/cxq234843654/article/details/52605441
uiautomator2.0+脱离PC运行(apk启动uiautomator2.0+)的实现方案
效果:
打开MyTest.apk,点击run uiautomator,就能直接运行你的脚本。
方案概述:
-
新建一个Android app工程MyTest,在Activity中添加Button,用于启动脚本
-
给这个app添加系统签名
-
在MyTest中新建一个module,命名为MyTestCase,用于编写脚本
-
使用
am instrument
命令实现脚本的运行
1. 新建一个Android应用,命名为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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
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);
}
/**
* 点击按钮对应的方法
* @param v
*/
public void runMyUiautomator(View v){
Log.i(TAG, "runMyUiautomator: ");
new UiautomatorThread().start();
}
/**
* 运行uiautomator是个费时的操作,不应该放在主线程,因此另起一个线程运行
*/
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;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
其中CMDUtils.Java内容如下:
package cxq.com.mytest;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 执行命令
*/
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;
}
}
/**
* 执行命令
*
* @param command 命令
* @param isShowCommand 是否显示执行的命令
* @param isNeedResultMsg 是否反馈执行的结果
* @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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
4. 新建一个Module,用于写你的测试脚本
- 在Android Studio的左侧栏,右击“New – Module – Phone&Table Module”,并填写如下信息
next之后,选择add no activity – finish
5. 修改mytestcase模块的gradle文件
- 修改默认的runner,在defaultConfig中添加runner
defaultConfig {
applicationId "cxq.com.mytestcast"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 在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‘
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
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("联系人"));
x.click();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
Case说明:
@RunWith注解,代表使用什么runner
@Test注解,表示当前的方法为测试方法,同理还有其他的@Before等注解,具体case的写法本文不赘述
运行一下你的case,测试是否有效,运行方法:点击带有Test注解的方法左侧绿色三角形
此时,run窗口会有如下信息:
如果你只是急于达到效果,而不注重原理,可以跳过原理分析的阅读
原理分析:
可以看到,窗口中其实运行了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到手机 -
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是怎么把用例跑起来的,仿照这个原理就可以自己实现通过apk调用uiautomator用例,只要让app中的button的响应事件去执行am instrument
命令即可,但是由于执行这个命令也是需要权限的,因此需要给app添加系统签名
7. 给APP添加系统签名
大致过程如下(具体的细节可以参照我之前写过的文章Android Studio自动生成带系统签名的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">
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
b. 生成js文件
c. 使用keytool-importkeypair对jks文件引入系统签名
d. 配置gradle(app)
8. 通过app启动uiautomator
运行MyTest下的app,界面如下:
点击RUN,则回到桌面,并且打开桌面上的联系人,至此,通过apk启动uiautomator教程完毕。
[uiautomator篇] apk 允许uiautomator用例