最近将appium更新到了1.6.5,uiautomator2确实能实现对Android toast内容的断言。但后续发现之前的
wd.pressDeviceKey()
方法不起作用。
更新
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。
解决
知道了问题原因,可以想到三种解决方案:
1.修改uiautomator2 server
对appium编译安装uiautomator2 server的过程没深入研究,尝试卸载原手机里的io.appium.uiautomator2.server程序,修改AppiumServlet,并没有起作用,所以这个解决方法我还是等作者吧。。
更新
感谢Carl的提示,原来appium-uiautomator2-server项目的Readme已经给出编译方法。
- AS打开appium-uiautomator2-server项目,完成上面提到的AppiumServelt代码的修改
- 运行
gradle clean assembleServerDebug assembleServerDebugAndroidTest
,这里编译可能会遇到一些问题,我遇到的是:app的build.gradle中,配置了对sdk-manager-plugin的依赖,而阿里云的maven库里并没有收录这个项目。解决方法: 将相关配置的代码注释掉 - gradle编译成功后,会在/app/build/outputs/apk/下生产两个apk:
1.appium-uiautomator2-server-v0.1.5.APK,执行命令的apk,(GitHub上已经更新到0.1.6)
2.appium-uiautomator2-server-debug-androidTest.apk,用于启动Server的apk - 替换设备中的apk,可以手动安装,也可以替换appium下默认的apk。默认apk放在appium-uiautomator2-driver的uiautomator2目录下,替换之。还未结束,查看driver里的uiautomator2.js代码:
async installServerApk () {
// Installs the apks on to the device or emulator
let apkPackage = await this.getPackageName(apkPath);
// appending .test to apkPackage name to get test apk package name
let testApkPackage = apkPackage + '.test';
let isApkInstalled = await this.adb.isAppInstalled(apkPackage);
let isTestApkInstalled = await this.adb.isAppInstalled(testApkPackage);
if (isApkInstalled || isTestApkInstalled) {
//check server apk versionName
let apkVersion = await this.getAPKVersion(apkPath);
let pkgVersion = await this.getInstalledPackageVersion(apkPackage);
if (apkVersion !== pkgVersion) {
isApkInstalled = false;
isTestApkInstalled = false;
await this.adb.uninstallApk(apkPackage);
await this.adb.uninstallApk(testApkPackage);
}
}
if (!isApkInstalled) {
await this.signAndInstall(apkPath, apkPackage);
}
if (!isTestApkInstalled) {
await this.signAndInstall(testApkPath, testApkPackage);
}
}
uiautomator2-driver会去校验设备是否已安装以及对版本号进行判断,所以想替换设备里的版本,还需修改你编译apk的version,或者提前删除设备已经安装的。
2.修改wd代码
既然uiautomator2-Server路由中只有press_keycode
,没有keyevent
,那可以在wd里添加相应的Post方法。
找到你项目中node_modules里wd模块,在/lib/commands.js中添加代码如下:
/**
* pressKeyCode(keycode, metastate, cb) -> cb(err)
* metastate is optional
*
* @jsonWire POST /session/:sessionId/appium/device/press_keycode
*/
commands.pressKeyCode = function() {
var fargs = utils.varargs(arguments);
var cb = fargs.callback,
keycode = fargs.all[0],
metastate = fargs.all[1];
var data = {keycode: keycode};
if(metastate) { data.metastate = metastate; }
this._jsonWireCall({
method: 'POST'
, relPath: '/appium/device/press_keycode'
, data: data
, cb: simpleCallback(cb)
});
};
重启项目后,driver就可以调用pressKeyCode方法,appium server会将请求转发到/press_keycode,uiautomator2 server也就可以识别。
3.修改uiautomator2-driver
我们在分析appium-uiautomator2-driver时,发现作者其实是有写keyevent方法的,只不过调用的是adb:
// uiautomator2 doesn't support metastate for keyevents
commands.keyevent = async function (keycode, metastate) {
log.debug(`Ignoring metastate ${metastate}`);
await this.adb.keyevent(keycode);
};
那么怎么才能让appium直接调用这个方法呢,我们发现uiautomator2-driver的driver.js里有个数组:
// NO_PROXY contains the paths that we never want to proxy to UiAutomator2 server.
// TODO: Add the list of paths that we never want to proxy to UiAutomator2 server.
// TODO: Need to segregate the paths better way using regular expressions wherever applicable.
// (Not segregating right away because more paths to be added in the NO_PROXY list)
const NO_PROXY = [
['POST', new RegExp('^/session/[^/]+/touch/multi/perform')],
['POST', new RegExp('^/session/[^/]+/touch/perform')],
['POST', new RegExp('^/session/[^/]+/element')],
['POST', new RegExp('^/session/[^/]+/appium/element/[^/]+/value')],
['POST', new RegExp('^/session/[^/]+/appium/element/[^/]+/replace_value')],
['GET', new RegExp('^/session/[^/]+/appium/[^/]+/current_activity')],
['POST', new RegExp('^/session/[^/]+/appium/[^/]+/start_activity')],
['POST', new RegExp('^/session/[^/]+/app/[^/]')],
['POST', new RegExp('^/session/[^/]+/location')],
['GET', new RegExp('^/session/[^/]+/appium/device/system_time')],
['POST', new RegExp('^/session/[^/]+/appium/settings')],
['GET', new RegExp('^/session/[^/]+/appium/settings')],
['POST', new RegExp('^/session/[^/]+/appium/device/app_installed')],
['POST', new RegExp('^/session/[^/]+/appium/device/lock')],
['POST', new RegExp('^/session/[^/]+/appium/app/close')],
['POST', new RegExp('^/session/[^/]+/appium/app/launch')],
['POST', new RegExp('^/session/[^/]+/appium/device/pull_file')],
['POST', new RegExp('^/session/[^/]+/appium/device/push_file')],
['POST', new RegExp('^/session/[^/]+/appium/app/reset')],
['POST', new RegExp('^/session/[^/]+/appium/app/background')],
['POST', new RegExp('^/session/[^/]+/appium/device/toggle_location_services')],
['POST', new RegExp('^/session/[^/]+/appium/device/is_locked')],
['POST', new RegExp('^/session/[^/]+/appium/device/unlock')],
['POST', new RegExp('^/session/[^/]+/appium/app/end_test_coverage')],
['GET', new RegExp('^/session/[^/]+/contexts')],
['POST', new RegExp('^/session/[^/]+/context')],
['GET', new RegExp('^/session/[^/]+/context')],
['POST', new RegExp('^/session/[^/]+/network_connection')],
['GET', new RegExp('^/session/[^/]+/network_connection')],
['POST', new RegExp('^/session/[^/]+/timeouts')],
['GET', new RegExp('^/session/[^/]+/screenshot')],
['GET', new RegExp('^/session/[^/]+/element/[^/]+/attribute')],
['GET', new RegExp('^/session/[^/]+/element/[^/]+/enabled')],
['GET', new RegExp('^/session/[^/]+/element/[^/]+/selected')],
['GET', new RegExp('^/session/[^/]+/element/[^/]+/displayed')],
['GET', new RegExp('^/session/[^/]+/element/[^/]+/name')],
['GET', new RegExp('^/session/(?!.*\/)')],
['POST', new RegExp('^/session/[^/]+/keys')],
['POST', new RegExp('^/session/[^/]+/appium/device/hide_keyboard')],
['POST', new RegExp('^/session/[^/]+/log')],
['POST', new RegExp('^/session/[^/]+/appium/device/remove_app')],
['GET', new RegExp('^/session/[^/]+/appium/device/is_keyboard_shown')]
];
注释里写了, 该数组维护的是不会转发到uiautomator2的路由,那么,我们只需要在该数组里添加:
['POST', new RegExp('^/session/[^/]+/appium/device/keyevent')]
。
p.s,该方法修改的是appium server依赖的appium-uiatumator2-driver,修改后需通过命令行启动appium,桌面程序似乎是带有缓存自制的,直接运行修改的代码不会起作用。