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

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。poptest测试开发工程师就业培训请大家咨询qq:908821478)移动端自动化测试是未来的测试工程师的技术要求,我们需要打好基础。

Flick

package io.appium.android.bootstrap.handler;

import com.android.uiautomator.core.UiDevice;

import io.appium.android.bootstrap.*;

import io.appium.android.bootstrap.exceptions.InvalidCoordinatesException;

import io.appium.android.bootstrap.utils.Point;

import org.json.JSONException;

import java.util.Hashtable;

/**

* This handler is used to flick elements in the Android UI.

*

* Based on the element Id, flick that element.

*

*/

public class Flick extends CommandHandler {

private Point calculateEndPoint(final Point start, final Integer xSpeed,

final Integer ySpeed) {

final UiDevice d = UiDevice.getInstance();

final Point end = new Point();

final double speedRatio = (double) xSpeed / ySpeed;

double xOff;

double yOff;

final double value = Math.min(d.getDisplayHeight(), d.getDisplayWidth());

if (speedRatio < 1) {

yOff = value / 4;

xOff = value / 4 * speedRatio;

} else {

xOff = value / 4;

yOff = value / 4 / speedRatio;

}

xOff = Integer.signum(xSpeed) * xOff;

yOff = Integer.signum(ySpeed) * yOff;

end.x = start.x + xOff;

end.y = start.y + yOff;

return end;

}

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @return {@link AndroidCommandResult}

*

* @throws JSONException

*

* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

* bootstrap.AndroidCommand)

*/

@Override

public AndroidCommandResult execute(final AndroidCommand command)

throws JSONException {

Point start = new Point(0.5, 0.5);

Point end = new Point();

Double steps;

final Hashtable<String, Object> params = command.params();

final UiDevice d = UiDevice.getInstance();

if (command.isElementCommand()) {

AndroidElement el;

try {

el = command.getElement();

start = el.getAbsolutePosition(start);

final Integer xoffset = (Integer) params.get("xoffset");

final Integer yoffset = (Integer) params.get("yoffset");

final Integer speed = (Integer) params.get("speed");

steps = 1250.0 / speed + 1;

end.x = start.x + xoffset;

end.y = start.y + yoffset;

} catch (final Exception e) {

return getErrorResult(e.getMessage());

}

} else {

try {

final Integer xSpeed = (Integer) params.get("xSpeed");

final Integer ySpeed = (Integer) params.get("ySpeed");

final Double speed = Math.min(1250.0,

Math.sqrt(xSpeed * xSpeed + ySpeed * ySpeed));

steps = 1250.0 / speed + 1;

start = getDeviceAbsPos(start);

end = calculateEndPoint(start, xSpeed, ySpeed);

} catch (final InvalidCoordinatesException e) {

return getErrorResult(e.getMessage());

}

}

steps = Math.abs(steps);

Logger.debug("Flicking from " + start.toString() + " to " + end.toString()

+ " with steps: " + steps.intValue());

final boolean res = d.swipe(start.x.intValue(), start.y.intValue(),

end.x.intValue(), end.y.intValue(), steps.intValue());

if (res) {

return getSuccessResult(res);

} else {

return getErrorResult("Flick did not complete successfully");

}

}

}

代码的步骤和swipe类似,而且最终调用的也是UiDevice.swipe方法,那么我们来看看到底区别在什么地方。首先它也分控件和坐标,分别分析:

控件

首先将控件的中心点作为起始坐标,然后根据提供的参数xoffset和yoffset来获取位移数据,speed参数用来计算步骤。

steps = 1250.0 / speed + 1;
end.x = start.x + xoffset;
end.y = start.y + yoffset;

起始坐标加上位移就是结束坐标,这个steps的设置还是有点让人摸不着头脑的,我这个1250我且认为是最大位移吧,speed代表每一步走的路程。用1250/speed得到使用多少步到结束点,再加上初始值的那个点就得到steps的值啦。至此起始点坐标、结束点坐标、步骤的值都设置完毕。

坐标

严格来说,不能说成坐标,应该算坐标位移,因为才传入的参数其实坐标系的速度xSpeed和ySpeed。x轴移动xSpeed距离,y轴移动ySpeed坐标。然后获取坐标值和steps值。
其中它用1250和位移的平方做了一次比较,取出最小值来计算steps。起始坐标点定位屏幕的中心点坐标。然后再调用end = calculateEndPoint(start, xSpeed, ySpeed);方法获取结束点坐标。

private Point calculateEndPoint(final Point start, final Integer xSpeed,

final Integer ySpeed) {

final UiDevice d = UiDevice.getInstance();

final Point end = new Point();

final double speedRatio = (double) xSpeed / ySpeed;

double xOff;

double yOff;

final double value = Math.min(d.getDisplayHeight(), d.getDisplayWidth());

if (speedRatio < 1) {

yOff = value / 4;

xOff = value / 4 * speedRatio;

} else {

xOff = value / 4;

yOff = value / 4 / speedRatio;

}

xOff = Integer.signum(xSpeed) * xOff;

yOff = Integer.signum(ySpeed) * yOff;

end.x = start.x + xOff;

end.y = start.y + yOff;

return end;

}

首先计算位移比speedRatio(x的位移/y的位移),然后获取屏幕宽和高中最小的一个数复制给value.如果speedRatio<1,x的移动距离为value的值的1/4.y坐标为和x移动的距离是一样的。所以经过上面的计算得到的结束点的坐标和起始点组成的应该是正方型的对角线。

最后调用UiDevice.swipe和Swipe中是一样的啦。没什么特别的

手机自动化测试: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是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

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

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

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

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

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

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

随机推荐

  1. 1305 Pairwise Sum and Divide

    1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对 ...

  2. [译]如何在Web开发中使用Python

    [译]如何在Web开发中使用Python 原文:HOWTO Use Python in the Web 摘要 这篇文档展示了Python如何融入到web中.它介绍了几种Python结合web服务器的方 ...

  3. .Net Standard扩展支持实例分享

    上篇(.Net基础体系和跨框架开发普及)介绍了.Net当前生态下的大概情况,也分享了简单实现的过程,这篇文章就是讲解我的OSS.Common项目扩展.Net Standard 支持的过程,主要集中在: ...

  4. 第22篇 js中的this指针的用法

    前面把js的相关知识总结了下,今天把js中的上下文的this,对于强类型语言,this的用法非常的单一,因为他们没有js特有的动态绑定. 首先看下面代码: function funcA() { thi ...

  5. AlloyFinger.js 源码 学习笔记及原理说明

    此手势库利用了手机端touchstart, touchmove, touchend, touchcancel原生事件模拟出了 rotate  touchStart  multipointStart   ...

  6. (五)CSS和JavaScript基础

    DHTML :制作动态HTML页面的技术 DHTML=HTML+层叠样式表CSS+脚本语言javascript 一.CSS 1.1 CSS样式的分类: 行内样式:只影响一行,其他相同标签也不影响.如下 ...

  7. android sdk 深入理解adb

    adb 服务器-客户端程序包括3部分 1.客户端(client) 在开发机器上运行,可通过adb命令行呼叫客户端,ADT插件和DDMS同样需要adb客户端   2.服务端(server) 在开发机器上 ...

  8. KoaHub平台基于Node.js开发的Koa的skip插件代码详情

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  9. Spring注解问题,[action中注入service失败

    pring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  10. 3-14 JS基础知识01

    JavaScript的组成: JS特点:JS是一门 脚本语言:不需要编译编译:把代码转化成计算机所认知的二进制语言.JS是一门弱类型语言:声明变量都用varJS是一种动态语言:认知当前的着这个变量到底 ...