Appium Appium 中使用 pressKeyCode 方法不起作用也没有报错 [已解决]

KD · 2018年03月13日 · 最后由 KD 回复于 2018年03月22日 · 2715 次阅读

问题详细描述:

为了使 appium 支持 Android 系统 7 及以上,automationName 使用了 UIAutomator2。但是发现,使用 androidDriver.pressKeyCode(AndroidKeyCode.ENTER) 方法时,页面没有回车搜索,没有达到预期模拟键盘中回车的效果,查看 server 日志也没有报错信息。如果将 automationName 改为 Appium 则正常。
百思不得其解,希望各位可以指点迷津。

以下是已尝试解决办法,但都未生效:

  • androidDriver.pressKeyCode(AndroidKeyCode.SEARCH)
  • androidDriver.pressKeyCode(66)
  • androidDriver.pressKeyCode(84)

环境描述:

appium server : v1.7.2
<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>5.0.4</version>
</dependency>
  • appium 部分初始化属性值如下:

    serverURL=http://127.0.0.1:4723/wd/hub
    automationName=UIAutomator2
    platformName=Android
    platformVersion=5.1.1
    deviceName=vivo X6SPlus D
    
  • keyboardSearchr 方法如下:

    /* 模拟点击键盘回车 */
    public void keyboardSearch() {
        AndroidDriver androidDriver = convertAndroidDriver();
        if (androidDriver != null) {
            androidDriver.pressKeyCode(AndroidKeyCode.ENTER);
        }
    }
    
  • appium server 日志如下:

    [2018-03-13 06:48:03][HTTP] <-- POST /wd/hub/session/32430c80-b93a-4ce9-897b-adf882a2296e/element/161af97a-b351-4c8b-a3e5-be4dfc7ae386/value 200 57 ms - 74   
    [2018-03-13 06:48:03][HTTP] --> POST /wd/hub/session/32430c80-b93a-4ce9-897b-adf882a2296e/appium/device/press_keycode {"keycode":66} 
    [2018-03-13 06:48:03][MJSONWP] Calling AppiumDriver.pressKeyCode() with args: [66,null,null,null,"32430c80-b93a-4ce9-897b-adf882a2296e"] 
    [2018-03-13 06:48:03][JSONWP Proxy] Proxying [POST /appium/device/press_keycode] to [POST http://localhost:8206/wd/hub/session/9464ba9e-8b73-46c6-81db-91299359c318/appium/device/press_keycode] with body: {"keycode":66,"metastate":null}
    [2018-03-13 06:48:04][JSONWP Proxy] Got response with status 200: {"sessionId":"9464ba9e-8b73-46c6-81db-91299359c318","status":0,"value":true}
    [2018-03-13 06:48:04][MJSONWP] Responding to client with driver.pressKeyCode() result: true
    [2018-03-13 06:48:04][HTTP] <-- POST /wd/hub/session/32430c80-b93a-4ce9-897b-adf882a2296e/appium/device/press_keycode 200 626 ms - 76 
    [2018-03-13 06:49:04][BaseDriver] Shutting down because we waited 60 seconds for a command
    [2018-03-13 06:49:04][UiAutomator2] Deleting UiAutomator2 session
    


  • 最终解决方法:

使用 UIA2 过程中,在进行 input 操作时,不知道什么原因,焦点不在输入框上,导致 pressKeyCode() 方法无效。(怀疑是 UIA2 框架的小 bug)

可在调用pressKeyCode()方法时,先重新 click 一下 input 输入框,然后执行 pressKeyCode() 方法即可。
在 input 和 click 操作之后,不需要调用第三方输入法,继续使用 io.appium.android.ime/.UnicodeIME 即可。

共收到 4 条回复 时间 点赞
KD 回复

可以尝试安装个搜狗输入法,adb 命令切换到搜狗输入法,再使用 presskeycode

KD 回复

楼上的方法应该可以,你试下

KD #4 · 2018年03月22日 Author

@slideplustest @xiaozi2400-github
试了在需要 pressKeyCode(66) 时,先切换到第三方输入法,再使用 pressKeyCode(66) 方法。
结果还是一样,server 无报错信息,但是并未生效。

启用第三方输入法

/**
 * 启用华为百度输入法
 */
public void setHuaWeiInput() {
    logger.info("setHuaWeiInput 启用华为百度输入法");
    excuteAdbShell("adb shell ime set com.baidu.input_huawei/.ImeService");
}

server 端日志如下

[HTTP] <-- POST /wd/hub/session/976abb7b-e49d-4318-bd28-223ee348804d/element/2ae448f9-e1e0-418d-b1a0-fb3a7cf7980c/value 200 675 ms - 74 
[HTTP] --> POST /wd/hub/session/976abb7b-e49d-4318-bd28-223ee348804d/appium/device/press_keycode {"keycode":66}
[MJSONWP] Calling AppiumDriver.pressKeyCode() with args: [66,null,null,null,"976abb7b-e49d-4318-bd28-223ee348804d"]
[JSONWP Proxy] Proxying [POST /appium/device/press_keycode] to [POST http://localhost:8201/wd/hub/session/04125e0b-739c-4d71-a822-efe2cd90baad/appium/device/press_keycode] with body: {"keycode":66,"metastate":null}
[JSONWP Proxy] Got response with status 200: {"sessionId":"04125e0b-739c-4d71-a822-efe2cd90baad","status":0,"value":true}
[MJSONWP] Responding to client with driver.pressKeyCode() result: true
[HTTP] <-- POST /wd/hub/session/976abb7b-e49d-4318-bd28-223ee348804d/appium/device/press_keycode 200 589 ms - 76 
[HTTP] --> POST /wd/hub/session/976abb7b-e49d-4318-bd28-223ee348804d/appium/device/press_keycode {"keycode":66}
[MJSONWP] Calling AppiumDriver.pressKeyCode() with args: [66,null,null,null,"976abb7b-e49d-4318-bd28-223ee348804d"]
[JSONWP Proxy] Proxying [POST /appium/device/press_keycode] to [POST http://localhost:8201/wd/hub/session/04125e0b-739c-4d71-a822-efe2cd90baad/appium/device/press_keycode] with body: {"keycode":66,"metastate":null}
[JSONWP Proxy] Got response with status 200: {"sessionId":"04125e0b-739c-4d71-a822-efe2cd90baad","status":0,"value":true}
[MJSONWP] Responding to client with driver.pressKeyCode() result: true

KD [该话题已被删除] 中提及了此贴 03月22日 13:55
KD 关闭了讨论 03月23日 13:09
KD #8 · 2018年03月14日 Author

求指点迷津,感谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册