手机自动化测试:appium源码分析之bootstrap九

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

TouchLongClick

package io.appium.android.bootstrap.handler;

import android.os.SystemClock;

import com.android.uiautomator.common.ReflectionUtils;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.Logger;

import java.lang.reflect.Method;

/**

* This handler is used to long click elements in the Android UI.

*

*/

public class TouchLongClick extends TouchEvent {

/*

* UiAutomator has a broken longClick, so we'll try to implement it using the

* touchDown / touchUp events.

*/

private boolean correctLongClick(final int x, final int y, final int duration) {

try {

/*

* bridge.getClass() returns ShellUiAutomatorBridge on API 18/19 so use

* the super class.

*/

final ReflectionUtils utils = new ReflectionUtils();

final Method touchDown = utils.getControllerMethod("touchDown", int.class,

int.class);

final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);

if ((Boolean) touchDown.invoke(utils.getController(), x, y)) {

SystemClock.sleep(duration);

if ((Boolean) touchUp.invoke(utils.getController(), x, y)) {

return true;

}

}

return false;

} catch (final Exception e) {

Logger.debug("Problem invoking correct long click: " + e);

return false;

}

}

@Override

protected boolean executeTouchEvent() throws UiObjectNotFoundException {

final Object paramDuration = params.get("duration");

int duration = 2000; // two seconds

if (paramDuration != null) {

duration = Integer.parseInt(paramDuration.toString());

}

printEventDebugLine("TouchLongClick", duration);

if (correctLongClick(clickX, clickY, duration)) {

return true;

}

// if correctLongClick failed and we have an element

// then uiautomator's longClick is used as a fallback.

if (isElement) {

Logger.debug("Falling back to broken longClick");

return el.longClick();

}

return false;

}

}

TouchLongClick类继承于TouchEvent,而TouchEvent继承于CommandHandler.调用TouchEvent的execute的方法中,调用了executeTouchEvent方法,所以我们来看上面的executeTouchEvent就好了,执行长点击事件,在uiautomator里有UiObject.longClick()方法,但是写过case的人知道,有时候这个方法达不到我们的需求,但是我们是自己了反射调用TouchDown和TouchUp两个个方法,而在appium里帮你解决了,它自己就帮你做到了这一点,如果你传入到是控件对象,那无可厚非,还是调用UiObject.longClick方法,如果你想根据坐标,时间在点击的话,那么就调用currectLongClick这个appium给你封装好的方法。

final ReflectionUtils utils = new ReflectionUtils();

final Method touchDown = utils.getControllerMethod("touchDown", int.class,

int.class);

final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);

通过反射得到uiautomator里的没有公开的类,从而我们想要的方法touchDown和touchUp.

public ReflectionUtils() throws IllegalArgumentException,

IllegalAccessException, SecurityException, NoSuchFieldException {

final UiDevice device = UiDevice.getInstance();

final Object bridge = enableField(device.getClass(), "mUiAutomationBridge")

.get(device);

if (API_18) {

controller = enableField(bridge.getClass().getSuperclass(),

"mInteractionController").get(bridge);

} else {

controller = enableField(bridge.getClass(), "mInteractionController")

.get(bridge);

}

}

因为uiautomator api的改动,在api18以上的版本中,mInteractionController是存在于UiAutomationBridge的父类中的变量,而在18以下的版本中它是存在于本类中的。所以反射时会有一点点小小点差异,但总的来说都是要获得InteractionController这个类,因为这个类里面存在有我们要但touch类但方法。最后我们就能轻松调用鼠标的TouchUp和TouchDown他们啦。然后再加上时间,长按就实现啦。

TouchUp

package io.appium.android.bootstrap.handler;

import com.android.uiautomator.common.ReflectionUtils;

import com.android.uiautomator.core.UiObjectNotFoundException;

import io.appium.android.bootstrap.Logger;

import java.lang.reflect.Method;

/**

* This handler is used to perform a touchDown event on an element in the

* Android UI.

*

*/

public class TouchDown extends TouchEvent {

@Override

protected boolean executeTouchEvent() throws UiObjectNotFoundException {

printEventDebugLine("TouchDown");

try {

final ReflectionUtils utils = new ReflectionUtils();

final Method touchDown = utils.getControllerMethod("touchDown", int.class,

int.class);

return (Boolean) touchDown.invoke(utils.getController(), clickX, clickY);

} catch (final Exception e) {

Logger.debug("Problem invoking touchDown: " + e);

return false;

}

}

}

有了上面的分析,对TouchUp和TouchDown还有TouchMove的分析就不用再多说了,都是反射的原理

手机自动化测试: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. Webpack学习系列(二)

    一: 安装: npm install webpack-dev-server -g npm install webpack-dev-server --save (下载到当前文件夹) npm instal ...

  2. [Linux] PHP程序员玩转Linux系列-搭建代码开发环境

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 有些同学可能觉得我写的都是啥yum安装的,随便配置一下而已,没啥技术含量,我的目的 ...

  3. WC2015 k小割(k短路+暴力+搜索)

    首先这道题不是非同一般的恶心,三个数据层次对应三个程序= = PROBLEM:http://uoj.ac/problems解法: 1~2直接暴力枚举边的选择与否+判断就行了 7~14可以发现是一个平面 ...

  4. Java实现GB2312文件转UTF8文件

    有些书带的光盘的源代码是GB2312编码.通常IDE的编码是UTF8.这样直接导入IDE会乱码. 这时候就需要把GB2312的文件转成UTF8的文件.转化的思路很简单,读入流初始化的时候告诉jvm是G ...

  5. linux 下 zookeeper安装

    1.安装zookeeper-3.4.6cd /usr/soft#解压zookeeper 安装包tar -zvxf zookeeper-3.4.6#拷贝安装包到安装目录cp zookeeper-3.4. ...

  6. 每天一个linux命令(36)--vmstat命令

    vmstat 是 Virtual Memory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...

  7. Thread类常用方法

    Thread类构造方法: 1.Thread(): 2.Thread(String name): 3.Thread(Runable r): 4.Thread(Runable r, String name ...

  8. 第十二篇 C# 将HTML 直接转成Excel

    前些天写项目的时候,客户要求用HTML表格把信息展示出来,后面还要用展示的内容要导出Excel.本来想想在后台操作的话估计是要做死了,但是经过细想,Excel能够发布成HTML,一定也可以由HTML转 ...

  9. Dapper的扩展这个你知道嘛?

    之前写的ORM对比文章中,我选Dapper作为底层ADO的基础访问框架后,我对此再次进行进一步的深入研究,发现里面还有延伸了一些好用的扩展方法和特性,那我便简单的跟大家说一下特性标签. 一.Table ...

  10. lxd-启动篇分析

    lxd是什么:lxd是基于lxc构筑的容器管理进程,提供镜像,网络,存储,以及容器的能力,对外暴漏restfull API.其与docker的区别是docker更切近与app container,以应 ...