手机自动化测试:appium源码分析之bootstrap五
手机自动化测试:appium源码分析之bootstrap五
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。poptest测试开发工程师就业培训请大家咨询qq:908821478)移动端自动化测试是未来的测试工程师的技术要求,我们需要打好基础。
Swipe代码:
package io.appium.android.bootstrap.handler;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;
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 swipe.
*
*/
public class Swipe extends CommandHandler {
/*
* @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 {
final Hashtable<String, Object> params = command.params();
final Point start = new Point(params.get("startX"), params.get("startY"));
final Point end = new Point(params.get("endX"), params.get("endY"));
final Integer steps = (Integer) params.get("steps");
final UiDevice device = UiDevice.getInstance();
Point absStartPos = new Point();
Point absEndPos = new Point();
if (command.isElementCommand()) {
try {
final AndroidElement el = command.getElement();
absStartPos = el.getAbsolutePosition(start);
absEndPos = el.getAbsolutePosition(end, false);
} catch (final UiObjectNotFoundException e) {
return getErrorResult(e.getMessage());
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
} catch (final Exception e) { // handle NullPointerException
return getErrorResult("Unknown error");
}
} else {
try {
absStartPos = getDeviceAbsPos(start);
absEndPos = getDeviceAbsPos(end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
}
}
Logger.debug("Swiping from " + absStartPos.toString() + " to "
+ absEndPos.toString() + " with steps: " + steps.toString());
final boolean rv = device.swipe(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), steps);
if (!rv) {
return getErrorResult("The swipe did not complete successfully");
}
return getSuccessResult(rv);
}
}
先分析源码:
final Hashtable<String, Object> params = command.params();
final Point start = new Point(params.get("startX"), params.get("startY"));
final Point end = new Point(params.get("endX"), params.get("endY"));
final Integer steps = (Integer) params.get("steps");
final UiDevice device = UiDevice.getInstance();
Point absStartPos = new Point();
Point absEndPos = new Point();
首先从命令里获取参数,后解析出3个变量:起始点start、终点end、步骤steps。然后获得设备对象,定义2个私有Point对象,以备后用。
然后分条件处理,处理控件还是处理坐标。
控件:
final AndroidElement el = command.getElement();
absStartPos = el.getAbsolutePosition(start);
absEndPos = el.getAbsolutePosition(end, false);
首先获取控件对象,再通过getAbsolutePosition传入不同的参数获得在该控件上点击的起始点和结束点。
public Point getAbsolutePosition(final Point point,
final boolean boundsChecking) throws UiObjectNotFoundException,
InvalidCoordinatesException {
final Rect rect = el.getBounds();
final Point pos = new Point();
Logger.debug("Element bounds: " + rect.toShortString());
if (point.x == 0) {
pos.x = rect.width() * 0.5 + rect.left;
} else if (point.x <= 1) {
pos.x = rect.width() * point.x + rect.left;
} else {
pos.x = rect.left + point.x;
}
if (boundsChecking) {
if (pos.x > rect.right || pos.x < rect.left) {
throw new InvalidCoordinatesException("X coordinate ("
+ pos.x.toString() + " is outside of element rect: "
+ rect.toShortString());
}
}
if (point.y == 0) {
pos.y = rect.height() * 0.5 + rect.top;
} else if (point.y <= 1) {
pos.y = rect.height() * point.y + rect.top;
} else {
pos.y = rect.left + point.y;
}
if (boundsChecking) {
if (pos.y > rect.bottom || pos.y < rect.top) {
throw new InvalidCoordinatesException("Y coordinate ("
+ pos.y.toString() + " is outside of element rect: "
+ rect.toShortString());
}
}
return pos;
}
上面的代码首先分析x坐标然后分析y坐标。x和y坐标的判断和处理时一样的
首先判断x坐标是否为0,如果为0,定义初始点的x坐标为控件的中心点的横坐标。如果x的坐标小于1,说明坐标为相对坐标,用百分比来求值,此时就要与宽度做乘积运算得到具体值。如果上面2种情况都不符合,那就是具体坐标值,那就直接元素的x坐标值加上控件的边框左坐标值。
最后根据传入的boolean值来判断是否做一个超出边界的验证。如果超出边界就跑出异常。y坐标的获取方式类似。最后得到坐标值并返回,回到execute方法中。
坐标
absStartPos = getDeviceAbsPos(start);
absEndPos = getDeviceAbsPos(end);
通过调用getDeviceAbsPos()方法得到坐标值来初始化之前声明的私有Point对象.
protected static Point getDeviceAbsPos(final Point point)
throws InvalidCoordinatesException {
final UiDevice d = UiDevice.getInstance();
final Point retPos = new Point(point); // copy inputed point
final Double width = (double) d.getDisplayWidth();
if (point.x < 1) {
retPos.x = width * point.x;
}
if (retPos.x > width || retPos.x < 0) {
throw new InvalidCoordinatesException("X coordinate ("
+ retPos.x.toString() + " is outside of screen width: "
+ width.toString());
}
final Double height = (double) d.getDisplayHeight();
if (point.y < 1) {
retPos.y = height * point.y;
}
if (retPos.y > height || retPos.y < 0) {
throw new InvalidCoordinatesException("Y coordinate ("
+ retPos.y.toString() + " is outside of screen height: "
+ height.toString());
}
return retPos;
}
类似于上面的方法,也是要先判断传过来的坐标值是否小于1,如果小于1,当作百分比来球坐标值。如果超出屏幕的范围抛出异常,最后返回坐标值回到execute方法。
final boolean rv = device.swipe(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), steps);
最后调用UiDevice.swipe方法来执行命令,判断是否执行成功。
总结
执行swipe命令有2中命令格式
a.控件
b.坐标
坐标又分为相对坐标百分比和绝对坐标两种方法。
手机自动化测试: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是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
随机推荐
- 继BAT之后 第四大巨头是谁
中国互联网三大巨头的位置,毫无疑问是属于百度腾讯阿里的,但在它们之后,哪家公司能进巨头之列?京东布局不错,走亚马逊路线:360同时占据传统和移动互联网两大领域入口:小米软硬整合,生态系统完整. 很多人 ...
- 项目中通过Sorlj获取索引库中的数据
在开发项目中通过使用Solr所提供的Solrj(java客户端)获取索引库中的数据,这才是真正对项目起实质性作用的功能,提升平台的检索性能及检索结果的精确性 第一步,引入相关依赖的jar包 第二步,根 ...
- linux-网络数据包抓取-tcpdump
用法格式: tcpdump [-i 网卡] [选项] '表达式' 选项说明如下: -i:interface 监听的网卡. -nn:表示以ip和port的方式显示来源主机和目的主机,而不是用主机名和 ...
- 前端学PHP之日期与时间
前面的话 在Web程序开发时,时间发挥着重要的作用,不仅在数据存储和显示时需要日期和时间的参与,好多功能模块的开发,时间通常都是至关重要的.网页静态化需要判断缓存时间.页面访问消耗的时间需要计算.根据 ...
- 每天一个linux命令(47)--scp命令
scp是secure copy 的简写,用于在Linux 下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝而不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当 ...
- 关于adb重启的一些问题
有时候我们在使用eclipse启动虚拟机进行程序测试的时候会提示,要我们重启adb和eclipse,这个时候,重启adb的方式就是,使用cmd定位到adb所在的文件夹,然后输入指令:adb kill- ...
- mybatis系列笔记(2)---mapper代理方法
mapper代理方法 在我们在写MVC设计的时候,都会写dao层和daoimp实现层,但假如我们使用mapper代理的方法,我们就可以不用先daoimp实现类 当然这得需要遵守一些相应的规则: (1) ...
- 代写编程的作业、笔试题、课程设计,包括但不限于C/C++/Python
代写编程作业/笔试题/课程设计,包括但不限于C/C++/Python 先写代码再给钱,不要任何定金!价钱公道,具体见图,诚信第一! (截止2016-11-22已接12单,顺利完成!后文有成功交付的聊天 ...
- 实战Tomcat配置SSL,使用openssl制作证书
制作证书以及Tomcat配置 搭建openssl环境,下载openssl并设置环境变量方便命令行的使用: 修改openssl配置文件,设置dir目录,如设置dir=e:/temp/openssl_ca ...
- Java并发之需要了解但不能太依赖的东东
<Java 编程思想>在并发一章中提到了Sleep休眠.优先级.后台线程,提醒读者需要了解但又不能太依赖他们.就让我们一起看看吧. 休眠Sleep import java.util.con ...