手机自动化测试: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. linux下php开发环境搭建(nginx+php+mysql)

    安装前准备工作 先安装一些必要的类库 yum install -y wget  zlib-devel bzip2-devel  curl-devel openssl openssl-devel vim ...

  2. 剑指offer编程题Java实现——面试题5从头到尾打印链表

    题目描述* 剑指offer面试题5:从尾到头打印链表 输入一个链表的头结点,从尾到头打印出每个结点的值 解决方案一:首先遍历链表的节点后打印,典型的"后进先出",可以使用栈来实现这 ...

  3. Python从入门到放弃之迭代器

    迭代器是Python2.1中新加入的接口(PEP 234),说明如下: The iterator provides a 'get next value' operation that produces ...

  4. iOS 访问URL转码

    访问URL时,需要对字符串进行转码: urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; ...

  5. JS中的类,类的继承方法

    大牛请无视此篇! 首先我们定义一个类,方法很简单,就像我们定义函数一样,只不过我们为了与函数区分,名称首字母要大写,看代码: function Person (){ } 这就是一个很简单的Poson类 ...

  6. 云服务器spark集群搭建

    ---恢复内容开始--- 1:去官网下载spark http://spark.apache.org 2:解压,然后在自己的机器上编译conf中的两个文件 mv slaves.template slav ...

  7. 在Windows平台搭建轻巧的Python开发环境——面向工程和科研的扩展包配置

    首先,下载最新版本的Python. 为什么强调最新版本呢,因为新版本的漏洞通常会少得多,而且反映了未来的趋势. 既然要学,何不起点高一点? 官方下载地址:https://www.python.org/ ...

  8. MDX 用Ancestors得到Hierarchy中指定Level的值(附带SCOPE用法之一)

    需求:用户想要用Excel,对比每月预算和整年预算,需要在两个用户定义的Hierarchy都可以浏览.财年季月日(FYQMD)和财年月日(FYMD). 自定义hierarchy 属性关系(Attrib ...

  9. jquery与js的区别与基础操作

    一.什么是 jQuery jQuery是一个JavaScript库,它通过封装原生的JavaScript函数得到一整套定义好的方法.它的作者是John Resig,于2006年创建的一个开源项目,随着 ...

  10. .NET 发布网站步骤

    本文章分为三个部分: web网站发布.IIS6 安装方法.ASP.NET v4.0 安装方法 一.web网站发布 1.打开 Visual Studio 2013 编译环境 2.在其解决方案上右击弹出重 ...