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 --------------------------------------- ...
随机推荐
- Android监听键盘右下角确定键
代码改变世界 Android监听键盘右下角确定键 kotlin写法 <EditText android:id="@+id/seachFriend" android:layou ...
- iOS-----openGL--openGL ES iOS 入门篇3---> 纹理贴图(texture)
在这篇文章中,我们将学习如何在openGL中使用纹理贴图. penGL中纹理可以分为1D,2D和3D纹理,我们在绑定纹理对象的时候需要指定纹理的种类.由于本文将以一张图片为例,因此我们为我们的纹理对象 ...
- 【bzoj2150】部落战争 有上下界最小流
题目描述 lanzerb的部落在A国的上部,他们不满天寒地冻的环境,于是准备向A国的下部征战来获得更大的领土. A国是一个M*N的矩阵,其中某些地方是城镇,某些地方是高山深涧无人居住.lanzerb把 ...
- 【Luogu】P1430序列取数(DP)
题目链接 博弈DP太喵了qwq 设f[i][j]表示剩下区间[i,j]要取,先手最大值 明显我们要从这区间里面拿个最大的 就等价于这段区间的前缀和,我们要给对手留下个最小的 就是f[i][j]=sum ...
- 【Luogu】P3709大爷的字符串题(莫队算法)
题目链接 语文题啊…… 看题解发现是让求区间中最多的数的个数,于是果断理解了一会题解……莫队套上完事. sum[i]表示i这个数出现的次数,cnt[i]表示出现i次的数有几个,然后乱搞搞……就好了 # ...
- Eclipse或MyEclipse没有在java类文件上显示Spring图标的问题
Eclipse或MyEclipse没有在java类文件上显示接口图标的问题解决办法: 前: 后:
- 处理登录和cookie
做法 1: 可以一步一步cookies,毫无疑问,这非常麻烦. import requests params = {'',''} r = requests.post('',params) r = re ...
- La 3942 字符串+dp
题目大意:一个字符串,可以分解成若干英语单词的连接(单词可以重复使用),求有多少种方法? #include<iostream> #include<cstdio> #includ ...
- utilize HttpClient to generate a SSL access and generate REST access to fetch data, async programming? cool and brief
WebRequestHandler handler = new WebRequestHandler(); try { X509Certificate2 certificate = new X509Ce ...
- mybatis原理源码大牛连接
mybatis讲解的非常好的连接: https://www.jianshu.com/nb/5226994 执行流程简介参考:http://www.cnblogs.com/dongying/p/4142 ...