uiautomator2.0+脱离PC运行(apk启动uiautomator2.0+)的实现方案
来源:互联网 发布:中铁物资西南公司知乎 编辑:程序博客网 时间:2024/05/16 11:59
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>
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; } }}
其中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; }}
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" }
- 在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("联系人")); x.click(); }}
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">
b. 生成js文件
c. 使用keytool-importkeypair对jks文件引入系统签名
d. 配置gradle(app)
8. 通过app启动uiautomator
运行MyTest下的app,界面如下:
点击RUN,则回到桌面,并且打开桌面上的联系人,至此,通过apk启动uiautomator教程完毕。
- uiautomator2.0+脱离PC运行(apk启动uiautomator2.0+)的实现方案
- UiAutomator2.0
- UIAutomator2.0 简介
- UiAutomator2.0资料收集
- Uiautomator2.0+junit4
- UiAutomator2.0升级填坑记
- UiAutomator2.0升级填坑记
- UiAutomator2
- android stuido 上uiautomator2.0的使用
- UiAutomator2.0环境搭建(一)
- UIAutomator2.0详解(入门篇)
- UIAutomator2.0详解(JUnit Annotation篇)
- UIAutomator2.0详解(IDE View篇)
- UIAutomator2.0详解(UIDevice篇---- 序)
- UIAutomator2.0详解(UIDevice篇----Wait)
- UIAutomator2.0详解(UIDevice篇----waitForIdle)
- UIAutomator2.0详解(UIDevice篇----waitForWindowUpdate)
- UIAutomator2.0详解(UIDevice篇----Hierarchy)
- Java动态调整数组大小
- Fragment生命周期
- mongodb安装
- spring配置中,properties文件以及xml文件配置问题
- hibernate查询
- uiautomator2.0+脱离PC运行(apk启动uiautomator2.0+)的实现方案
- emacs 使用 etags 阅读代码
- Android中简单的des加密
- Swift - 从应用中跳转到App Store页面,并进行评论打分
- Linux学习33_CURL常用命令
- 130.PHP的语言结构和函数的区别
- Info.plist 国际化
- linux 系统操作高阶命令
- [spring学习之一]自动装配