Run as junit error(android-appium): A new session could not be created

detail log:
org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c “C:\Users\sxie\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
Build info: version: ‘3.4.0’, revision: ‘unknown’, time: ‘unknown’
System info: host: ‘WL00070299’, ip: ‘10.110.12.39’, os.name: ‘Windows 8.1’, os.arch: ‘x86’, os.version: ‘6.3’, java.version: ‘1.8.0_40’
Driver info: driver.version: AndroidDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at io.appium.java_client.remote.AppiumProtocolHandShake.createSession(AppiumProtocolHandShake.java:161)
at io.appium.java_client.remote.AppiumProtocolHandShake.createSession(AppiumProtocolHandShake.java:76)
at io.appium.java_client.remote.AppiumCommandExecutor.doExecute(AppiumCommandExecutor.java:111)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:162)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:137)
at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:38)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:88)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:112)
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:73)
at com.sky.demo.ContactTest.setUp(ContactTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

code:
public class ContactTest {
private AndroidDriver driver;
@Before
public void setUp() throws Exception {
//设置apk的路径
File classpathRoot = new File(System.getProperty(“user.dir”));
File appDir = new File(classpathRoot, “apps”);
File app = new File(appDir, “ContactManager.apk”);

    //设置自动化相关参数
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("deviceName", "emulator-5554");// Galaxy_Nexus_API_25
    
    capabilities.setCapability("noReset", true);
    
    //设置安卓系统版本
    capabilities.setCapability("platformVersion", "7.1.1");
    //设置apk路径
    capabilities.setCapability("app", app.getAbsolutePath()); 
    
    //设置app的主包名和主类名
    capabilities.setCapability("appPackage", "com.example.android.contactmanager");
    capabilities.setCapability("appActivity", ".ContactManager");
     
    //初始化
    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);       
}

@Test
public void addContact(){
    WebElement el = driver.findElement(By.name("Add Contact"));
    el.click();
    List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
    textFieldsList.get(0).sendKeys("feimaoyuzhubaobao");
    textFieldsList.get(2).sendKeys("forever together");
    driver.swipe(100, 500, 100, 100, 2);
    driver.findElementByName("Save").click();
}    

@After
public void tearDown() throws Exception {
    driver.quit();
}

From the log it looks like it’s unable to start the emulator and hence throwing that session not created. Try below steps:

  1. Start the emulator outside of code
  2. run the same test

I am assuming this should work. Once this is successful, pass the avdReady capability and set it to some higher time.

You can switch to a faster emulator like Genymotion or run the test on actual physical device.

Thanks,
Nishant

Hi Nishant,
This is my first demo for Appium, Thank you very much for your help. But I didn’t understand your solution. “Start the emulator outside of code” what does this mean or how to start the emulator outside the code?
Another information is, I execute the command(C:\Windows\system32\cmd.exe /s /c “C:\Users\sxie\AppData\Local\Android\sdk\platform-tools\adb.exe -s emulator-5554 shell “ps ‘uiautomator’””)in CMD, it show error: bad pid ‘uiautomator’. Not sure if this is the cause.

Now my step is:

  1. start the appium
  2. start the avd in Android studio
  3. run as Junit in Eclipse

resolved by this way:
adb.js(C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js) 中1035 行this.shell(“ps '” + name + “’”, function (err, stdout) {
对应执行的指令是ps ‘uiautomator’, Android7不支持这个指令格式,所以执行结果是bad pid ‘uiautomator’
目前Appium未对此进行处理,所以需要修改此指令的执行方式
即将
this.shell(“ps '” + name + “’”, function (err, stdout) {
if (err) return cb(err);
替换成
this.shell_grep(“ps”, name, function (err, stdout) {

if (err) {

logger.debug(“No matching processes found”);
return cb(null, []);

}

并增加上面用到的shell_grep函数:
ADB.prototype.shell_grep = function (cmd, grep, cb) {
if (cmd.indexOf(’"’) === -1) {
cmd = ‘"’ + cmd + ‘"’;
}
var execCmd = 'shell ’ + cmd + '| grep ’ + grep;
this.exec(execCmd, cb);

};