Swipe

我定义为滑动,但它字面的意思又不是,事件的形式类似于小时候拿着一块石头片,朝水面飞过去,假设你手法能够那么就是swipe走的路线,假设你手法不行,接触水面的时候就没再飞起来那就会被人嘲笑的。

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 java.util.Hashtable; /**
* This handler is used to swipe.
*
*/
public class Swipe 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)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
final Hashtable<String, Object> params = command.params();
final Point start = new Point(params.get("startX"), params.get("startY"));
final Point end = new Point(params.get("endX"), params.get("endY"));
final Integer steps = (Integer) params.get("steps");
final UiDevice device = UiDevice.getInstance(); Point absStartPos = new Point();
Point absEndPos = new Point(); if (command.isElementCommand()) {
try {
final AndroidElement el = command.getElement();
absStartPos = el.getAbsolutePosition(start);
absEndPos = el.getAbsolutePosition(end, false);
} catch (final UiObjectNotFoundException e) {
return getErrorResult(e.getMessage());
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
} catch (final Exception e) { // handle NullPointerException
return getErrorResult("Unknown error");
}
} else {
try {
absStartPos = getDeviceAbsPos(start);
absEndPos = getDeviceAbsPos(end);
} catch (final InvalidCoordinatesException e) {
return getErrorResult(e.getMessage());
}
} Logger.debug("Swiping from " + absStartPos.toString() + " to "
+ absEndPos.toString() + " with steps: " + steps.toString());
final boolean rv = device.swipe(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), steps);
if (!rv) {
return getErrorResult("The swipe did not complete successfully");
}
return getSuccessResult(rv);
}
}

无论它怎样定义,先分析源代码最后再定义。

    final Hashtable<String, Object> params = command.params();
final Point start = new Point(params.get("startX"), params.get("startY"));
final Point end = new Point(params.get("endX"), params.get("endY"));
final Integer steps = (Integer) params.get("steps");
final UiDevice device = UiDevice.getInstance();     Point absStartPos = new Point();
    Point absEndPos = new Point();

首先从命令里取得參数,然后解析出所须要的3个变量:起始点start、终点end、步骤steps。然后获得设备对象,定义2个私有Point对象,以备后用。

然后分条件处理,处理控件还是处理坐标。

控件

        final AndroidElement el = command.getElement();
absStartPos = el.getAbsolutePosition(start);
absEndPos = el.getAbsolutePosition(end, false);

首先获取控件对象,再通过getAbsolutePosition传入不同的參数获得在该控件上点击的起始点和结束点。

public Point getAbsolutePosition(final Point point,
final boolean boundsChecking) throws UiObjectNotFoundException,
InvalidCoordinatesException {
final Rect rect = el.getBounds();
final Point pos = new Point();
Logger.debug("Element bounds: " + rect.toShortString()); if (point.x == 0) {
pos.x = rect.width() * 0.5 + rect.left;
} else if (point.x <= 1) {
pos.x = rect.width() * point.x + rect.left;
} else {
pos.x = rect.left + point.x;
}
if (boundsChecking) {
if (pos.x > rect.right || pos.x < rect.left) {
throw new InvalidCoordinatesException("X coordinate ("
+ pos.x.toString() + " is outside of element rect: "
+ rect.toShortString());
}
} if (point.y == 0) {
pos.y = rect.height() * 0.5 + rect.top;
} else if (point.y <= 1) {
pos.y = rect.height() * point.y + rect.top;
} else {
pos.y = rect.left + point.y;
}
if (boundsChecking) {
if (pos.y > rect.bottom || pos.y < rect.top) {
throw new InvalidCoordinatesException("Y coordinate ("
+ pos.y.toString() + " is outside of element rect: "
+ rect.toShortString());
}
} return pos;
}

上面的一大段代码,看起来非常复杂,事实上非常easy,业务非常容易理解,处理这样的点的时候就须要推断非常多东西。上面的代码首先分析x坐标然后分析y坐标。x和y坐标的推断和处理时一样的,所以我仅仅讲一下x坐标。

首先推断x坐标是否为0,假设为0,定义初始点的x坐标为控件的中心点的横坐标。假设x的坐标小于1,说明坐标为相对坐标,用百分比来求值,此时就要与宽度做乘积运算得到详细值。假设上面2种情况都不符合,那就是详细坐标值,那就直接元素的x坐标值加上控件的边框左坐标值。最后依据传入的boolean值来推断是否做一个超出边界的验证。假设超出边界就跑出异常。y坐标的获取方式类似。最后得到坐标值并返回,回到execute方法中。

坐标

absStartPos = getDeviceAbsPos(start);
absEndPos = getDeviceAbsPos(end);

通过调用getDeviceAbsPos()方法得到坐标值来初始化之前声明的私有Point对象.

