手机自动化测试: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. Javascript高级程序设计——语法、关键字、保留字、变量、数据类型

    1.了解基本语法,JS大小写区分.注释风格.什么是严格模式等. 2.知道ES3和ES5的关键字和保留字大概有哪些,如果使用关键字会报什么错,使用保留字决定于特定浏览器引擎. 3.全局变量和局部变量的定 ...

  2. 从HTML5规范弄清i、em、b、strong元素的区别

    为了语义化,HTML5增加了不少新标签.其中i.em和b.strong这两组标签是最容易弄混的,不好好去探究一下,还真说不清.这个也是前端面试中经常会问的问题.今天从源头上,也就是从HTML5的文档( ...

  3. web前端的发展态势 浅识

    以前 作为一个java程序员写的代码主要还是后台的代码,虽然开始的时候前后端都写,但是也是用别人造好的轮子来用,学学html,css,js,jquery,再找一个前端ui框架学学,上手之后我们就可以写 ...

  4. java中 i = i++ 的结果

    昨天看到下面这段代码,分享出来给大家看看,大家也可以讨论讨论. int i = 0; i = i++; System.out.println("i的值是 "+i); 根据我们通常所 ...

  5. ThinkPHP项目总结

    1.当你 require ThinkPHP之后,会自动在App目录下生成 common, Home, Runtime 三个文件夹. 2.输入网址 http://localhost/blog/app/i ...

  6. 学好UI你必须要掌握这些技术

    转自:http://blog.sina.com.cn/s/blog_15da22ed10102x0gx.html ui设计现在已经是设计行业中的瞩目之星,无论在PC端.移动端还是游戏上都是大放异彩. ...

  7. Asp.Net MVC 之 Autofac 初步使用3 集成web api

    今天我们试着在WebApi2实现autofac的注入,关于这方面也是看了几位园友的分享省了不少时间,所以结合着前篇的demo再新建webapi进行... 一样开篇还是发下大概demo结构: 还是nug ...

  8. Linux实战教学笔记21:Rsync数据同步工具

    第二十一节 Rsync数据同步工具 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载,转载 ...

  9. 中软卓越IT培训:给IT程序员的18个忠告

    1 .想清楚,写清楚,说清楚,才是真正的清楚! 2 .多花点时间沟通清楚需求,才能把握正确方向! 3 .修复需求错误的成本是代码错误的几十倍! 4 .程序员最大的坏习惯就是:急于动手写代码! 5 .提 ...

  10. Debugging

    Debugging Debugging A debugger is an application that enables a developer to observe and correct pro ...