Appium Appium 使用 appium 在 android7.0 真机上测试程序时,启动报错,该怎么解决

小胡 · 2017年03月08日 · 最后由 hansen 回复于 2018年06月07日 · 3010 次阅读

error: Failed to start an Appium session, err was: Error: Could not extract PIDs from ps output. PIDS: [], Procs: ["bad pid 'uiautomator'"]
未能启动 Appium 会话,无法提取 PS 输出 PID

共收到 12 条回复 时间 点赞

Appium 版本不支持,需要 UIAutomator2.0

appium 版本 1.4.16,android7.0,使用的手机是华为 Mate9,执行时,报错

Starting Node Server
info: Welcome to Appium v1.3.4 (REV c8c79a85fbd6870cd6fc3d66d038a115ebe22efe)
info: Appium REST http interface listener started on 127.0.0.1:4723
info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"platformName":"Android","platformVersion":"18","automationName":"Appium"}
info: Console LogLevel: debug
info: --> POST /wd/hub/session {"desiredCapabilities":{"appPackage":"com.huawei.ichannel","appWaitPackage":"com.huawei.ichannel","appWaitActivity":"com.huawei.ichannel.StartActivity","fullReset":"false","deviceName":"","deviceReadyTimeout":"3600","appActivity":"com.huawei.ichannel.StartActivity","newCommandTimeout":"3600","platformVersion":"7.0","automationName":"Appium","locationContextEnabled":true,"webStorageEnabled":true,"browserName":"","javascriptEnabled":true,"unicodeKeyboard":"True","platformName":"Android","resetKeyboard":"True"}}
info: Client User-Agent string: Apache-HttpClient/4.3.4 (java 1.5)
warn: Converting cap fullReset from string to boolean. This might cause unexpected behavior.
warn: Converting cap unicodeKeyboard from string to boolean. This might cause unexpected behavior.
warn: Converting cap resetKeyboard from string to boolean. This might cause unexpected behavior.
info: [debug] The following desired capabilities were provided, but not recognized by appium. They will be passed on to any other services running on this server. : locationContextEnabled, webStorageEnabled, javascriptEnabled
info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device
info: [debug] Creating new appium session 31f544b9-1ee0-463e-aa3c-698831acda29
info: Starting android appium
info: [debug] Getting Java version
info: Java version is: 1.8.0_60
info: [debug] Using fast reset? true
info: [debug] Preparing device for session
info: [debug] Not checking whether app is present since we are assuming it's already on the device
info: [debug] Checking whether adb is present
info: [debug] Using adb from D:\Tools\android-sdk\platform-tools\adb.exe
info: Retrieving device
info: [debug] Trying to find a connected android device
info: [debug] Getting connected devices...
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe devices
info: [debug] 1 device(s) connected
info: Found device GWY0216C07005073
info: [debug] Setting device id to GWY0216C07005073
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 3600)
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 wait-for-device
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "echo 'ready'"
info: [debug] Starting logcat capture
warn: No app capability, can't parse package/activity
info: [debug] Getting device API level
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 24
info: Device API level is: 24
info: [debug] Apk doesn't exist locally
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "rm -rf /data/local/tmp/strings.json"
info: [debug] Not uninstalling app since server not started with --full-reset
info: [debug] Skipping install since we launched with a package instead of an app path
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 forward tcp:4724 tcp:4724
info: [debug] Pushing appium bootstrap to device...
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 push "D:\Tools\Appium\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
info: [debug] Enabling Unicode keyboard support
info: [debug] Pushing unicode ime to device...
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 install "D:\Tools\Appium\node_modules\appium\build\unicode_ime_apk\UnicodeIME-debug.apk"
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "settings get secure default_input_method"
info: [debug] Unsetting IME 'com.nuance.swype.emui/com.nuance.swype.input.HuaweiIME'
info: [debug] Setting IME to 'io.appium.android.ime/.UnicodeIME'
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "ime enable io.appium.android.ime/.UnicodeIME"
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "ime set io.appium.android.ime/.UnicodeIME"
info: [debug] Pushing settings apk to device...
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 install "D:\Tools\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
info: [debug] Pushing unlock helper app to device...
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 install "D:\Tools\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "ps 'uiautomator'"
info: [debug] Resetting IME to 'com.nuance.swype.emui/com.nuance.swype.input.HuaweiIME'
info: [debug] executing cmd: D:\Tools\android-sdk\platform-tools\adb.exe -s GWY0216C07005073 shell "ime set com.nuance.swype.emui/com.nuance.swype.input.HuaweiIME"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
warn: UiAutomator did not shut down fast enough, calling it gone
error: Failed to start an Appium session, err was: Error: Command failed:
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] Error: Command failed:
at ChildProcess.exithandler (child_process.js:637:15)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:743:16)
at Socket. (child_process.js:956:11)
at Socket.EventEmitter.emit (events.js:95:17)
at Pipe.close (net.js:465:12)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Command failed: )","killed":false,"code":1,"signal":null,"origValue":"Command failed: "},"sessionId":null}
info: <-- POST /wd/hub/session 500 11130.372 ms - 192

