手机自动化测试: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. mac os x在PC上安装

    系统安装之前的准备工作及安装过程简介 前面我们已经提到,苹果电脑虽然已经采用了x86架构的Intel处理器,但其官方并不提供在非苹果电脑上安装Mac OS的支持.所以,要想在普通PC/笔记本电脑上安装 ...

  2. iOS开发tips-UIScrollView的Autlayout布局

    UIScrollViewj尽管继承于UIView,但它是一个相对比较特殊的视图,特别是当它遇到了AutoLayout之后.在UIScrollView中使用AutoLayout的目的除了使用相对约束确定 ...

  3. Winfrom 程序打包及安装

    前言 近期被公司外派到驻空调厂的项目组,支援一个TCP相关的程序对接.主要是做智能门禁系统,然后主要是统计出实时的进出人数. 我这边能作为服务端,门禁设备作为客户端,整个流程并不算复杂,根据协议来写, ...

  4. Log日志规范

    Overview 一个在生产环境里运行的程序如果没有日志是很让维护者提心吊胆的,有太多杂乱又无意义的日志也是令人伤神.程序出现问题时候,从日志里如果发现不了问题可能的原因是很令人受挫的.本文想讨论的是 ...

  5. CK editor 制作 ”小“plugin

    ckeditor 是什么? http://ckeditor.com/ 这工具里有大量他人写好的功能(plugin),如果要求不高,会找到的.但是我就是没有找到... 需求: ·自己制作一个小功能,可以 ...

  6. 初始Django

    Django概述 Django是什么 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C. Django的主要目标是使得开发复杂的 ...

  7. x86主机搭建家庭智能路由系统 ---- 设计篇

    组件简介 Proxmox: 基于Debian的虚拟化系统,支持OpenVZ和KVM. pfSense: 基于FreeBSD的开源路由系统. FreeNAS: 开源NAS系统. shadowsocks: ...

  8. ERP实施规范化及示例——邮件沟通

    上门服务前 上门服务计划,要详细,提前发给客户,反复确定双方的行程 上门服务所需的常规文档(培训SOP,培训记录) 项目组内反复确认本次上门要完成的事项 …… 上门服务中 先跟老板沟通项目进展,本次上 ...

  9. 移动OA日程支持费用及评论

    业务介绍 AIO7系统最新更新版本在移动OA的日程管理进行改进,增加了创建费用的功能,且在日程批注上也可查看:在日程个人界面和批注界面都支持了评论功能.移动OA上日程对费用及评论的支持,方便用户外出时 ...

  10. Hadoop伪分布安装配置

    安装环境: 系统:Ubuntu 14.10   64bit hadoop:2.5.1 jdk:1.8.0_11 安装步骤: 一.安装JDK 安装 jdk,并且配置环境以及设置成默认 sudo gedi ...