bootstrap之PressKeyCode&&LongPressKeyCode
PressKeyCode
package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice;
import io.appium.android.bootstrap.AndroidCommand;
import io.appium.android.bootstrap.AndroidCommandResult;
import io.appium.android.bootstrap.CommandHandler;
import org.json.JSONException;
import org.json.JSONObject; import java.util.Hashtable; /**
* This handler is used to PressKeyCode.
*
*/
public class PressKeyCode extends CommandHandler {
public Integer keyCode;
public Integer metaState; /*
* @param command The {@link AndroidCommand} used for this handler.
*
* @return {@link AndroidCommandResult}
*
* @throws JSONException
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
try {
final Hashtable<String, Object> params = command.params();
Object kc = params.get("keycode");
if (kc instanceof Integer) {
keyCode = (Integer) kc;
} else if (kc instanceof String) {
keyCode = Integer.parseInt((String) kc);
} else {
throw new IllegalArgumentException("Keycode of type " + kc.getClass() + "not supported.");
} if (params.get("metastate") != JSONObject.NULL) {
metaState = (Integer) params.get("metastate");
UiDevice.getInstance().pressKeyCode(keyCode, metaState);
} else {
UiDevice.getInstance().pressKeyCode(keyCode);
} return getSuccessResult(true);
} catch (final Exception e) {
return getErrorResult(e.getMessage());
}
}
}
有的时候手机键盘的一些键须要按。可是又没有像pressBack这样的方法供我们直接调用,这个时候我们就须要发送键盘的keycode来模拟这些键被点击。
所以PressKeyCode就是封装这类事件的,通过上面的代码能够看出,发送keycode分2类事件。每一类调用的方法不一样
- 单点键:pressKeyCode(keyCode,metaState)
- 组合键: pressKeyCode(keyCode,metaState)
LongPressKeyCode
package io.appium.android.bootstrap.handler; import android.os.SystemClock;
import android.view.InputDevice;
import android.view.InputEvent;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import com.android.uiautomator.common.ReflectionUtils;
import io.appium.android.bootstrap.AndroidCommand;
import io.appium.android.bootstrap.AndroidCommandResult;
import io.appium.android.bootstrap.CommandHandler;
import org.json.JSONException;
import org.json.JSONObject; import java.lang.reflect.Method;
import java.util.Hashtable; /**
* This handler is used to LongPressKeyCode.
*
*/
public class LongPressKeyCode extends CommandHandler {
public Integer keyCode; public Integer metaState; /*
* @param command The {@link AndroidCommand} used for this handler.
*
* @return {@link AndroidCommandResult}
*
* @throws JSONException
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command)
throws JSONException {
try {
final ReflectionUtils utils = new ReflectionUtils();
final Method injectEventSync = utils.getControllerMethod("injectEventSync",
InputEvent.class);
final Hashtable<String, Object> params = command.params();
keyCode = (Integer) params.get("keycode");
metaState = params.get("metastate") != JSONObject.NULL ? (Integer) params
.get("metastate") : 0;
final long eventTime = SystemClock.uptimeMillis();
// Send an initial down event
final KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, keyCode, 0, metaState,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
if ((Boolean) injectEventSync.invoke(utils.getController(), downEvent)) {
// Send a repeat event. This will cause the FLAG_LONG_PRESS to be set.
final KeyEvent repeatEvent = KeyEvent.changeTimeRepeat(downEvent,
eventTime, 1);
injectEventSync.invoke(utils.getController(), repeatEvent);
// Finally, send the up event
final KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_UP, keyCode, 0, metaState,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
injectEventSync.invoke(utils.getController(), upEvent);
}
return getSuccessResult(true);
} catch (final Exception e) {
return getErrorResult(e.getMessage());
}
}
}
长点击某个键盘键,和上面的单击是有差别的,由于没有直接的API能够调用。所以又要用到反射来做这件事,这次的反射呢调用的是InteractionController中的injectEventSync方法。首先会运行ACTION_DOWN,然后在一段时间里会反复运行这个down的动作,等事件到了以后。运行ACTION_UP这个动作松开键盘键。从而达到了我们到长按到要求。
bootstrap之PressKeyCode&&LongPressKeyCode的更多相关文章
- bootstrap之Click大事
上一篇文章中谈到了bootstrap流程,本文开始把目光bootstrap它可以接受指令(从源代码视图的透视.因为appium该项目现在还处于不断更新,因此,一些指令已经实现.也许未来会实现一些.从视 ...
- Appium Android Bootstrap源码分析之命令解析执行
通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...
- Appuim源码剖析(Bootstrap)
Appuim源码剖析(Bootstrap) SkySeraph Jan. 26th 2017 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www. ...
- 手机自动化测试:appium源码分析之bootstrap三
手机自动化测试:appium源码分析之bootstrap三 研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...
- 手机自动化测试:appium源码分析之bootstrap二
手机自动化测试:appium源码分析之bootstrap二 在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...
- 手机自动化测试:appium源码分析之bootstrap一
手机自动化测试:appium源码分析之bootstrap一 前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...
- 手机自动化测试:appium源码分析之bootstrap十四
手机自动化测试:appium源码分析之bootstrap十四 poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...
- appium框架之bootstrap
(闲来无事,做做测试..)最近弄了弄appium,感觉挺有意思,就深入研究了下. 看小弟这篇文章之前,先了解一下appium的架构,对你理解有好处,推荐下面这篇文章:testerhome appium ...
- 旺财速啃H5框架之Bootstrap(五)
在上一篇<<旺财速啃H5框架之Bootstrap(四)>>做了基本的框架,<<旺财速啃H5框架之Bootstrap(二)>>篇里也大体认识了bootst ...
随机推荐
- 使用matlab判断男声与女声
(转自) http://wenku.baidu.com/view/1d55480fbe1e650e52ea99a3.html %filename:manwoman.m %different man f ...
- 动态内存管理详解:malloc/free/new/delete/brk/mmap
c++ 内存获取和释放 new/delete,new[]/delete[] c 内存获取和释放 malloc/free, calloc/realloc 上述8个函数/操作符是c/c++语言里常用来做动 ...
- 基于UDP高性能传输协议UDT doc翻译(一)
原文转自:http://hi.baidu.com/doodlezone/item/74a203155efe26dbbf9042dd UDT文档阅读理解 一. 概述 ...
- 在C#中将数字转换成中文
上篇我们讲了在MSSQL中将数字转换成中文,这篇我们讲讲在C#中将数字转换成中文 下篇将讲一下如何将金额转换成中文金额,废话不多说,具体代码如下: /// <summary> /// 数字 ...
- UVA 1151 Buy or Build MST(最小生成树)
题意: 在平面上有n个点,要让所有n个点都连通,所以你要构造一些边来连通他们,连通的费用等于两个端点的欧几里得距离的平方.另外还有q个套餐,可以购买,如果你购买了第i个套餐,该套餐中的所有结点将变得相 ...
- AtCoder Grand Contest 012 B Splatter Painting (反向处理 + 记忆化)
题目链接 agc012 Problem B 题意 给定一个$n$个点$m$条边的无向图,现在有$q$个操作.对距离$v$不超过$d$的所有点染色,颜色编号为$c$. 求每个点最后的颜色状态. 倒过 ...
- Xamarin XAML语言教程通过ProgressTo方法对进度条设置
Xamarin XAML语言教程通过ProgressTo方法对进度条设置 在ProgressBar中定义了一个ProgressTo方法,此方法也可以用来对进度条当前的进行进行设置,ProgressTo ...
- make: ./libtool:命令未找到
make: ./libtool:命令未找到 问题描述: [root@bogon jpeg-6b]# ; make install./libtool --mode=compile gcc -O2 -I. ...
- UBIFS介绍 - MTD网站
转:http://blog.csdn.net/kickxxx/article/details/6583463 目录(?)[-] Big red note Overview Scalabity Writ ...
- 编译qt提示找不到gmake
转:http://wuyuans.com/2012/11/gmake-not-found/ 在用debian编译qt4.5的时候提示gmake: not found,gmake是什么东西,用aptit ...