Appium在Android7.0及以上系统运行时报错的解决方案

mac2022-06-30  27

背景:在使用Samsung S系列手机进行自动化测试时,发现同样脚本的情况下华为荣耀系列可以正常运行,最终发现差异在于Android7.0及以上系统和appium版本不匹配,需要升级appium。但需要升级的文件较多,在win环境下不方便执行,所以需要找到一个能够快速解决的方案。

要解决的问题:appium在androidV7.0系统上运行时报错 Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]

原因分析:appium的uiautomator版本与androidV7.0不适配。

前后使用了如下方法,最终问题解决。

方案一:

             1、  找到appium的安装目录下的adb.js文件,目录为:Appium\node_modules\appium\node_modules\appium-adb\lib

             2、  打开adb.js,找到如下代码:

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()

              3、  重启appium

而我用这种方法并没有解决问题,就使用了第二种方法。

方案二:

               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两个应用

解决步骤:1.修改源码文件-注释安装appiumSettings和unlock的两行代码

                   文件路径: X:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android\android.js

             2.修改源码文件如下:

             路径: \Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js

             在adb.js的1035行找到

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, []); }

 

             并增加上面用到的shell_grep函数:

ADB.prototype.shell_grep = function (cmd, grep, cb) { if (cmd.indexOf('"') === -1) { cmd = '"' + cmd + '"'; } var execCmd = 'shell ' + cmd + '| grep ' + grep; this.exec(execCmd, cb); };

 

          3.重启appium

         最终问题解决,在这里做一个记录,方便大家遇到同样问题时处理。

转载于:https://www.cnblogs.com/pengfy/p/10653104.html

相关资源:自定义脚本抖音群控系统(5).pdf
最新回复(0)