appium报错:A new session could not be created

appium中报错:

selenium.common.exceptions.SessionNotCreatedException: Message: A new session could not be created. (Original error: Command failed: C:\\WINDOWS\\system32\\cmd.exe /s /c "D:\\tools\\Android\\android-sdk\\platform-tools\\adb.exe -s 127.0.0.1:62001 shell "ps 'uiautomator'""

在cmd中执行错误信息中的命令:

报的错误信息是:bad pid ‘uiautomator’

 

 

解决方法:

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

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.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");
    _.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、将上边代码注释掉,用/*开始注释,以*/注释结束

5、替换为下面的代码

//替换代码段
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);
  });
};

6、重启appium

posted @ 2022-01-11 15:15  小卜同学  阅读(517)  评论(0编辑  收藏  举报