一、運行Appium,執行測試腳本,報錯信息爲:error: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
如圖所示:
原因分析:
這種錯誤並不是ANDROID_HOME環境變量沒有配置,也不是配置有問題(你要確定自己的ANDROID_HOME環境變量一定要配對)
解決辦法:
以管理員身份運行Appium即可
二、出錯信息裏含有"ps 'uiautomator',具體信息爲 A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c "C:\Users\sxie\AppData\Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell "ps 'uiautomator'"",如圖所示:
這種情況,直接在cmd中執行"C:\Windows\system32\cmd.exe /s /c "C:\Users\sxie\AppData\Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell "ps 'uiautomator'"", 報錯爲: bad pid 'uiautomator'
原因分析:
對應執行的指令是ps 'uiautomator', Android7不支持這個指令格式,所以執行結果是bad pid 'uiautomator';
目前Appium未對此進行處理,所以需要修改此指令的執行方式
解決辦法(重點,重點,本人親自嘗試,保證解決問題):
1.打開adb.js(路徑:X:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js)
2.找到第1035 行this.shell,也就是:
this.shell("ps '" + name + "'", function (err, stdout) {
if (err) return cb(err);
將此部分代碼替換爲:
this.shell_grep("ps", name, function (err, stdout) {
if (err) {
logger.debug("No matching processes found");
return cb(null, []);
}
3.替換完成後,在此代碼的上邊加入如下代碼:
ADB.prototype.shell_grep = function (cmd, grep, cb) {
if (cmd.indexOf('"') === -1) {
cmd = '"' + cmd + '"';
}
var execCmd = 'shell ' + cmd + '| grep ' + grep;
this.exec(execCmd, cb);
};
4.修改後的adb.js如圖所示(注意符號,空格之類容易報錯的地,切記!切記!):
5.再次運行腳本,成功解決此問題,如圖所示:
三、第二次運行python腳本時,appium報錯如下:
報錯詳細信息爲:
Error: Command failed: C:\WINDOWS\system32\cmd.exe /s /c ""D:\Program Files\android-sdk-windows\platform-tools\adb.exe" -s HMKNW17414008402 install "D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk""
> adb: failed to install D:\Program Files (x86)\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運行python腳本時,當連接手機(真機)之後,使用appium在執行app自動化測試時,是會在手機上安裝2個應用程序的,分別是:AppiumSettings和Unlock,也就是說在運行腳本的時候需要手動去刪除這兩個程序,我們先打開cmd看下手機都裝了哪些程序,命令:adb shell pm list package,如圖所示:
確定手機中確實安裝了這兩個程序之後,我們手動將其卸載即可
命令:adb uninstall io.appium.unlock adb uninstall io.appium.settings
如圖所示,提示success,則卸載成功:
再次運行腳本,完美解決此問題: