手机自动化测试:appium源码分析之bootstrap七
手机自动化测试:appium源码分析之bootstrap七
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。poptest测试开发工程师就业培训请大家咨询qq:908821478)移动端自动化测试是未来的测试工程师的技术要求,我们需要打好基础。
Drag
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 org.json.JSONObject;
import java.util.Hashtable;
/**
* This handler is used to drag in the Android UI.
*
*/
public class Drag 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)
*/
private static class DragArguments {
public AndroidElement el;
public AndroidElement destEl;
public final Point start;
public final Point end;
public final Integer steps;
public DragArguments(final AndroidCommand command) throws JSONException {
final Hashtable<String, Object> params = command.params();
try {
if (params.get("elementId") != JSONObject.NULL) {
el = command.getElement();
}
} catch (final Exception e) {
el = null;
}
try {
if (params.get("destElId") != JSONObject.NULL) {
destEl = command.getDestElement();
}
} catch (final Exception e) {
destEl = null;
}
start = new Point(params.get("startX"), params.get("startY"));
end = new Point(params.get("endX"), params.get("endY"));
steps = (Integer) params.get("steps");
}
}
private AndroidCommandResult drag(final DragArguments dragArgs) {
Point absStartPos = new Point();
Point absEndPos = new Point();
final UiDevice device = UiDevice.getInstance();
try {
absStartPos = getDeviceAbsPos(dragArgs.start);
absEndPos = getDeviceAbsPos(dragArgs.end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
}
Logger.debug("Dragging from " + absStartPos.toString() + " to "
+ absEndPos.toString() + " with steps: " + dragArgs.steps.toString());
final boolean rv = device.drag(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
}
return getSuccessResult(rv);
}
private AndroidCommandResult dragElement(final DragArguments dragArgs) {
Point absEndPos = new Point();
if (dragArgs.destEl == null) {
try {
absEndPos = getDeviceAbsPos(dragArgs.end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
}
Logger.debug("Dragging the element with id " + dragArgs.el.getId()
+ " to " + absEndPos.toString() + " with steps: "
+ dragArgs.steps.toString());
try {
final boolean rv = dragArgs.el.dragTo(absEndPos.x.intValue(),
absEndPos.y.intValue(), dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
} else {
return getSuccessResult(rv);
}
} catch (final UiObjectNotFoundException e) {
return getErrorResult("Drag did not complete successfully"
+ e.getMessage());
}
} else {
Logger.debug("Dragging the element with id " + dragArgs.el.getId()
+ " to destination element with id " + dragArgs.destEl.getId()
+ " with steps: " + dragArgs.steps);
try {
final boolean rv = dragArgs.el.dragTo(dragArgs.destEl.getUiObject(),
dragArgs.steps);
if (!rv) {
return getErrorResult("Drag did not complete successfully");
} else {
return getSuccessResult(rv);
}
} catch (final UiObjectNotFoundException e) {
return getErrorResult("Drag did not complete successfully"
+ e.getMessage());
}
}
}
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
// DragArguments is created on each execute which prevents leaking state
// across executions.
final DragArguments dragArgs = new DragArguments(command);
if (command.isElementCommand()) {
return dragElement(dragArgs);
} else {
return drag(dragArgs);
}
}
}
首先来看DragArguments对象。该类为Drag中的私有类,它的构造方法回去解析传入的command,然后获得并存储一些drag方法用到的参数。例如拖拽控件时,被拖拽的控件对象,拖拽到的控件对象。坐标拖拽时,起始点坐标、终点坐标、步骤。所以就把它看成一个实体类就行了。然后分控件和坐标分别调用dragElement()和drag()方法。
dragElement()
如果拖拽到的控件不存在,那么就要用到结束坐标。如果拖拽到的控件存在,那么就用该控件计算拖拽到坐标,调用UiObject.dragTo()方法来执行命令。
drag()
直接调用UiObject.dragTo来执行
手机自动化测试: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是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...
随机推荐
- ABP Zero源码
测试运行地址:http://ghy.demo.aspnetzero.com 账号:admin 密码:123456 需要源码,请加QQ:858-048-581 1.先编译成功,Nuget下载ABP的依 ...
- 第25篇 jQuer快速学习(上)---选择器和DOM操作
这个文章经历的时间比较长,不是因为jQuery比较难,而是东西比较多,真心是个体力活.所以本来想把jQuery做成一篇去写,但由于写的时候发现jQuery发现写成一篇的话过于长,对于阅读起来也不是一个 ...
- 关于脚本化css
---恢复内容开始--- 想把自己认为的最重要的,最有用的几块写上,以后会边学边总结完善. 1.首先我们通过JavaScript可以获取到我们想要获取的元素的样式.而这个样式并非单独的哪一个部分的规则 ...
- 云服务器spark集群搭建
---恢复内容开始--- 1:去官网下载spark http://spark.apache.org 2:解压,然后在自己的机器上编译conf中的两个文件 mv slaves.template slav ...
- Eclipse的Spring IDE插件的安装和使用
Spring IDE是Spring官方网站推荐的Eclipse插件,可提供在研发Spring时对Bean定义文件进行验证并以可视化的方式查看各个Bean之间的依赖关系等. 安装 使用Eclipse M ...
- observe.js 源码 学习笔记
/** * observejs --- By dnt http://kmdjs.github.io/ * Github: https://github.com/kmdjs/observejs * MI ...
- Bootstrap学习-排版
1.标题 <h1>~<h6>,所有标题的行高都是1.1(也就是font-size的1.1倍). 2.副标题 <small>,行高都是1,灰色(#999) <h ...
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- 1782: [Usaco2010 Feb]slowdown 慢慢游
1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 570 Solved: 346[Sub ...
- wxpython tab切换页面
最近没事学习下wxpython,发现很少有关于页面切换的demo,这边分享下2中切换的方法.第一种:利用wx.Notebook第二种:利用Sizer布局实现(自己写的),代码没有涉及到什么重构之类的优 ...