解决appium 连接真机Android 9启动报错.....shell "ps 'u

时间:2021-12-07 18:05:03

好久没有使用Appium了,感觉要从头开始了,正好换个电脑要重新装环境,装环境就不描述了,照着网上的资料就可以了,问题就出现在连接真机,手机是Android9,vivoZ5x,启动APP时候始终报错,提示....shell "ps ‘uiautomator‘。

网上搜了很多,但是如下的解决版本亲测有效。

在appium的安装目录,找到该目录下C:softAppiumnode_modulesappiumnode_modulesappium-adblib的 adb.js 文件

1.找到这段代码:

ADB.prototype.shell = function (cmd, cb) {
  if (cmd.indexOf(‘"‘) === -1) {
    cmd = ‘"‘ cmd ‘"‘;
  }
  var execCmd = ‘shell ‘ cmd;
  this.exec(execCmd, cb);
};

2.在这段代码后面加上如下:

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.注释如下代码:

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);
  });
};

4.增加下列代码:

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);
  });
};

5.重启APPIUM即可,Android9就可以运行了