手机自动化测试: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. xx-net 使用方式

    <iframe width='738' height='523' class='preview-iframe' scrolling='no' frameborder='0' src='http: ...

  2. centos快速安装redis

    mkdir redis cd redis wget http://labfile.oss.aliyuncs.com/files0422/redis-2.8.9.tar.gz 解压 tar -xvfz ...

  3. 《剑指offer》— JavaScript(21)栈的压入、弹出序列

    栈的压入.弹出序列 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5, ...

  4. Nginx rewrite(重读)

    Nginx Rewrite规则相关指令  Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Re ...

  5. mfc---右键蹦出菜单

    WM_CONTEXTMENU CMenu menu; menu.LoadMenu(IDR_MENU_POPUP); menu.GetSubMenu(0)->TrackPopupMenu(TPM_ ...

  6. Java基础之数据类型和转换

    一.常见的数据类型分类 1.java中基本数据类型分为三大类,即布尔类型,字符型,数值型.其中数值型又分为整型和浮点型.引用数据类型分为类,接口,数组,枚举,注解.具体如下: 注:一个字节 = 8位 ...

  7. 数据库基础——(SQLserver)约束

                                                              数据库定义:一些存储在硬盘上的数据文件 内存:计算机临时存储的一些数据 .net - ...

  8. BOM基础(二)

    跟DOM一样,BOM其实也是由很多的API组成. 不过对于BOM来说,最痛苦的不是不记得API,而是明明记得这个这个API,却没有考虑到它的兼容性. 之前的文章中讲到了offset系列的属性,他的宽高 ...

  9. KoaHub平台基于Node.js开发的Koa的调试实用程序

    debug small debugging utility debug tiny node.js debugging utility modelled after node core's debugg ...

  10. 3384/1750: [Usaco2004 Nov]Apple Catching 接苹果

    3384/1750: [Usaco2004 Nov]Apple Catching 接苹果 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 18  Solv ...