appium目前最新的windows版本是1.4.16,在android8.0.0真机上测试脚本时会报错:command failed shell “ps ‘uiautomator’”。
刚开始以为错误原因是模拟器系统和真机系统版本没对应上(小菜,所以在猜原因),因为运行的时候,提示:Device is at API Level 26。后面更新了模拟器系统版本后,错误原因有部分不一样了,但实质问题没修复。后面查了很多资料,综合网上各位大神,解决办法如下:
1、找到appium的安装目录下的adb.js文件,目录为:Appium\node_modules\appium\node_modules\appium-adb\lib
2、打开adb.js,找到如下代码:
ADB.prototype.shell = function (cmd, cb) {
if (cmd.indexOf('"') === -1) {
cmd = '"' + cmd + '"';
}
var execCmd = 'shell ' + cmd;
this.exec(execCmd, cb);
};
在这段代码下面加入这段代码:
ADB.prototype.shell_grep = function (cmd, grep, cb) {
if (cmd.indexOf('"') === -1) {
cmd = '"' + cmd + '"';
}
var execCmd = 'shell ' + cmd + '| grep ' + grep;
this.exec(execCmd, cb);
};
再找到如下代码:
ADB.prototype.getPIDsByName = function (name, cb) {
logger.debug("Getting all processes with '" + name + "'");
this.shell("ps '" + name + "'", function (err, stdout) {
if (err) return cb(err);
stdout = stdout.trim();
var procs = [];
var outlines = stdout.split("\n");
outlines.shift();
_.each(outlines, function (outline) {
if (outline.indexOf(name) !== -1) {
procs.push(outline);
}
});
if (procs.length < 1) {
logger.debug("No matching processes found");
return cb(null, []);
}
var pids = [];
_.each(procs, function (proc) {
var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);
if (match) {
pids.push(parseInt(match[1], 10));
}
});
if (pids.length !== procs.length) {
var msg = "Could not extract PIDs from ps output. PIDS: " +
JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);
return cb(new Error(msg));
}
cb(null, pids);
});
};
把这段代码注释掉(注释方式/* */),用如下代码代替:
ADB.prototype.getPIDsByName = function (name, cb) {
logger.debug("Getting all processes with '" + name + "'");
this.shell_grep("ps", name, function (err, stdout) {
if (err) {
logger.debug("No matching processes found");
return cb(null, []);
}
var pids = [];
_.each(procs, function (proc) {
var match = /[^\t ]+[\t ]+([0-9]+)/.exec(proc);
if (match) {
pids.push(parseInt(match[1], 10));
}
});
if (pids.length !== procs.length) {
var msg = "Could not extract PIDs from ps output. PIDS: " +
JSON.stringify(pids) + ", Procs: " + JSON.stringify(procs);
return cb(new Error(msg));
}
cb(null, pids);
});
};
3、重启appium
本以为问题解决了,因为首次运行脚本的时候,手机上“Appium Settings”和“Unlock”这两个应用是没有安装的,但是第二次运行的时候报错了,如下:
selenium.common.exceptions.SessionNotCreatedException: Message: A new session could not be created. (Original error: Command failed:
C:\Windows\system32\cmd.exe /s /c "D:\Appium\Android_SDK\android-sdk-windows\platform-tools\adb.exe -s MKJDU18818001594 install
"D:\Appium\Appium\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk""
adb: failed to install D:\Appium\Appium\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS:
Attempt to re-install io.appium.settings without first uninstalling.]
)
Appium上报错如下:
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created.
(Original error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"D:\\Appium\\Android_SDK\\android-sdk-windows\\platform-tools\\adb.exe -s
MKJDU18818001594 install \"D:\\Appium\\Appium\\Appium\\node_modules\\appium\\build\\settings_apk\\settings_apk-debug.apk\"\"\nadb: failed to install
D:\\Appium\\Appium\\Appium\\node_modules\\appium\\build\\settings_apk\\settings_apk-debug.apk:
Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]\r\n)","killed":false,"code":1,"signal":null,"cmd":"C:\\Windows\\system32\\cmd.exe
/s /c \"D:\\Appium\\Android_SDK\\android-sdk-windows\\platform-tools\\adb.exe -s MKJDU18818001594 install \"D:\\Appium\\Appium\\Appium\\node_modules\\appium\\build\\settings_apk\\settings_apk-debug.apk\"\"",
"origValue":"Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"D:\\Appium\\Android_SDK\\android-sdk-windows\\platform-tools\\adb.exe -s MKJDU18818001594 install \"D:\\Appium\\Appium\\Appium\\node_modules\\appium\\build\\settings_apk\\settings_apk-debug.apk\"\"\nadb: failed to install
D:\\Appium\\Appium\\Appium\\node_modules\\appium\\build\\settings_apk\\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS:
Attempt to re-install io.appium.settings without first uninstalling.]\r\n"},"sessionId":null}
“ps ‘uiautomator’”报错是没有了,解决办法如下:
解决方法1:
这样每次运行都得删除掉这两个应用,不然依旧报错。
解决方法2:
在window 系统下,找到appium 安装目录
Appium\node_modules\appium\lib\devices\android
找到Android.js 文件,注释下述的三行代码即可:(颜色为天蓝色的代码行)
重启,Appium。这个方法有另外一个问题,如果,真机上没安装这两个“Appium Settings”和“Unlock”应用,将不会被安装了。
暂时未能找到其他方法。