Appium Parallel test runs on both platform

I have appium 2.0 java project thats works in one thread and everyfing is ok, but when I start to use a few devices all set ups is ok, but tests isnt started, only app started, for test using PageFactory. I set ports and udids in my code with methods

`public class Base extends SetDrivers {

public void setDriverToApp() throws IOException {
    List<Devices> devices = getAllConnectedDevices();

    for (Devices device : devices) {
        if (device.getPlatformName().equalsIgnoreCase("ios")) {
        } else if (device.getPlatformName().equalsIgnoreCase("android")) {
        } else {
            System.out.println("No devices connected");


I iterate all connected devices and send it udid to its driver

`    public static AppiumDriver getDriver() {
    return driver.get();

public static void setDriver(AppiumDriver driver) {

public static ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();`

`  public static int getFreePort() throws IOException {
    ServerSocket socket = new ServerSocket(0);
    int port = socket.getLocalPort();
    return port;

public static void setIOSDriver(Devices device) throws IOException {
    var xcuiTestOptions = new XCUITestOptions();
    xcuiTestOptions.setApp(System.getProperty("user.dir") + "app.ipa");
    xcuiTestOptions.setCapability("appium:xcodeOrgId", "xxxxx");
    xcuiTestOptions.setCapability("appium:xcodeSigningId", "xxxx");

    setDriver(new IOSDriver(service.getUrl(), xcuiTestOptions));

public static void setAndroidDriver(Devices device) throws IOException {
    var uiAutomator2Options = new UiAutomator2Options();
    uiAutomator2Options.setApp(System.getProperty("user.dir") + "//app.apk");
    uiAutomator2Options.setCapability("skipChromeDownload", false);
    uiAutomator2Options.setCapability("appium:testTagsAsResourceId", true);
    uiAutomator2Options.setCapability("appium:disableIdLocatorAutocompletion", true);
    uiAutomator2Options.setCapability("appium:enableMultiWindows", true);
    uiAutomator2Options.setCapability("appium:allowInvisibleElements", true);
    setDriver(new AndroidDriver(service.getUrl(), uiAutomator2Options));

testng xml sample is:

<test name="Device 1" >
        <class name="tests.testpackage.Test"/>
<test name="Device 2" >
        <class name="tests.testpackage.Test"/>
<test name="Device 3" >
        <class name="tests.testpackage.Test"/>

Get error org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up

I think you need to set certain other ports. Here’s some reading material:

1 Like

Thanks for your answer, I read the approach2, and appium can manage a few device with 4723 port, that in my code.
You say that i i must set port from 8100 -8199?
But I dont know how to set it dynamically, i dont want to harcode ports, cause it need additional classes to every device. Or it`s mandatory for that case?

appreciate your help in that question

I don’t like to act like I’m an authority on this, but I do try to help.

For the ports, it’s not clear from the getFreePort() method what port you will get. Maybe you could add a parameter to that method that restricts it to getting a free port within a certain range. It’s also important that the Appium server knows what ports you are using, which is something I don’t see in your code, but maybe just didn’t include that.

My best advice is to play around with the code provided in those links and find out what suits your needs and coding style. There is some really good advice on this subject in those links, but more is just a web search away if these don’t work for you.

1 Like

This task requires more research. Thanks for advices, i will try to use them.
If you will have more information, please reply to this thread.

this url must be unique per device. you did not add this function code to check.

Aleksei, thanks for answer. For start Appium server i use this function
public class AppiumManage {

static AppiumDriverLocalService service;

public static AppiumDriverLocalService runAppium() {

        service = new AppiumServiceBuilder()
        System.out.printf("Appium server started on url: '%s'%n",

    return service;

I want to run 1 Appium server, so can I just run this server, and when set driver I must
setDriver(new AndroidDriver(“localhost” + port, uiAutomator2Options));
Where port is the same port like in this setup uiAutomator2Options.setSystemPort(getFreePort());

does not work. you should run same number of appium servers as phones you have. each of appium server should use unique port. If you run all in ONE thread all works - same as you wrote. But in parallel it will not work.

Do I need to set this ports in testng.xml, or can I set port ramdom when appium bilder class and the same port in systemPort capability and URL in my code, for every appium server?

It all up to you. Hardly i can reccomend.