手机自动化测试: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来执行

最新文章

  1. DIV+CSS系统学习:转载
  2. 数据库基本概念-oracle介绍
  3. WCF系列学习5天速成
  4. 如何完全禁用或卸载Windows 10中的OneDrive - 51CTO.COM
  5. CDZSC_2015寒假新人(2)——数学 H
  6. Word常用实用知识2
  7. 关于CGI:Tomcat、PHP、Perl、Python和FastCGI之间的关系
  8. smm框架整合实现登录功能
  9. java虚拟机 jvm 栈数据区
  10. J2EE学习从菜鸟变大鸟之六 EJB(Enterprise JavaBean)企业级Java组件
  11. vue项目全局引入vue-awesome-swiper插件做出轮播效果
  12. Amazon.com 美国亚马逊 直邮中国 手把手教程(转)
  13. 三种方法让Response.Redirect在新窗口打开
  14. 【Nodejs】外研社一年级起三年级下MP3下载爬虫1.00
  15. web前端面试题(一)
  16. C#:(问题)已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
  17. java代码---实现随机产生1000个随机数,并10个一行的输出
  18. 福大软工1816:Alpha(9/10)
  19. hdu_2817_高速幂
  20. 给安卓端调用的短信发送接口demo

热门文章

  1. css3动画animate
  2. sping整合hibernate之二:dao层开发
  3. 关于 &lt;textarea &gt;&lt;/textarea &gt;标签在苹果微信浏览器出现 内阴影
  4. iOS开发——设计模式那点事
  5. 每天一个Linux命令(15)--tail命令
  6. Java Web(十三) 使用javamail进行发送邮件,(使用QQ,163,新浪邮箱服务器)
  7. UCSC genome browser 个人track 安装
  8. Bean复制
  9. MacOS无法登录App Store修复
  10. 3097: Hash Killer I