Appium Server + multiple connections + without selenium grid


I am starting appium server each device it detects it creates a random --port and -bp and -U of each device.

The appium server starts all connections with different port for each devices.

I then establish the following connection for each device depending on the platform type:
wd is a static WebDriver

wd = new AndroidDriver<>(new URL(“”+devices.getPortNo()+"//wd/hub"),capabilities)
wd = new iOSDriver<>(new URL(“”+devices.getPortNo()+"//wd/hub"),capabilities)

Below is the Appium Server starting foe each device:

2016-04-29 06:59:50:169 - info: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)
2016-04-29 06:59:50:169 - info: Welcome to Appium v1.4.13 (REV c75d8adcb66a75818a542fe1891a34260c21f76a)
2016-04-29 06:59:50:174 - info: Appium REST http interface listener started on
2016-04-29 06:59:50:174 - info: Appium REST http interface listener started on
2016-04-29 06:59:50:178 - info: [debug] Non-default server args: {“udid”:“”,“address”:“”,“port”:4513,“bootstrapPort”:4500,“sessionOverride”:true,“fullReset”:true,“log”:"/Users/admin/Documents/log/log.txt"}
2016-04-29 06:59:50:178 - info: [debug] Non-default server args: {“udid”:“”,“address”:“”,“port”:4567,“bootstrapPort”:4543,“sessionOverride”:true,“fullReset”:true,“log”:"/Users/admin/Documents/log/log.txt"}

However the test only executes on one device, I am not sure what have done wrong.

I don’t want to use selenium grid as a solution.

1 Like

I had such problems when I used singleton pattern.
Is ‘wd’ or any your object final / static ?

Hi Degard

public static WebDriver wd; is a global scope declaration

I create a thread for each server with its own mobile device

I followed the server arguments as stated in the previous log


It is reason because second thread rewrite your driver object.
Do not use global declarations with threads.

Hi Degard

Each thread has its own mobile declarations.

Each mobile devices connected the installation of the app happens on each of them and the application gets laughed.


Can you send logs from both servers?

Hi Degard

Attached is the files (20.4 KB) (236.7 KB)

Otherwise a single device works regardless.

I wanted to get parallel execution to work. Assistance would be appreciated without selenium grid

First, is your test code actually running in parallel? If it is, then there’s this bit @Degard pointed out :

That’s a problem. You have a shared variable that is accessed by two different threads. That’s a race condition. Each thread may have its own driver initialization, but the last thread that writes a reference to your static variable will overwrite the references the other threads wrote into wd. This means that all of your drivers and Appium sessions that you initialized will be lost and unrecoverable. However, your tests do not detect this. All your tests’ WebDriver commands will be directed to only 1 Appium session on only 1 server. This is indicated by how different your Appium logs are. One Appium log is very short: it’s mostly just initializing the test session and then shutting down due to lack of commands coming in. The other Appium log is quite extensive, indicating that the thread corresponding to your first log has lost its reference to its driver.

wd should not be static, and each thread should have its own WebDriver instance. I don’t know what your project structure looks like, but I have a WebDriver member variable as part of each TestNG test class I have, and each test class is run by only 1 unique thread each time my tests run. This ensures that all the test methods in my test class will use the same WebDriver instance for the same thread.

Multithreaded programming is full of subtle errors. After you solve the above problem, you’ll have to concern yourself with variables being cached for each thread and read-write reordering. If you really want to do this without the help of Selenium Grid managing your test nodes safely in a multithreaded environment, I recommend carefully studying and reviewing what Java’s static variables are and the Java Memory Model. You then want to build a framework that can safely give each of your test threads a driver.


Hi guys,

Here is my setup for parallel execution… hope it will help :slight_smile: happy testing…

Thanks I believe you are correct I will need to create object based classes to resolve the issue.

Thanks for your advice.


Will definitely watch your tutorial

Hi Afwang

I have written everything to be object based, I even pass the WebDriver to each object still only the one WebDriver Session completes

Post code and logs, if possible. I would also like to see how you’re spawning the separate threads for each test. If you’re using TestNG, it would be nice to see your TestNG XML file, or how you’re setting up TestNG if you’re doing it programmatically.

Hey @afwang, I am facing similar issue as @lance
I am spawning threads using this in TestNG:
<suite name=“E2E Scenarios” thread-count=“2” parallel=“tests”>

Is there any other way of doing the parallel ecexution?

That’s essentially what I do to spawn threads for parallel execution. The main difference is that I like to specify the thread-count and parallel attributes specifically on the <test …> tags. This allows me to group my test classes into two tests: a test whose classes are not thread-safe, and a test whose classes can have their methods run in parallel safely.

How to run script on multiple android device in Cucumber BDD framework . because @before hook not allowing me to pass parameters like TDD framework.