手机自动化测试:appium源码分析之bootstrap十七

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

MultiPointerGesture

package io.appium.android.bootstrap.handler;

import android.view.MotionEvent.PointerCoords;

import com.android.uiautomator.common.ReflectionUtils;

import io.appium.android.bootstrap.*;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.lang.reflect.Method;

import static io.appium.android.bootstrap.utils.API.API_18;

public class MultiPointerGesture extends CommandHandler {

private double computeLongestTime(final JSONArray actions)

throws JSONException {

double max = 0.0;

for (int i = 0; i < actions.length(); i++) {

final JSONArray gestures = actions.getJSONArray(i);

final double endTime = gestures.getJSONObject(gestures.length() - 1)

.getDouble("time");

if (endTime > max) {

max = endTime;

}

}

return max;

}

private PointerCoords createPointerCoords(final JSONObject obj)

throws JSONException {

final JSONObject o = obj.getJSONObject("touch");

final int x = o.getInt("x");

final int y = o.getInt("y");

final PointerCoords p = new PointerCoords();

p.size = 1;

p.pressure = 1;

p.x = x;

p.y = y;

return p;

}

@Override

public AndroidCommandResult execute(final AndroidCommand command)

throws JSONException {

try {

final PointerCoords[][] pcs = parsePointerCoords(command);

if (command.isElementCommand()) {

final AndroidElement el = command.getElement();

if (el.performMultiPointerGesture(pcs)) {

return getSuccessResult("OK");

} else {

return getErrorResult("Unable to perform multi pointer gesture");

}

} else {

if (API_18) {

final ReflectionUtils utils = new ReflectionUtils();

final Method pmpg = utils.getControllerMethod("performMultiPointerGesture",

PointerCoords[][].class);

final Boolean rt = (Boolean) pmpg.invoke(utils.getController(),

(Object) pcs);

if (rt) {

return getSuccessResult("OK");

} else {

return getErrorResult("Unable to perform multi pointer gesture");

}

} else {

Logger.error("Device does not support API < 18!");

return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR,

"Cannot perform multi pointer gesture on device below API level 18");

}

}

} catch (final Exception e) {

Logger.debug("Exception: " + e);

e.printStackTrace();

return new AndroidCommandResult(WDStatus.UNKNOWN_ERROR, e.getMessage());

}

}

private PointerCoords[] gesturesToPointerCoords(final double maxTime,

final JSONArray gestures) throws JSONException {

// gestures, e.g.:

// [

// {"touch":{"y":529.5,"x":120},"time":0.2},

// {"touch":{"y":529.5,"x":130},"time":0.4},

// {"touch":{"y":454.5,"x":140},"time":0.6},

// {"touch":{"y":304.5,"x":150},"time":0.8}

// ]

// From the docs:

// "Steps are injected about 5 milliseconds apart, so 100 steps may take

// around 0.5 seconds to complete."

final int steps = (int) (maxTime * 200) + 2;

final PointerCoords[] pc = new PointerCoords[steps];

int i = 1;

JSONObject current = gestures.getJSONObject(0);

double currentTime = current.getDouble("time");

double runningTime = 0.0;

final int gesturesLength = gestures.length();

for (int j = 0; j < steps; j++) {

if (runningTime > currentTime && i < gesturesLength) {

current = gestures.getJSONObject(i++);

currentTime = current.getDouble("time");

}

pc[j] = createPointerCoords(current);

runningTime += 0.005;

}

return pc;

}

private PointerCoords[][] parsePointerCoords(final AndroidCommand command)

throws JSONException {

final JSONArray actions = (org.json.JSONArray) command.params().get(

"actions");

final double time = computeLongestTime(actions);

final PointerCoords[][] pcs = new PointerCoords[actions.length()][];

for (int i = 0; i < actions.length(); i++) {

final JSONArray gestures = actions.getJSONArray(i);

pcs[i] = gesturesToPointerCoords(time, gestures);

}

return pcs;

}

}

多点触控根据你传递过来的参数决定,如果参数是一个控件元素,那么就要调用performMultiPointerGesture方法,如果参数是一系列的点,那么就要调用反射。那么具体来看看2个方法细节。

控件

performMultiPointerGesture

