手机自动化测试: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. JS中函数参数和函数返回值的理解

    函数本质就是功能的集合 JS中函数是对象,因此,函数名实际上仅仅是一个指向函数对象的指针,不会与某个函数绑定,所以,JS中没有重载(重载就是通过传递不同类型的参数,使两个相同函数名的函数执行不同的功能 ...

  2. JavaScript中国象棋程序(1) - 界面设计

    "JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第1节. 这一系列共有9个部分: 0.JavaScript中国象 ...

  3. FTPS (FTP over SSL) vs. SFTP (SSH 文件传输协议): 我们如何做出选择

    第一个RFC的FTP协议发布通过网络使用FTP协议(由RFC 959或更高版本)的文件传输始于1980年,FTP提供上传,下载和删除文件,创建和删除目录,读取目录内容的功能.虽然FTP是非常受欢迎的, ...

  4. 作为前端工程师,必须要学会的基本工具之一(AI)

    由于之前工作中一直使用的都是photoshop,所以对AI软件的使用并不是很熟,导致使用过程中,遇到了困难----字体无法选中.作为前端工程师都知道,一张设计图中有很多文字,如果纯手打那简直是要累死人 ...

  5. Overlay网络技术之vxvlan

    了解云计算数据中心网络架构的过程中,看到这么一篇不错的关于大二层技术overlay的文章,转载分享给大家! 一. 传统网络面临的问题 随着企业业务的快速扩展需求,IT做为基础设施,快速部署和减少投入成 ...

  6. MySQL学习分享--数值类型

    数值类型 MySQL的数值类型包括整数类型.浮点数类型.定点数类型.位类型. 整数类型 MySQL支持的整数类型有tinyint.smallint.mediumint.int.bigint(范围从小到 ...

  7. Unbutu14.04 切换ROOT用户后无法启用音频

    系统环境: Ubuntu14.04 x64 问题描述: 今天安装了Ubuntu14.04的64位系统,启用root用户登录后,观看视频时出现没有声音的现象. 问题原因: Ubuntu安装后默认root ...

  8. 1623: [Usaco2008 Open]Cow Cars 奶牛飞车

    1623: [Usaco2008 Open]Cow Cars 奶牛飞车 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 291  Solved: 201[S ...

  9. 深入理解ajax系列第五篇——进度事件

    前面的话 一般地,使用readystatechange事件探测HTTP请求的完成.XHR2规范草案定义了进度事件Progress Events规范,XMLHttpRequest对象在请求的不同阶段触发 ...

  10. linux 部署jenkins

    1.安装jdk,配置jdk路径,python路径 (当前用户的配置文件)  vi .bash_profile export JAVA_HOME=$HOME/local/jdk1.8.0_111 exp ...