解决方法如下:
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);
};
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);
});
};
此时解决了每次启动都重复安装setting和unlock的APK文件,那么如何解决重复安装ime.APK文件呢?按照以下步骤即可:
4.解决每次启动Appium重复安装ime.APK文件的问题:
找到该目录下js文件: Appium\node_modules\appium\lib\devices\android\android-common.js
androidCommon.pushUnicodeIME=function(cb){
cb() (添加这个方法,其他的代码内容直接注释掉)
/* logger.debug("Pushing unicode ime to device...");
var imePath = path.resolve(__dirname, "..", "..", "..", "build", "unicode_ime_apk", "UnicodeIME-debug.apk");
fs.stat(imePath, function (err) {
if (err) {
cb(new Error("Could not find Unicode IME apk; please run " + "'reset.sh --android' to build it."));
} else { this.adb.install(imePath, false, cb);
}
}.bind(this));
*/(注释以上代码)
};
5.重启Appium,这样你就可以随心所欲的进行自动化工具测试啦!