HomeAppium

Error: Command failed: *adb.exe -s emulator-5554 shell “ps ‘uiautomator'””

Like Tweet Pin it Share Share Email

Especially when working with Appium (1.4.16), Android Studio (2.2.3 and above) and with Android N (7.0) version, execution of test automation script encounters the below error:

Error: Command failed: C:\Windows\system32\cmd.exe /s /c “C:\Users\{user}\AppData\Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell “ps ‘uiautomator'””

Error you see in Appium server:

info: [debug] Error: Command failed: C:\Windows\system32\cmd.exe /s /c 
"C:\Users\132266\AppData\Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell 
"ps 'uiautomator'""
> 
>     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 \"C:\\Users\\132266\\AppData\\Local\\Android\\sdk\\platform-tools\\adb.exe -s 
emulator-5554 shell \"ps 'uiautomator'\"\"\n)","killed":false,"code":1,"signal":null,"cmd":
"C:\\Windows\\system32\\cmd.exe /s /c \"C:\\Users\\{user}\\AppData\\Local\\Android\\sdk\\
platform-tools\\adb.exe -s emulator-5554 shell \"ps 'uiautomator'\"\"","origValue":
"Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"C:\\Users\\{user}\\AppData\\Local
\\Android\\sdk\\platform-tools\\adb.exe -s emulator-5554 shell \"ps 'uiautomator'\"\"\n"},
"sessionId":null}
> info:

Error in Android studio run console:

org.openqa.selenium.SessionNotCreatedException: A new session could not be created. 
(Original error: Command failed: C:\Windows\system32\cmd.exe /s /c "C:\Users\{user}\AppData\
Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell "ps 'uiautomator'""
) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

Solution for this error is to append and replace the code in adb.js file present at location

C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\lib

Code changes to be done goes as below:

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

Performing above changes you are good to go ahead, for more detail you can refer to the actual discussion at appium support website

Comments (12)

Leave a Reply to Srinivas P Cancel reply

Your email address will not be published. Required fields are marked *