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:
在真機上 手動卸載 AppimSetting 和 Unlock 兩個應用;
這樣每次運行都得刪除掉這兩個應用,不然依舊報錯。
解決方法2:
在window 系統下,找到appium 安裝目錄
Appium\node_modules\appium\lib\devices\android
找到Android.js 文件,註釋下述的三行代碼即可:(顏色爲天藍色的代碼行)
重啓,Appium。這個方法有另外一個問題,如果,真機上沒安裝這兩個“Appium Settings”和“Unlock”應用,將不會被安裝了。
暫時未能找到其他方法。