APP自动化框架-ATX原理解析及JAVA版客户端
作为网易开源的ATX APP自动化测试框架,对比现有的macaca自动化框架/Appium自动化框架,最大的特别就是在于可远程进行自动化测试
先给大家看一张我自己梳理的框架架构图
框架巧妙点:
1. 使用golang作为server端运行在Android手机上,免root运行
2. AutomatorHttpService使用NanoHTTPD框架,也自己运行一个server,专门监听及处理过来的http jsonRpc请求
public class AutomatorHttpServer extends NanoHTTPD { public AutomatorHttpServer(int port) {
super(port);
} private Map<String, JsonRpcServer> router = new HashMap<String, JsonRpcServer>(); public void route(String uri, JsonRpcServer rpc) {
router.put(uri, rpc);
} @Override
public Response serve(String uri, Method method,
Map<String, String> headers, Map<String, String> params,
Map<String, String> files) {
Log.d(String.format("URI: %s, Method: %s, params, %s, files: %s", uri, method, params, files)); if ("/stop".equals(uri)) {
stop();
return newFixedLengthResponse("Server stopped!!!");
} else if ("/ping".equals(uri)) {
return newFixedLengthResponse("pong");
} else if ("/screenshot/0".equals(uri)) {
float scale = 1.0f;
if (params.containsKey("scale")) {
try {
scale = Float.parseFloat(params.get("scale"));
} catch (NumberFormatException e) {
}
}
int quality = 100;
if (params.containsKey("quality")) {
try {
quality = Integer.parseInt(params.get("quality"));
} catch (NumberFormatException e) {
}
}
File f = new File(InstrumentationRegistry.getTargetContext().getFilesDir(), "screenshot.png");
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).takeScreenshot(f, scale, quality); try {
return newChunkedResponse(Response.Status.OK, "image/png", new FileInputStream(f));
} catch (FileNotFoundException e) {
Log.e(e.getMessage());
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!");
}
} else if (router.containsKey(uri)) {
JsonRpcServer jsonRpcServer = router.get(uri);
ByteArrayInputStream is = null;
if (params.get("NanoHttpd.QUERY_STRING") != null)
is = new ByteArrayInputStream(params.get("NanoHttpd.QUERY_STRING").getBytes());
else if (files.get("postData") != null)
is = new ByteArrayInputStream(files.get("postData").getBytes());
else
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Invalid http post data!");
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
jsonRpcServer.handleRequest(is, os);
return newFixedLengthResponse(Response.Status.OK, "application/json", new ByteArrayInputStream(os.toByteArray()), os.size());
} catch (IOException e) {
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!");
}
} else
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found!!!");
} }
3. 使用jsonRpc反射反射形式对外提供 uiautomator方式
package com.github.uiautomator.stub; import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until; import com.fasterxml.jackson.databind.ObjectMapper;
import com.googlecode.jsonrpc4j.JsonRpcServer; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith; /**
* Use JUnit test to start the uiautomator jsonrpc server.
*
* @author xiaocong@gmail.com
*/
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Stub {
private final String TAG = "UIAUTOMATOR";
private static final int LAUNCH_TIMEOUT = 5000; int PORT = 9008;
AutomatorHttpServer server = new AutomatorHttpServer(PORT); @Before
public void setUp() throws Exception {
launchService();
//这是关键核心代码,把AutomatorService使用jsonRpcServer进行反射处理
server.route("/jsonrpc/0", new JsonRpcServer(new ObjectMapper(), new AutomatorServiceImpl(), AutomatorService.class));
server.start();
} private void launchPackage(String packageName) {
Log.i(TAG, "Launch " + packageName);
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(packageName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent); device.wait(Until.hasObject(By.pkg(packageName).depth(0)), LAUNCH_TIMEOUT);
device.pressHome();
} private void launchService() throws RemoteException {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
Context context = InstrumentationRegistry.getContext();
device.wakeUp(); // Wait for launcher
String launcherPackage = device.getLauncherPackageName();
Boolean ready = device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
if (!ready) {
Log.i(TAG, "Wait for launcher timeout");
return;
} Log.d("Launch service");
context.startService(new Intent("com.github.uiautomator.ACTION_START")); // Reset Configurator Wait Timeout
Configurator configurator = Configurator.getInstance();
configurator.setWaitForSelectorTimeout(0L); // BUG(uiautomator): setWaitForIdleTimeout is useless
// Refs: https://www.ydkf.me/archives/22
} @After
public void tearDown() {
server.stop();
Context context = InstrumentationRegistry.getContext();
context.startService(new Intent("com.github.uiautomator.ACTION_STOP"));
} @Test
@LargeTest
public void testUIAutomatorStub() throws InterruptedException {
while (server.isAlive()) {
Thread.sleep(100);
}
}
}
4. AutomatorServiceImpl把原生UiAutomation加了一定处理,重写了一遍,只要确保入参数保持一致
@Override
public boolean click(int x, int y) {
return device.click(x, y);
}
@Override
public boolean drag(int startX, int startY, int endX, int endY, int steps) throws NotImplementedException {
return device.drag(startX, startY, endX, endY, steps);
}
从整体而言,代码简洁、可读性、代码解耦,在ATX上提现较为明显
附上我这边写的java版ATX客户端,原框架只提供了python版
https://github.com/tigerge000/atxuiautomatorclient
APP自动化框架-ATX原理解析及JAVA版客户端的更多相关文章
- App 自动化框架设计思路
最近在整理和学习Appium+Java 自动化框架,对APP自动化框架的部分设想参考了一些文章,先进行整理下: 框架的思路一: 思考引入:https://www.cnblogs.com/yunfeio ...
- APP自动化框架LazyAndroid使用手册(2)--元素自动抓取
作者:黄书力 概述 前面的一篇博文简要介绍了安卓自动化测试框架LazyAndroid的组成结构和基本功能,本文将详细描述此框架中元素自动抓取工具lazy-uiautomaterviewer的使用方法. ...
- Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net
Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net 1. 提升单例有能力的1 2. 减少工作数量2 2.1. 减少距 ...
- 【转】URL短地址压缩算法 微博短地址原理解析 (Java实现)
转自: URL短地址压缩算法 微博短地址原理解析 (Java实现) 最近,项目中需要用到短网址(ShortUrl)的算法,于是在网上搜索一番,发现有C#的算法,有.Net的算法,有PHP的算法,就是没 ...
- [置顶]
滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理
上周末,滴滴与360都开源了各自的插件化框架,VirtualAPK与RePlugin,作为一个插件化方面的狂热研究者,在周末就迫不及待的下载了Virtualapk框架来进行研究,本篇博客带来的是Vir ...
- Android中免root的hook框架Legend原理解析
一.前言 Android中hook框架已经非常多了,最优秀的当属Xposed和Substrate了,这两个框架我在之前的文章都详细介绍过了,不了解的同学,可以转战这里:http://www.wjdia ...
- APP自动化框架LazyAndroid使用手册(1)--框架简介
作者:cryanimal QQ:164166060 APP自动化简介 APP自动化,即通过自动化的方式,对APP施行一系列的仿按键输入.触摸屏输入.手势输入等操作,以达到对APP的功能进行自动化测试 ...
- URL短地址压缩算法 微博短地址原理解析 (Java实现)
原博客地址:http://blog.csdn.net/xyz_lmn/article/details/8057270 最近,项目中需要用到短网址(ShortUrl)的算法,于是在网上搜索一番,发现有C ...
- APP自动化框架LazyAndroid使用手册(4)--测试模板工程详解
概述 前面的3篇博文分别对lazyAndroid的框架简介.元素抓取和核心API进行了说明,本文将基于框架给出的测试模板工程,详细阐述下使用该框架进行安卓UI自动化测试的步骤. 模板工程 先来看一下模 ...
随机推荐
- 基于springboot的ssm
参考该网址成功搭建: https://blog.csdn.net/liboyang71/article/details/73459909 目前有几个问题: 1.我使用application.yml配置 ...
- 51nod-1627 瞬间移动(组合数+逆元)
题目描述: 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几种方案,答案对10 ...
- JAVA写接口傻瓜(!)教程(二)
接上一篇 3.数据库SQL sever配置 理论上来说,java web项目连接什么数据库都行,但是我电脑上SQL sever用了三年了,各种操作都比较熟悉,所以也懒得再用My SQL.想用My SQ ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- Python数据类型——字符串
概论 字符串顾名思义就是一串字符,由于Python中没有“字符”这种数据类型,所以单个的字符也依然是字符串类型的.字符串可以包含一切数据,无论是能从键盘上找到的,还是你根本都不认识的.与数一样,字符串 ...
- es6(一)
一.let和const: let :块作用域,不能重复声明. const:块作用域,声明的时候必须赋值,声明的值类型不能修改,引用类型由于是指针,所以可以修改. 二.解构赋值: 左边一种结构,右边一种 ...
- asp.net IHttpModule浅析
在asp.net程序中,我们除了使用系统自带的HttpModule模块,还可以自己定义一些自己的HttpModule接口模块.一个asp.net程序可以接收多个HttpModule模块. 众所周知,a ...
- 根据不同访问设备跳转到PC页面或手机页面
目前很多网站都是采用了响应式自适应页面的设计了,根据访问设备的不同,显示不同的内容.但是还是会有一些节奏比较慢的网站,还是PC页面和手机PAD页面不同的访问域名.正好我这里有个需要,同一个域名要根据不 ...
- boostrap 日期插件(带中文显示)
不知道大家用什么样的日期插件,我最近用了bootstrap插件,整理了下,分享给大家,第四部分是我找的插件源码,可以省去大家的找的时间,按1-3的步骤用就好了,有些样式上的小问题就自己调一调: (1) ...
- Android 面试100问- 0序0
准备找android方面的工作,现收集面试题,打算收集100个并记录