public boolean performMultiPointerGesture(PointerCoords[] ...touches) {

try {

if (API_18) {

// The compile-time SDK expects the wrong arguments, but the runtime

// version in the emulator is correct. So we cannot do:

//   `return el.performMultiPointerGesture(touches);`

// Instead we need to use Reflection to do it all at runtime.

Method method = this.el.getClass().getMethod("performMultiPointerGesture", PointerCoords[][].class);

Boolean rt = (Boolean)method.invoke(this.el, (Object)touches);

return rt;

} else {

Logger.error("Device does not support API < 18!");

return false;

}

} catch (final Exception e) {

Logger.error("Exception: " + e + " (" + e.getMessage() + ")");

return false;

}

}

UiObject中有直接可以调用的performMultiPointerGesture方法,为什么还要用反射呢。上面的方法里的注释是这样解释的:编译的时候sdk会认为参数是错误的,但是运行时却认为是正确的,所以只有在运行时调用才能保证正确性。反射调用的就是运行时的环境,所以它使用了反射调用了performMultiPointerGesture。

点组

在api18以上的版本中才有传点组的方法可调用,所以先判断sdk的版本。如果api在18以上,那么就要调用InteractionController..performMultiPointerGesture的方法来执行

手机自动化测试:appium源码分析之bootstrap十七的更多相关文章

  1. 手机自动化测试:appium源码分析之bootstrap三

    手机自动化测试:appium源码分析之bootstrap三   研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...

  2. 手机自动化测试:appium源码分析之bootstrap二

    手机自动化测试:appium源码分析之bootstrap二   在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...

  3. 手机自动化测试:appium源码分析之bootstrap一

    手机自动化测试:appium源码分析之bootstrap一   前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...

  4. 手机自动化测试:appium源码分析之bootstrap十六

    手机自动化测试:appium源码分析之bootstrap十六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  5. 手机自动化测试:appium源码分析之bootstrap十五

    手机自动化测试:appium源码分析之bootstrap十五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  6. 手机自动化测试:appium源码分析之bootstrap十四

    手机自动化测试:appium源码分析之bootstrap十四   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

  7. 手机自动化测试:appium源码分析之bootstrap十三

    手机自动化测试:appium源码分析之bootstrap十三   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

  8. 手机自动化测试:appium源码分析之bootstrap十一

    手机自动化测试:appium源码分析之bootstrap十一   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  9. 手机自动化测试:appium源码分析之bootstrap十二

    手机自动化测试:appium源码分析之bootstrap十二   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

随机推荐

  1. 利用AForge.NET 调用电脑摄像头进行拍照

    当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...

  2. Redis【第一篇】安装

    第一步:准备 1. 操作系统 CentOS-7-x86_64-Everything-1511 2. redis 版本 redis-3.2.8 3. 修改内核参数 有三种方式: 1)编辑/etc/sys ...

  3. 使用秘钥连接ssh

    ssh服务器搭建 通过秘钥登陆连接另外一台虚拟机 创建证书  ssh-keygen -t rsa 第一行密钥保存位置直接输入回车 确定默认创建位置为 /root/.ssh 公钥必须改名为  autho ...

  4. iOS基础之顺传逆传传值(delegate、block)

    写给iOS新手的福利! 在项目中经常会用到传值,根据传值的方向分为顺传(从根控制器到子控制器)和逆传(从子控制器到根控制器).在这里写了个Demo简单演示了效果,创建了两个控制器: 一个为根控制器,一 ...

  5. NDK 开发(Android.mk配置)

         在我写这篇文章的时候,Android Studio已经是2.3版本了,已经集成CMake 编译工具, 用户只需在 新建项目的时候,添加选项(Include C++ support),Andr ...

  6. Java并发之线程异常捕获

    由于线程的本质特性,使得你不能捕获从线程中逃逸的异常,如: import java.util.concurrent.ExecutorService; import java.util.concurre ...

  7. HTML入门第二天

    一. URL url:统一资源定位符 组成: 协议://域名:端口号/文件?参数名1=值1&参数名2=值2 例子:http://www.163.com:80/index.html?userna ...

  8. ACM 子串和

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...

  9. [Spark] - SparkCore程序优化总结

    http://spark.apache.org/docs/1.6.1/tuning.html1) 代码优化 a. 对于多次使用的RDD,进行数据持久化操作(eg: cache.persist) b. ...

  10. sys模块和Python常用的内建函数

    1.sys模块 当Python执行import sys语句的时候,它在sys.path变量中所列目录中寻找sys.py模块.如果找到了这个文件,这个模块的主块中的语句将被运行,然后这个模块将能够被使用 ...