手机自动化测试:appium源码分析之bootstrap十四

 

poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

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这个动作松开键盘键。从而达到了我们到长按到要求。

手机自动化测试:appium源码分析之bootstrap十四的更多相关文章

  1. 手机自动化测试:appium源码分析之bootstrap十六

    手机自动化测试:appium源码分析之bootstrap十六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  2. 手机自动化测试:appium源码分析之bootstrap十五

    手机自动化测试:appium源码分析之bootstrap十五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  3. 手机自动化测试:appium源码分析之bootstrap十二

    手机自动化测试:appium源码分析之bootstrap十二   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  4. 手机自动化测试:appium源码分析之bootstrap十

    手机自动化测试:appium源码分析之bootstrap十   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣, ...

  5. 手机自动化测试:appium源码分析之bootstrap三

    手机自动化测试:appium源码分析之bootstrap三   研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...

  6. 手机自动化测试:appium源码分析之bootstrap二

    手机自动化测试:appium源码分析之bootstrap二   在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...

  7. 手机自动化测试:appium源码分析之bootstrap一

    手机自动化测试:appium源码分析之bootstrap一   前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...

  8. 手机自动化测试:appium源码分析之bootstrap十七

    手机自动化测试:appium源码分析之bootstrap十七   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  9. 手机自动化测试:appium源码分析之bootstrap十三

    手机自动化测试:appium源码分析之bootstrap十三   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

随机推荐

  1. css3动画animate

    CSS3 动画 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. @keyframes 定义动画关键帧: @keyframes anima ...

  2. devexpress表格控件gridcontrol图片列,按钮列,时间列等特殊列的实现

    1.项目中经常会在表格中插入按钮列,图片列,表格列一些非文本的特殊列.如何在devexpress表格控件gridcontrol中实现呢?以下列举一个实现添加图片列,按钮列,时间列,按钮列,开关列的示例 ...

  3. 锋利的jQuery中的事件与动画

    奋夜的奋斗  ----  事件与动画 ----  来自地狱的战镰 小小的单词难不倒我们哦!!!!!!!    bind:绑定     unbind:接触绑定    toggle:栓牢   fadeou ...

  4. IOS缓存管理之YYCache使用

    前言: 最近一直在致力于为公司app添加缓存功能,为了寻找一个最佳方案,这几天先做个技术预研,经过这两天的查找资料基本上确定了两个开源框架进行选择,这两个开源框架分别是:PINCache.YYCach ...

  5. 每天一个Linux命令(15)--tail命令

    tail 命令从指定点开始将文件写到标准输出.使用  tail  命令的  -f  选项可以方便的查阅正在改变的日志文件 , tail  -f  filename  会把  filename  里最尾 ...

  6. mvc路由

    一.路由常规设置 1.URL模式     路由系统用一组路由来实现它的功能.这些路由共同组成了应用程序的URL架构或方案. URL的两个关键行为:     a.URL模式是保守的,因而只匹配与模式具有 ...

  7. 基于vue的多引擎搜索及关键字提示

    关键代码: <div class="header-search"> <form id="form" action="http://m ...

  8. java基础:学生管理系统

    package com.lovo.manager; import java.util.Scanner; /** * 学生管理 * * @author 向往的生活 * */public class St ...

  9. Java豆瓣电影爬虫——模拟登录的前世今生与验证码的爱恨情仇

    前言 并不是所有的网站都能够敞开心扉让你看个透彻,它们总要给你出些难题让你觉得有些东西是来之不易的,往往,这也更加激发你的激情和斗志! 从<为了媳妇的一张号,我与百度医生杠上了>里就有网友 ...

  10. Reverse Words in a String leetcode

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...