protected static Point getDeviceAbsPos(final Point point)
throws InvalidCoordinatesException {
final UiDevice d = UiDevice.getInstance();
final Point retPos = new Point(point); // copy inputed point final Double width = (double) d.getDisplayWidth();
if (point.x < 1) {
retPos.x = width * point.x;
} if (retPos.x > width || retPos.x < 0) {
throw new InvalidCoordinatesException("X coordinate ("
+ retPos.x.toString() + " is outside of screen width: "
+ width.toString());
} final Double height = (double) d.getDisplayHeight();
if (point.y < 1) {
retPos.y = height * point.y;
} if (retPos.y > height || retPos.y < 0) {
throw new InvalidCoordinatesException("Y coordinate ("
+ retPos.y.toString() + " is outside of screen height: "
+ height.toString());
} return retPos;
}

类似于上面的方法,也是要先推断传过来的坐标值是否小于1,假设小于1,当作百分比来球坐标值。假设超出屏幕的范围抛出异常,最后返回坐标值回到execute方法。

===============================================================================================================================

final boolean rv = device.swipe(absStartPos.x.intValue(),
absStartPos.y.intValue(), absEndPos.x.intValue(),
absEndPos.y.intValue(), steps);

最后调用UiDevice.swipe方法来运行命令,推断是否运行成功。

总结

运行swipe命令有2中命令格式

  • 控件
  • 坐标

坐标又分为相对坐标百分比和绝对坐标两种方法。

bootstrap之Swipe的更多相关文章

  1. 【读书笔记《Bootstrap 实战》】3.优化站点资源、完成响应式图片、让传送带支持手势

    A.优化站点资源 速度很重要.用户很关心.我们的站点必须加载够快,否则用户就会走人.SEO 也很重要.我们的站点必须加载够快,否者搜索排名就会下降. 明白了这样,我们就来清点一下 [Bootstrap ...

  2. 【Bootstrap】3.优化站点资源、完成响应式图片、让传送带支持手势

    A.优化站点资源 速度很重要.用户很关心.我们的站点必须加载够快,否则用户就会走人.SEO 也很重要.我们的站点必须加载够快,否者搜索排名就会下降. 明白了这样,我们就来清点一下 [Bootstrap ...

  3. 让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果.然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有支 ...

  4. Bootstrap_让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    Bootstrap 的 carousel.js 插件并没有支持手势. 3种解决方案 : jQuery Mobile (http://jquerymobile.com/download/) $(&quo ...

  5. Bootstrap幻灯轮播如何支持触屏左右滑动手势?

    最近ytkah在学习用bootstrap搭建网站,Bootstrap能自适应pc端和手机端,并且移动设备优先,适合现如今移动营销.bootstrap是封装好的框架,需要某些功能只需调用相应的组件就可以 ...

  6. bootstrap实现 手机端滑动效果,滑动到下一页,jgestures.js插件

    bootstrap能否实现 手机端滑动效果,滑动到下一页 jgestures.js插件可以解决,只需要引入一个JS文件<script src="js/jgestures.min.js& ...

  7. bootstrap之Flick

    Flick package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice; imp ...

  8. bootstrap之Click大事

    上一篇文章中谈到了bootstrap流程,本文开始把目光bootstrap它可以接受指令(从源代码视图的透视.因为appium该项目现在还处于不断更新,因此,一些指令已经实现.也许未来会实现一些.从视 ...

  9. Appium Android Bootstrap源码分析之命令解析执行

    通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...

随机推荐

  1. cmd用到的基本操作

    dir #显示当前目录中的文件和子目录 dir /a #显示当前目录中的文件和子目录,包括隐藏文件和系统文件 a = all dir c: /a:d #显示 C 盘当前目录中的目录 d = direc ...

  2. Unity 编辑器扩展自定义窗体

    这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...

  3. Vanya and Scales(思维)

    Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. OpenStack 部署总结之:在CentOS 6.5上使用RDO单机安装icehouse(Ml2+GRE)

    本文主要介绍怎样在CentOS6.5上通过RDO来安装icehouse,因为安装的过程中涉及的软件较多,以及依赖关系比較复杂,建议使用一个全新的操作系统来进行安装. 安装步骤详细例如以下 (1)安装操 ...

  5. static_cast

    C 风格(C-style)强制转型例如以下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这 ...

  6. Esper学习之六:EPL语法(二)

    中秋三天,说闲也不闲,调调工作的代码,倒还解决不少问题.不过也是因为最近工作忙的缘故,Esper被我冷落不少日子了,趁着今天最后一天,赶紧写一篇出来. 从上一篇开始说EPL的语法,主要是关于注解的.今 ...

  7. SSH框架-Caused by: org.hibernate.MappingException: column attribute may not be used together with <column> subelement

    昨晚修改了一些表关系,在相关的hbm.xml文件中做了改动,今天早上起来启动tomcat后,发现项目启动不了,控制台报错: 2015-6-14 9:09:42 org.apache.catalina. ...

  8. 在美国公司架构中,LLC、LLP 和 Corporation 的区别何在?

    这个问题,首先需要弄清楚这样一个事实:LLC.LLP.Corporation分别属于三种不同类型的公司实体. 1,LLC (Limited Liability Company)是责任有限公司: 2,L ...

  9. (转)Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds

    仰天长啸 Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 S ...

  10. 伪元素”:after” , “:before"

    伪元素就是源码html中不存在,而视觉上又存在的元素     简单用法: blockquote:before {      content: open-quote;      // 其他样式 } // ...