Appium报错command failed shell "ps '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);
};

在这段代码下面加入这段代码:

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

问题解决。

敲黑板,划重点
原因:
adb.js 中 this.shell(“ps ‘” + name + “’”, function (err, stdout) {
对应执行的指令是ps ‘uiautomator’, Android7不支持这个指令格式,所以执行结果是bad pid ‘uiautomator’
目前Appium未对此进行处理,所以需要修改此指令的执行方式

这只是版本不兼容问题的解决方法之一,或许你用了该方法还不ok,还需要不断去探索~

参考:
https://blog.youkuaiyun.com/qq_15283475/article/details/80001157

### Appium启动会话报错解决方案 #### 一、确认环境配置无误 确保本地开发环境中已正确安装并配置好所有必需组件,包括但不限于Java JDK、Android SDK以及Node.js等。对于Android平台而言,还需特别留意SDK Manager内的Tools与Platform-Tools版本是否最新,因为过期的工具包可能导致兼容性问题从而引发异常状况[^4]。 #### 二、处理会话冲突错误 当尝试创建新的测试会话却收到`Failed to start an Appium session, err was: Error: Requested a new session but one was in progress`这样的反馈时,这通常意味着当前存在未结束的老会话占用资源。此时应先通过命令行执行`adb shell ps | grep node`查找是否有残留进程,并利用`adb kill-server && adb start-server`重启ADB服务来清理可能存在的僵尸节点;另外也可以考虑升级至更高版本的Appium Server以获得更稳定的性能表现[^2]。 #### 三、验证应用Activity路径准确性 如果遇到类似于`activity used to start app doesn’t exist or cannot be launched! Make sure it exists and is a launchable activity`这类提示,则表明指定的应用入口点有误或是该Activity不具备可被外部调起的能力。建议开发者仔细核对manifest文件中的定义,确保所提供的package name及main Activity名称完全匹配实际项目结构[^5]。 #### 四、排查UIAutomator2驱动程序初始化失败的情况 针对`Encountered internal error running command: Error: Appium Settings app is not running after 5000ms`这种情形,除了按照前述方法更新相关依赖外,还应当检查设备端口映射关系是否正常(可通过netstat指令查看),同时适当延长等待超时时间以便给予足够的缓冲空间让设置应用程序完成加载过程[^3]。 ```java // Java客户端代码片段示例:调整默认超时参数 DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("newCommandTimeout", 60); // 设置新命令的最大允许间隔时间为60秒 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值