A new session could not be created. (Original error: Requested a new session but one was in progress)

Hi

I am using grid concept to run testng.xml on two real devices but when i run that scenario it is executing in anyone of the device randomly but i need to run both at a time can some one help me how i can resolve it

org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 188 milliseconds
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'LIN66002470', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Driver info: io.appium.java_client.android.AndroidDriver
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:109)
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:39)
at com.andriod_Scripts.MobileGrid.setupCapabilities(MobileGrid.java:38)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
… Removed 18 stack frames

Script i am using

package com.andriod_Scripts;

import static org.testng.Assert.assertEquals;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SeleniumGrid {
DesiredCapabilities caps=new DesiredCapabilities();

AndroidDriver d;
String url;

@Test
 public void testLogin() throws InterruptedException
 {
    d.get("https://www.facebook.com/?_rdr");
    //Type user name
    d.findElement(By.name("email")).sendKeys("[email protected]");
    //Type pass word
    d.findElement(By.name("pass")).sendKeys("appium");
    //Click Login
    d.findElement(By.name("login")).click();
    assertEquals("Need an account?",d.findElement(By.linkText("Need an account?")).getText());
    Thread.sleep(6000);
 }

@Parameters("device_name")
@BeforeMethod
public void setUp(String device_name) throws Exception{
    
    if (device_name.equalsIgnoreCase("ZX1D62P7C5")) {
        
        setcapabilities(device_name);
    }
    if(device_name.equalsIgnoreCase("ZX1B3265HN")){
        setcapabilities(device_name);
    }
    
    
    
    d = new AndroidDriver (new URL("http://127.0.0.1:4444/wd/hub"), caps);
    d.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
 

}

    public void setcapabilities(String device) throws Exception{

        System.out.println("in setcapabilities");
        
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, device);
        caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
        
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
        //caps.setCapability(MobileCapabilityType.APP_ACTIVITY, appActivity);
        //caps.setCapability(MobileCapabilityType.APP_PACKAGE, appPackage);
        caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 130);
        caps.setCapability("udid", device);
        
    }

    @AfterClass
    public void tearDown() throws Exception
     {
         
        d.quit();
         
     }

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<test name="moto g" parallel="classes" thread-count="2" >
    <parameter name="device_name" value="ZX1D62P7C5"/>
        <classes>
            <class name="com.andriod_Scripts.MobileGrid" />
        </classes>
</test> 


<test name="moto e" parallel="classes" thread-count="2" >
    <parameter name="device_name" value="ZX1B3265HN"/>
        <classes>
            <class name="com.andriod_Scripts.MobileGrid" />
        </classes>
</test> 

You can only have one session at a time on one server. We are planning to improve hat, but it is not always possible.

You will have to start two different Appium servers(using different ports) and assign devices to that server.

Refer below link for more detail:
http://appium.io/slate/en/master/?java#parallel-android-tests

Hi Mitesh and sebv

thanks for reply

@mithesh

–port 4728

–port 4729

tried with above ports still same error running same @test commands on two devices

hi Mitesh,

I am able to run two differenct scenarios but when i am running same scenario on two devices i am facing this issue. using TestNg framework on this. like when i am running same test then i am getting this issue.

Can you please share the detailed error & script which you are using to achieve this?