手机自动化测试: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. java dbutils查询数据库时无法给部分字段赋值原因

    1,javaBean如下: public class User {        /**     * 用户唯一标识(ID)     */    private String uid;    /**   ...

  2. 锋利的jQuery中的事件与动画

    奋夜的奋斗  ----  事件与动画 ----  来自地狱的战镰 小小的单词难不倒我们哦!!!!!!!    bind:绑定     unbind:接触绑定    toggle:栓牢   fadeou ...

  3. Linux系统(五)负载均衡LVS集群之DR模式

    序言 DR模式是lvs集群中三种负载均衡模式的其中一种,那么上一篇中我写啦关于NAT模式的搭建与原理,为什么还要有DR模式与IP隧道模式呢? 首先我们来看3张图.LVS/NAT模式如下图: LVS/I ...

  4. Android Monkey压力测试介绍

    monkey:通过Monkey程序模拟用户触摸屏幕.滑动Trackball. 按键等操作来对设备上的程序进行压力测试,检测程序多久的时间会发生异常. Monkey的构架 Monkey的参数 Monke ...

  5. Qt入门之基础篇(三):掌握Qt4的静态编译基本方法

    转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...

  6. 【openstack N版】——可视化服务horizon

    一. openstack可视化服务horizon 1.1dashboard介绍 仪表盘依赖于功能核心服务,包括身份,图像服务,计算和网络两种(neutron)或传统网络(nova-neutron)与独 ...

  7. 模拟做饭系统(java+线程中的join方法)

    (一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...

  8. css :target

    花了半小时在找如果完成:target的问题 需求:点击<a href="#Main">Main</a>时,会触发:target 效果 结果在网络上没有找到, ...

  9. Java 集合的简单实现 (ArrayList & LinkedList & Queue & Stack)

    ArrayList 就是数组实现的啦,没什么好说的,如果数组不够了就扩容到原来的1.5倍 实现了迭代器 package com.wenr.collection; import java.io.Seri ...

  10. MySQL Sniffer

    MySQL Sniffer 是360开源的一个基于 MySQL 协议的抓包工具, 能实时抓取客户端端请求,并格式化输出操作语句,操作十分简单.对于问题的定位,操作的审核是个不错的利器. Github地 ...