0
votes

i am getting below error while i am running parallel test scripts in appium (Android)with Selenium grid.

Default suite Total tests run: 6, Failures: 0, Skips: 3 Configuration Failures: 1, Skips: 0

Stack trace message: org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Requested a new session but one was in progress) My TestNG xml file:

<suite name="Default suite" thread-count="4" parallel="tests">
<test name="Nexus">
<Parameters>
<parameter name="platform" value="Nexus"/>
<parameter name="browsername" value="Android"/>
<parameter name="remoteurl" value="http://0.0.0.0:4723/wd/hub"/>
</Parameters>
<classes>
<class name="AppiumTest">
<methods>
<include name="Test1"/>
<include name="Test2"/>
<include name="Test3"/>
</methods>
</class>
</classes>
</test>
<test name="Moto E">
<Parameters>
<parameter name="platform" value="Moto E"/>
<parameter name="browsername" value="Android"/>
<parameter name="remoteurl" value="http://0.0.0.0:4726/wd/hub"/>
</Parameters>
<classes>
<class name="AppiumTest">
<methods>
<include name="Test1"/>
<include name="Test2"/>
<include name="Test3"/>
</methods>
</class>
</classes></suite>

Config.Json file:

{
"capabilities":
[
{
"browserName":"Nexus",
"version":"5.1.1",
"maxInstances": 1,
"platform":"ANDROID"
}
],
"configuration":
{
"cleanUpCycle":2000,
"timeout":30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url":"http://0.0.0.0:4723/wd/hub",
"host":"192.168.50.114",
"port": 4723,
"maxSession": 1,
"register": true,
"registerCycle": 5000,
"hubPort": 4448,
"hubHost": "192.168.50.114"
}
}

Command for selenium node: appium --nodeconfig path/to/json file.

Test script:

public class AppiumTest { //static RemoteWebDriver driver;

@SuppressWarnings("rawtypes")
public static AndroidDriver driver;


@SuppressWarnings("rawtypes")
@org.testng.annotations.BeforeTest

public static void setUp() throws MalformedURLException{

    DesiredCapabilities capabilities=new DesiredCapabilities();
    capabilities.setCapability("deviceName","Nexus");
    capabilities.setCapability("platformName","Android");

    capabilities.setCapability("appPackage","club.apptu.basic.callrecorder");
    capabilities.setCapability("appActivity","MainActivity");
    driver=new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"),capabilities);

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

}

@org.testng.annotations.Test
public void Test1(){
  turnonoff();
  turnonoff();

  //longclick();

 // important();
 // delete();
//  deleterecord();
//  scroll();
//  languagechange();

}
@org.testng.annotations.Test
public void Test2(){

      drawer();
      drawer();

}


@org.testng.annotations.Test
public void Test3(){
  clickonitem();
  play();
  pause();

}

Any suggestions would be appreciated.

2
What's wrong with my question ?Dev-Tester
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."Renzo

2 Answers

0
votes

I have been working with Selenium Grid and Appium and have encountered the same error. It crops up when the devices lose connection, if you are using real devices try disconnecting and reconnecting the USB and restart the nodes.

You can run

adb devices 

and if they are listed you shouldn't get that error.

For any further help please post test scripts and JSON configuration files for grid.

Hope this helps.

3
votes

So the setup I am using for the same purpose which works is:

  • TestNG.xml file for 2 devices:

    <?xml version="1.0" encoding="UTF-8"?>
        <suite name="Suite" parallel="tests" thread-count="2">
            <test name="Nexus 7">
                <parameter name="udid"  value="XXXX" />
                <classes>
                    <class name="testNG.TestOne"/>
                </classes>
            </test> <!-- Test -->
            <test name="HTC">
                <parameter name="udid"  value="XXXX" />
                <classes>
                    <class name="testNG.TestOne"/>
                </classes>
            </test> <!-- Test -->
       </suite> <!-- Suite -->
    

Here I am passing in the UDID of each device, its not necessary to pass in anything else, you are passing in the URL which I believe is a mistake, as you should pass all AndroidDrivers to the Selenium Grid Hub URL and using the capabilities the Hub should allocate the test to the correct device.

  • DriverSetup class with the following method:

    public static AndroidDriver getDriver(String udid) throws MalformedURLException{
    
    String SELENIUM_HUB_URL = "XXXX";
    ThreadLocal<AndroidDriver> driver = null;  
    
    DesiredCapabilities capabilities = new DesiredCapabilities();       
    capabilities.setCapability("device", udid);
    capabilities.setCapability("deviceName", udid);
    capabilities.setCapability("udid", udid);
    capabilities.setPlatform(Platform.ANDROID);
    capabilities.setCapability("browserName", "Chrome");    
    
    try {
        driver = new ThreadLocal<AndroidDriver>();
        driver.set(new AndroidDriver(new URL(SELENIUM_HUB_URL),
                capabilities));
    } catch (MalformedURLException e) {
        System.out.println("Tackle Issue with RemoteDriverSetup");
    }
    driver.get().manage().timeouts()
            .pageLoadTimeout(20L, TimeUnit.SECONDS);
    driver.get().manage().timeouts()
            .implicitlyWait(20L, TimeUnit.SECONDS);
    
    return driver.get();
    }
    

You will need to change the capabilities to suit your project, however I would recommend using UDID as a capability as it mean the test has to run on a certain device.

The UDID for a device is found by typing adb devices into the console.

You need to use the Selenium Grid Hub URL when initialising your AndroidDriver, if you bypass the Hub and send the tests straight to the nodes you lose the Grid's functionality to run tests in parallel.

  • A BeforeClass method in my TestOne class:

    @Parameters({"udid"})
    @BeforeClass
    
    public void beforeClass(String udid) throws UnsupportedEncodingException, MalformedURLException, InterruptedException{
    System.out.println("Run started for " + udid);
    driver = DriverSetup.getDriver(udid);
    }
    
  • JSON config files

    {
    "capabilities":
    [
    {
    "browserName": "Chrome",
    "deviceName": "XXXX",
    "device": "XXXX",
    "udid":"XXXX",
    "version":"4.2",
    "maxInstances": 5,
    "platform":"ANDROID",
    "platformName": "Android"
    }
    ],
    "configuration":
    {
    "cleanUpCycle":2000,
    "timeout":10000,
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", 
    "url":"http://0.0.0.0:4723/wd/hub",
    "maxSession": 5,
    "port": 4723,
    "host": "0.0.0.0",
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": "localhost"
    }
    }
    

I Hope this help you resolve your issue.