手机自动化测试: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. iframe的优缺点?

    1.`<iframe>`优点: 解决加载缓慢的第三方内容如图标和广告等的加载问题 Security sandbox 并行加载脚本 2.`<iframe>`的缺点: *ifram ...

  2. 不惧面试:HTTP协议(3) - Cookie

    v博客前言 先交代下背景,写这个系列的原因是总结自己遇到的面试题以及可能遇到的题目,更重要的是,今年定的目标是掌握网络这一块的知识点,先是搞懂HTTP协议,然后是TCP/IP协议,再就是WCF如何运用 ...

  3. 微软MSBI商业智能视频

    第一讲:BI介质安装.BI基础知识讲解.BI方法论讲解.项目讲解                 1.BI基础知识讲解.BI方法论实施2.微软BI的介绍(数据仓库介绍.SSIS介绍.SSAS介绍.SS ...

  4. 卷积神经网络(CNN)前向传播算法

    在卷积神经网络(CNN)模型结构中,我们对CNN的模型结构做了总结,这里我们就在CNN的模型基础上,看看CNN的前向传播算法是什么样子的.重点会和传统的DNN比较讨论. 1. 回顾CNN的结构 在上一 ...

  5. Python编程快速上手——让繁琐工作自动化学习笔记

    第一部分 基本语法 1.字符串不能直接和数字相加,要用str()转一下:但是可以和数字相乘,用于表示多个字符串复制:字符串不能和浮点数直接结合,字符串可以和字符串直接相加: 2.输入函数用input( ...

  6. mfc---单文档工程添加消息响应

    写消息映射:.h中些函数头文件afx_mag … .cpp中写函数体 .cpp中写消息映射 给toolbar添加消息: .h中添加头文件afx_msg …. .cpp中添加函数体,消息映射ON_COM ...

  7. mfc---CFileFind

    使用CFileFind实现在指定路径下,查找指定类型文件 CFileFind.FindFile(FilePath + "\\*.*"),成功返回true,否则返回false CFi ...

  8. 移动OA日程支持费用及评论

    业务介绍 AIO7系统最新更新版本在移动OA的日程管理进行改进,增加了创建费用的功能,且在日程批注上也可查看:在日程个人界面和批注界面都支持了评论功能.移动OA上日程对费用及评论的支持,方便用户外出时 ...

  9. 【Harmony】概述

      原文来自本人的微信公众号文章  系统工程实验室     引言 基于模型的系统工程(简称MBSE,英文全称Model based System Engineering )的实践至少需要三个维度的支撑 ...

  10. Svm相关

    Svm相关: 1)  SVM方法是通过一个非线性映射p,把样本空间映射到一个高维乃至无穷维的特征空间中(Hilbert空间),使得在原来的样本空间中非线性可分的问题转化为在特征空间中的线性可分的问题. ...