手机自动化测试:appium源码分析之bootstrap六
手机自动化测试: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六的更多相关文章
- 手机自动化测试:appium源码分析之bootstrap三
手机自动化测试:appium源码分析之bootstrap三 研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...
- 手机自动化测试:appium源码分析之bootstrap二
手机自动化测试:appium源码分析之bootstrap二 在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...
- 手机自动化测试:appium源码分析之bootstrap一
手机自动化测试:appium源码分析之bootstrap一 前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...
- 手机自动化测试:appium源码分析之bootstrap十七
手机自动化测试:appium源码分析之bootstrap十七 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
- 手机自动化测试:appium源码分析之bootstrap十六
手机自动化测试:appium源码分析之bootstrap十六 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
- 手机自动化测试:appium源码分析之bootstrap十五
手机自动化测试:appium源码分析之bootstrap十五 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
- 手机自动化测试:appium源码分析之bootstrap十四
手机自动化测试:appium源码分析之bootstrap十四 poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...
- 手机自动化测试:appium源码分析之bootstrap十三
手机自动化测试:appium源码分析之bootstrap十三 poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...
- 手机自动化测试:appium源码分析之bootstrap十一
手机自动化测试:appium源码分析之bootstrap十一 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
随机推荐
- js设计模式--工厂模式
工厂模式: 工厂模式的目的是为了创建对象,它经常是在类和类的方法中实现.简单的工厂模式是由一个方法来决定到底要创建哪类的实例,这些实例经常拥有相同的接口,这种模式在所实例化的类型在编译期并不确定,而是 ...
- redux 初识
前言 Redux 试图让 state 的变化变得可预测 react/react-native 将组件的更新交给了状态机(state),想要更新页面活着页面的某个组件就必须得通过改变state的方式.页 ...
- Humble Numbers(丑数) 超详解!
给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑 ...
- 禁止linux被ping
cho "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf sysctl -p 生效 开启ping功能: 删除/ ...
- sql server中部分函数功能详解
1.TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. SQL Server 的语法: SELECT TOP number|percen ...
- 兜转数年,老跳成了卖过软件开过店写过APP的电脑老师
老跳(因为性格太跳,被大家公认的外号),87年生,湖南人,上篇中老赵的大学下铺兄弟. 2008年大学毕业两人一同去的广州,住在求职公寓里找工作. 老赵找工作找了半个月,老跳却在到广州的第二天就开始上班 ...
- 【2017-02-24】循环嵌套、跳转语句、异常语句、迭代穷举、while
一.循环嵌套 1.格式 for() { for() { } } 2.执行顺序 先执行外边循环进到循环体发现里面的循环,开始执行里面的循环.等到里面的循环执行完毕,再执行外边的循环. 在外面循环第一次, ...
- 给 Qt 添加模块
添加 Qt 模块 QtCanvas3D 由于需要使用 Qt Quick 进行 3D 绘图,因此在网上找了一些资料. JS 绘制 3D 的有 ThreeJS 库,应该可以用于 QML.继续搜索,发现Qt ...
- css 超出隐藏显示...
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- PTVS在Visual Studio中的安装
下载链接,点这里 PTVS是VS下的python开发插件 1.下载完成后,双击运行,安装完毕 2.解释脚本:打开VS,找到文件-新建-项目,在新建项目页面的左侧树形菜单的已安装->模板-> ...