1.找到appium的安装目录下的adb.js文件,目录为:Appium
ode_modulesappium
ode_modulesappium-adblib
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(" "); 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 = /[^ ]+[ ]+([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); }); };
把上面这段代码注释掉,用如下代码代替:其实就是重写了一下“getPIDsByName”这个函数
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 = /[^ ]+[ ]+([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