Appium+Python移动自动化【2】:"ps 'uiautomator'"

Pycharm报错信息:

selenium.common.exceptions.SessionNotCreatedException: Message: A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c “E:\android-sdk-windows\platform-tools\adb.exe -s 02743107C5002344 shell “ps ‘uiautomator’””)

在这里插入图片描述

Appium报错信息:

error: Failed to start an Appium session, err was: Error: Command failed: C:\Windows\system32\cmd.exe /s /c “E:\android-sdk-windows\platform-tools\adb.exe -s 02743107C5002344 shell “ps ‘uiautomator’””

info: [debug] Error: Command failed: C:\Windows\system32\cmd.exe /s /c “E:\android-sdk-windows\platform-tools\adb.exe -s 02743107C5002344 shell “ps ‘uiautomator’””

在这里插入图片描述
在cmd中执行错误信息中的命令:
报的错误信息是:bad pid ‘uiautomator’
在这里插入图片描述

原因:

对应执行的指令是ps ‘uiautomator’,Android7不支持这个指令格式,所以执行结果是bad pid ‘uiautomator’;
目前Appium未对此进行处理,所以需要修改此指令的执行方式。

解决方法:

1、从目录Appium的安装目录\Appium\node_modules\appium\node_modules\appium-adb\lib找到文件adb.js。
2、修改代码:

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

效果如图:
在这里插入图片描述
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);
  });
};

将上方代码段注释掉,新增下方的代码段:

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

4、保存并重启启动Appium,再次运行脚本,运行成功。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值