UiAutomator自动化框架实现对控件的长按功能
作者:魅族科技 发布时间:[ 2016/10/19 11:04:40 ] 推荐标签:软件测试工具 UiAutomator
在使用uiautomator自动化测试框架过程中,基于对控件实现长按,发现uiautomator自带的方法longClick()很多时候不能实现对控件的长按,因此需要重写该方法。
主要代码如下:
public static boolean longClickWithTime(UiObject obj, long time) throws Exception {
Class<?> InteractionControllerClass = null;
Method findAccessibilityNodeInfoMethod = null;
Method getInteractionControllerMethod = null;
Method touchDownMethod = null;
Method touchUpMethod = null;
Field WAIT_FOR_SELECTOR_TIMEOUT = null;
findAccessibilityNodeInfoMethod = UiObject.class.getDeclaredMethod("findAccessibilityNodeInfo",long.class);
findAccessibilityNodeInfoMethod.setAccessible(true);
WAIT_FOR_SELECTOR_TIMEOUT = UiObject.class.getDeclaredField("WAIT_FOR_SELECTOR_TIMEOUT");
WAIT_FOR_SELECTOR_TIMEOUT.setAccessible(true);
AccessibilityNodeInfo node = (AccessibilityNodeInfo) findAccessibilityNodeInfoMethod.invoke(obj, WAIT_FOR_SELECTOR_TIMEOUT.getLong(obj));
if(node == null) {
throw new UiObjectNotFoundException(obj.getSelector().toString());
}
Rect rect = obj.getVisibleBounds();
getInteractionControllerMethod = UiObject.class.getDeclaredMethod("getInteractionController");
getInteractionControllerMethod.setAccessible(true);
Object interactionController = getInteractionControllerMethod.invoke(obj);
InteractionControllerClass = Class.forName("com.android.uiautomator.core.InteractionController");
touchDownMethod = InteractionControllerClass.getDeclaredMethod("touchDown", int.class, int.class);
touchDownMethod.setAccessible(true);
touchUpMethod = InteractionControllerClass.getDeclaredMethod("touchUp", int.class, int.class);
touchUpMethod.setAccessible(true);
if(Boolean.parseBoolean(touchDownMethod.invoke(interactionController, rect.centerX(), rect.centerY()).toString())) {
SystemClock.sleep(time);
if(Boolean.parseBoolean(touchUpMethod.invoke(interactionController, rect.centerX(), rect.centerY()).toString())) {
return true;
}
}
return false;
}
调用方式:
在使用uiautomator自动化框架时,调用longClickWithTime(Object, time)方法来实现对UiObject控件长按效果。其中Object参数:uiautomator UiObject控件对象,time:表示长按时间,单位为毫秒。
例如:对手机主页时钟按钮进行长按5s操作,
longClickWithTime(new UiCollection(new UiObject(new UiSelector().text("Clock")), 5000);
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11