更新
wd@1.3.0添加了press_keycode的路由,修复了这个bug
Replace the deprecated sendKeyEvent API with pressKeycode for Appium POST /session/:sessionId/appium/device/press_keycode Send key event to device (mjsonWire). pressKeycode(keycode, metastate, cb) -> cb(err) metastate is optional. |
问题
首先分析appium server的日志:
2017-07-04 10:10:14:838 - info: [HTTP] --> POST /wd/hub/session/744e508b-3f7c-446b-b75f-a4373b0da0c1/appium/device/keyevent {"keycode":4} 2017-07-04 10:10:14:838 - info: [MJSONWP] Driver proxy active, passing request on via HTTP proxy 2017-07-04 10:10:14:838 - info: [debug] [JSONWP Proxy] Proxying [POST /wd/hub/session/744e508b-3f7c-446b-b75f-a4373b0da0c1/appium/device/keyevent] to [POST http://localhost:8200/wd/hub/session/3705f215-26ac-409d-a89d-c9ab578af2b4/appium/device/keyevent] with body: {"keycode":4} 2017-07-04 10:10:14:947 - error: [MJSONWP] Encountered internal error running command: Error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: 404 - undefined at doJwpProxy$ (C:\Users\lichen2\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\mjsonwp\mjsonwp.js:354:13) at tryCatch (C:\Users\lichen2\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\lichen2\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\lichen2\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) at GeneratorFunctionPrototype.invoke (C:\Users\lichen2\AppData\Local\Programs\appium-desktop\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) 2017-07-04 10:10:14:947 - info: [HTTP] <-- POST /wd/hub/session/744e508b-3f7c-446b-b75f-a4373b0da0c1/appium/device/keyevent 500 119 ms - 274 |
日志显示,Appium server接收到了keyevent的POST请求,但转发出去时: Error: Could not proxy. Proxy error: Could not proxy command to remote server. Original error: 404 - undefined,很明显,404表示设备端的sever没有响应该请求。
原因
出了问题,第一反应是去appium上翻Issue,确实有人提过同样的问题,但作者并没有给出Answer:
Can't use sendKeyEvent。
于是只能自己翻源码了,因为问题出在appium server发POST到uiautomator2 server时404,所以直接去appium-uiautomator2-server项目里看:
找到文件AppiumServelt,该类看上去是注册路由的:
private void registerPostHandler() { register(postHandler, new NewSession("/wd/hub/session")); register(postHandler, new FindElement("/wd/hub/session/:sessionId/element")); register(postHandler, new FindElements("/wd/hub/session/:sessionId/elements")); register(postHandler, new Click("/wd/hub/session/:sessionId/element/:id/click")); register(postHandler, new Click("/wd/hub/session/:sessionId/appium/tap")); register(postHandler, new Clear("/wd/hub/session/:sessionId/element/:id/clear")); register(postHandler, new RotateScreen("/wd/hub/session/:sessionId/orientation")); register(postHandler, new RotateScreen("/wd/hub/session/:sessionId/rotation")); register(postHandler, new PressBack("/wd/hub/session/:sessionId/back")); register(postHandler, new SendKeysToElement("/wd/hub/session/:sessionId/element/:id/value")); register(postHandler, new SendKeysToElement("/wd/hub/session/:sessionId/keys")); register(postHandler, new Swipe("/wd/hub/session/:sessionId/touch/perform")); register(postHandler, new TouchLongClick("/wd/hub/session/:sessionId/touch/longclick")); register(postHandler, new OpenNotification("/wd/hub/session/:sessionId/appium/device/open_notifications")); register(postHandler, new PressKeyCode("/wd/hub/session/:sessionId/appium/device/press_keycode")); register(postHandler, new LongPressKeyCode("/wd/hub/session/:sessionId/appium/device/long_press_keycode")); register(postHandler, new Drag("/wd/hub/session/:sessionId/touch/drag")); register(postHandler, new AppStrings("/wd/hub/session/:sessionId/appium/app/strings")); register(postHandler, new Flick("/wd/hub/session/:sessionId/touch/flick")); register(postHandler, new ScrollTo("/wd/hub/session/:sessionId/touch/scroll")); register(postHandler, new MultiPointerGesture("/wd/hub/session/:sessionId/touch/multi/perform")); register(postHandler, new TouchDown("/wd/hub/session/:sessionId/touch/down")); register(postHandler, new TouchUp("/wd/hub/session/:sessionId/touch/up")); register(postHandler, new TouchMove("/wd/hub/session/:sessionId/touch/move")); register(postHandler, new UpdateSettings("/wd/hub/session/:sessionId/appium/settings")); register(postHandler, new NetworkConnection("/wd/hub/session/:sessionId/network_connection")); } |
发现上面代码里并没有对应wd: POST /session/:sessionId/appium/device/keyevent,只有register(postHandler, new PressKeyCode("/wd/hub/session/:sessionId/appium/device/press_keycode"));
所以,404的原因找到了,笔者也在上面的Issue里给作者提了,希望后面作者能更新下uiautomator2 server。