手机自动化测试: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七的更多相关文章

  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. PHP基础学习(函数一)

    PHP(Hypertext Preprocessor):超文本预处理器,一种嵌入在HTML中并且运行在服务器端的脚本语言. var_dump--打印变量相关信息 说明:  <?php var_d ...

  2. Java实现GB2312文件转UTF8文件

    有些书带的光盘的源代码是GB2312编码.通常IDE的编码是UTF8.这样直接导入IDE会乱码. 这时候就需要把GB2312的文件转成UTF8的文件.转化的思路很简单,读入流初始化的时候告诉jvm是G ...

  3. 基于php(Thinkphp)+jquery 实现ajax多选,反选,不选 删除数据 新手学习向

    jquery多选源码来源素材火http://www.sucaihuo.com/ (有什么问题可以私本人邮箱1428937236@qq.com,也可加qq来时表明来意即可) 建议有js基础,了解jque ...

  4. HttpWebRequest 模拟网站登录获取数据

    此文档仅仅是一个BaseCode,已做后续查阅 项目使用IBM Platform Symphony分布式平台,所有业务处理都在这个分布式平台上计算,需求是获取这些计算机机群的运行状态,和每一个服务的的 ...

  5. java基础之路(二)上

    &和&&的区别 我们今天就来说一下&与&&运算符的区别 &运算符有两种用法:(1)按位与:(2)逻辑与.&&运算符是短路与运算. ...

  6. DOM基础(一)

    在我们刚刚学JavaScript的时候,就应该听说过,JavaScript是由三部分组成的.分别是ECMAScript,DOM和BOM组成的.ECMAScript是JavaScript的核心,它描述了 ...

  7. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  8. 使用IDEA的gradle整合spring+springmvc+mybatis 采用javaconfig配置

    1.在上篇博客里讲述了spring+mybatis的整合,这边在上篇的基础上进行开发. 上篇博客链接http://www.cnblogs.com/huangyichun/p/6149946.html ...

  9. solr query的post方式

    众所周知, solr 是通过 GET 方式来进行查询的. 那么solr 是否支持POST 方式进行查询呢? 通过一番调研,发现SOLR其实是支持POST方式进行查询的. 方式为: 使用form 方式提 ...

  10. javaScript事件(六)事件类型之滚轮事件

    滚轮事件其实就是一个mousewheel事件,这个事件跟踪鼠标滚轮,类似Mac的触屏版. 一.客户区坐标位置 鼠标事件都是在浏览器视口的特定位置上发生的.这个位置信息保存在事件对象的clientX和c ...