有知道是什么原因吗?

按照这个方法解决就行,

解决 Android 7.0 系统的手机无法执行 appium 脚本的问题,改问题的具体解决方法如下:
1、 找到 appium 的安装目录下的 adb.js 文件,windows 版本的目录如下:Appium\node_modules\appium\node_modules\appium-adb\lib
2、 打开 adb.js,手动修改该文件下的内容:
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(); //在该处添加此行代码
3、重启 appium

lyf 回复

楼上方法太给力了,怎么想到这方法的啊,原理是什么

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

这是哪里出错了,看不懂

info: [debug] App is installed
info: App is already installed, resetting app
info: [debug] Running fast reset (stop and clear)
info: [debug] executing cmd: F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 shell "am force-stop com.appium.xixi.testapp"
info: [debug] executing cmd: F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 shell "pm clear com.appium.xixi.testapp"
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 forward tcp:4724 tcp:4724
info: [debug] Pushing appium bootstrap to device...
info: [debug] executing cmd: F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 push "D:\Program Files (x86)\Appium\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
info: [debug] Pushing settings apk to device...
info: [debug] executing cmd: F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install "D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
warn: UiAutomator did not shut down fast enough, calling it gone
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
error: Failed to start an Appium session, err was: Error: Command failed: C:\Windows\system32\cmd.exe /s /c "F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install "D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk""
Failed to install D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]

info: [debug] Error: Command failed: C:\Windows\system32\cmd.exe /s /c "F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install "D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk""
Failed to install D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]

at ChildProcess.exithandler (child_process.js:751:12)
at ChildProcess.emit (events.js:110:17)
at maybeClose (child_process.js:1016:16)
at Process.ChildProcess._handle.onexit (child_process.js:1088:5)
info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c \"F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install \"D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk\"\"\nFailed to install D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]\r\n)","killed":false,"code":1,"signal":null,"cmd":"C:\Windows\system32\cmd.exe /s /c \"F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install \"D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk\"\"","origValue":"Command failed: C:\Windows\system32\cmd.exe /s /c \"F:\Android\sdk\platform-tools\adb.exe -s 5LM0216906001288 install \"D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk\"\"\nFailed to install D:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk: Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.appium.settings without first uninstalling.]\r\n"},"sessionId":null}
info: <-- POST /wd/hub/session 500 10884.488 ms - 1306

xiaolichun 回复

软件已经安装了,删了再试试

改了之后还是有这个报错

匿名 #9 · 2017年12月01日

最后怎么弄好的啊?

gundamsing 回复

你是在胡说八道!

lyf 回复

我查看 adb.js 连这个方法都没找到 不晓得是怎么回事呢

一楼方法没解决,我用另一个方法解决了:
Go to adb.js in the path 'node_modules//appium/node_modules/appium-adb/lib/adb.js'
**Find the below*
ADB.prototype.shell = function (cmd, cb) {
if (cmd.indexOf('"') === -1) {
cmd = '"' + cmd + '"';
}
var execCmd = 'shell ' + cmd;
this.exec(execCmd, cb);
};
Append the below
ADB.prototype.shell_grep = function (cmd, grep, cb) {
if (cmd.indexOf('"') === -1) {
cmd = '"' + cmd + '"';
}
var execCmd = 'shell ' + cmd + '| grep ' + grep;
this.exec(execCmd, cb);
};
2 Find the 'ADB.prototype.getPIDsByName ' and replace the whole section

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

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册