bootstrap之鼠标操作
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的分析就不用再多说了,都是反射的原理
TouchDown
类同
TouchMove
类同
bootstrap之鼠标操作的更多相关文章
- Python模拟键盘输入和鼠标操作
Python模拟键盘输入和鼠标操作 一.Python键盘输入模拟: import win32api import win32con win32api.keybd_event(17,0,0,0) #c ...
- c# 鼠标操作
1#region 3using System; 4using System.Runtime.InteropServices; 6#endregion 8namespace Windows.Forms. ...
- opencv鼠标操作及GUI矩形绘画
OpenCV的鼠标操作是通过一个中介函数配合回调函数来实现的.指定鼠标操作消息回调函数的函数为SetMouseCallback. void setMouseCallback(const string& ...
- WPF 中模拟键盘和鼠标操作
转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...
- python selenium-webdriver 元素操作之鼠标操作(四)
上节内容主要说明了元素的定位,本节内容说要说对元素的操作,元素的操作分为两部分一部分是鼠标的操作,另一种是对键盘对元素的操作,下面我们主要讲解一下鼠标对元素的操作. webdriver 模块中几种比较 ...
- python-web自动化-鼠标操作
鼠标操作由ActionChains类来完成鼠标操作 perform() 执行鼠标操作move_to_element() 鼠标悬浮:最常用的操作double_click() 双击操作context_cl ...
- Selenium基础知识(二)鼠标操作
一.鼠标操作 这个需要使用webdriver下的ActionChains类,这个类是操作鼠标操作的: from selenium.webdriver import ActionChains 鼠标操作可 ...
- selenium自动化之鼠标操作
在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素.那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件——ActionChai ...
- 【python】鼠标操作
[python]鼠标操作 推荐地址:http://www.cnblogs.com/fnng/p/3288444.html --------------------------------------- ...
随机推荐
- 标准C程序设计七---113
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- declaration specifier, declarator, type specifier
static struct abc * b; static struct abc : declaration specifier * b : declarator struct abc : type ...
- configure: error: off_t undefined; check your library configuration
configure: error: off_t undefined; check your library configuration 发生背景: 编译PHP时出现的提示,报错信息为: configu ...
- docker部署 mysql redis问题
问题:(ubuntu不报错,centos报错) ERROR: : starting container process caused "process_linux.go:402: conta ...
- Hash Collision DoS 问题
Hash Collision DoS 问题http://coolshell.cn/articles/6424.html Hash Collision DoS (Hash碰撞的拒绝式服务攻击),有恶意的 ...
- Oracle单个datafile大小的限制
http://blog.itpub.net/30776559/viewspace-2146790/
- SQL Server 触发器 详细讲解
最近在做微信活动,需要用到存储过程,在网上找了下感觉使用触发器更好些,和大家分享下 希望对你有用. 触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动 ...
- es6 export、import
一.输出变量 1.export var a = 0; 2.var a = 0'; export {a}; 3.var a =0 ; export {a as rename}; //使用as重命名的对外 ...
- 仿照jquery封装一个自己的js库
所谓造轮子的好处就是复习知识点,加深对原版jquery的理解.本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包括 ...
- Data.FireDACJSONReflect单元不支持跨平台
Data.FireDACJSONReflect不支持跨平台 Data.FireDACJSONReflect里面:IFDJSONDeltasApplyUpdates,TFDJSONDeltas,TFDJ ...