Has anyone successfully run test scripts parallel in two or more Android devices?

Hi @gualber98,

This is the command that is used to launch the appium server and register it with selenium grid.

C:\Users\mkarthik\Documents\selenium\Appium\node.exe C:\Users\mkarthik\Documents\selenium\Appium\node_modules\appium\bin\appium.js --address 127.0.0.1 --port 4728 --udid 085d3b920c928938 --bootstrap-port 4724 --nodeconfig C:\Users\mkarthik\workspace\Android\AppiumMobileAutomation\src\com\nodeconfig\085d3b920c928938.json

i am not specifying any chromedriver -port number

regards,
M.Karthik

I was saying that the problem in your case could be because you are not setting up a different --chromedriver-port

oh ok, Thanks @gualber98 i will try to execute scripts with different chromedriver port…

thanks for your suggestion.

Regards,
M.Karthik

@gualber98,

I tried with specifying --chromedriver-port 9516 and 9515 for two different appium instances.

Execution is carried out on a single device. Not sure why commands are not re-directed the second device.

Thanks and regards,
M.Karthik

@karthik_holla

“Execution is carried out on a single device.”
That is exactly what is happening with my execution, any updates on your attempts?

Try the example from the thread:

Thank you, I will try it out. One question are your running two appium instances?

As mush as you need - android devices connected to PC:-)

Can open multiple driver instance with some delay for parallel execution. If all parallel instance are opening simultaneously I am getting ECONNRESET error. So passed one parameter from TestNG xml to wait for 5-10 seconds. It is working file for me :smile:

1 Like

@Fayizkc Can you please provide the testng xml . Need to know how to configure the delay b/w two tests.

Hi Siva.
Delay is not in the TestNG file. You can implement a logic to stop creating appium capabilities parallel. You can create separate class or method to create WebDriver and make sure you are not hitting that method at the same time.

public static boolean driverCreating;  
    WebDriver createApppiumDriver()
    {
    while (driverCreating){}
    driverCreating = true;
     driver = methodForAppiumDriver()
    driverCreating = false;
    }

You can take a look at this

@saikrishna321 I used the above code. But still appium crashes in between.

When u say Appium … Does not give any hint … Can u pls provide the Appium logs when your tests Re triggered in parallel

It was due to some other error, now things seems to work fine. Thanks!!!. I used the framework mentioned by you:

Need a featured if you can build it for cucumber:

  1. Need a before all kind of method

@Dhiren_Mudgil would be really helpfull if u can raise a issue on the repo https://github.com/saikrishna321/AppiumTestDistribution and provide more details. will surely look into this

YeahI have tried and its working fine for me.
can you share your code, so that I can guide you.

Thanks
Ravi

@ravikr42
Need help on Selenium Grid: I am trying executes script on different mobile emulators but i can’t succeed . I have tried different tutorials but unable to succeed.

Tried but doesn’t work. Other script are runnung fine. I have use following procedure
1)Started Hub
2)Started Node
3)try to execute script

Let me know if i am missing anything

I am trying demo example:
Code 1:- CalcExample .java

package example;
import java.io.File;
import libs.BaseTest;
import org.openqa.selenium.By;
public class CalcExample extends BaseTest{

public CalcExample(){
}

public CalcExample(int deviceNum) {
    super(deviceNum);
}

public void performOperations() {
    
    try
    {
        driver.findElement(By.id("com.android2.calculator3:id/cling_dismiss")).click();
        driver.findElement(By.id("com.android2.calculator3:id/digit5")).click();
        driver.findElement(By.id("com.android2.calculator3:id/plus")).click();
        driver.findElement(By.id("com.android2.calculator3:id/digit9")).click();
        driver.findElement(By.id("com.android2.calculator3:id/equal")).click();
        String num = driver.findElement(By.xpath("//android.widget.EditText[@index=0]")).getText();
        System.out.println("Result : "+num);
        driver.closeApp();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public void run(){
    File app = new File("C:\\AndroidCalculator.apk");
    String appPath = app.getAbsolutePath();
    createDriver(appPath); // create devices
    performOperations(); // user function
}
public static void main(String[] args) {
    // Create object
    CalcExample calc = new CalcExample();
    calc.execute();
}

}

Code 2:-

package libs;
import libs.DeviceConfiguration;
import io.appium.java_client.android.AndroidDriver;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class BaseTest implements Runnable{
public AndroidDriver driver;
protected BaseTest deviceThreads;
protected int numOfDevices;
protected String deviceId;
protected String deviceName;
protected String osVersion;
protected String port;
protected Thread t;
protected int deviceCount;

AppiumManager appiumMan = new AppiumManager();
static Map<String, String> devices = new HashMap<String, String>();
static DeviceConfiguration deviceConf = new DeviceConfiguration();
public BaseTest(){
    try {
        devices = deviceConf.getDivces();
        deviceCount = devices.size()/3;
    }catch (Exception e) {
        e.printStackTrace();
    }
}

public BaseTest(int i){
    int deviceNumber = (i+1);
    this.deviceId = devices.get("deviceID"+deviceNumber);
    this.deviceName = devices.get("deviceName"+deviceNumber);
    this.osVersion = devices.get("osVersion"+deviceNumber);
}

public void createDriver(){
    try    {
        port = appiumMan.startAppium();             // Start appium server              
          
        // create appium driver instance
        DesiredCapabilities capabilities = DesiredCapabilities.android();
        capabilities.setCapability("deviceName", deviceName);
        capabilities.setCapability("platformName", "android");
        capabilities.setCapability(CapabilityType.VERSION, osVersion);
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
        capabilities.setCapability("udid", deviceId);
            
        this.driver = new AndroidDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"),capabilities);
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

public void createDriver(String appPath){
    try    {
        port = appiumMan.startAppium();             // Start appium server              
          
        // create appium driver instance
        DesiredCapabilities capabilities = DesiredCapabilities.android();
        capabilities.setCapability("deviceName", deviceName);
        capabilities.setCapability("platformName", "android");
        capabilities.setCapability(CapabilityType.VERSION, osVersion);
        capabilities.setCapability("app", appPath);
        capabilities.setCapability("udid", deviceId);
            
        this.driver = new AndroidDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"),capabilities);
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

public void destroyDriver()
{
    driver.quit();
    try {
        deviceConf.stopADB();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void start(){
    if (t == null){
      t = new Thread(this);
      t.start ();
    }
}
public void run(){
}

public  <c> void execute()
{
    Class<?> c;
    try {
        int startMethod = 0;
        String className = this.getClass().toString();
        System.out.println("class : "+className);
        className = className.replace("class ", "");
        System.out.println("class : "+className);
        // Get extended class name
        c = Class.forName(className);
        System.out.println("class : "+c);
        
        // Get start method
        Method[] m = c.getMethods();
        System.out.println("methods: "+m.length);
        for(int i=0;i<m.length;i++)    {
            //System.out.println("methods: "+m[i]);
            if(m[i].toString().contains("start")){
                startMethod=i;
                break;
            }
        }
        System.out.println("methods: "+m[startMethod]);
        // get constructor
        Constructor<?> cons = c.getConstructor(Integer.TYPE);
        System.out.println("cons: "+cons);
        
        System.out.println("deviceCount: "+deviceCount);
        // Create array of objects
        Object obj =  Array.newInstance(c, deviceCount);
        for (int i = 0; i < deviceCount; i++) {
            Object val = cons.newInstance(i);
            Array.set(obj, i, val);
        }
        for (int i = 0; i < deviceCount; i++) {
            Object val = Array.get(obj, i);
            m[startMethod].invoke(val);
        }
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}

}

Hi,

I was trying out parallel or sequential execution on multiple android devices, but my scripts always ran on only one device. I finally figured out that I needed to provide unique values for - Appium Port, Bootstrap Port and Device ID (UDID) to make it work.

I have written a post about this, which you can check out below. It uses Java Thread and Runnable interface to run the test in parallel in 2 devices. The logic for parallel execution can be anything, but as mentioned above the important thing is that we pass different values for Appium Port, Bootstrap Port, Device ID (not Device Name) and Chrome Driver Port (if using Chrome Browser)

1 Like

Hi @vaibhav_khachane

Code and steps looks good. are you giving unique address and port details in node configuration file.? how are you executing scripts ? Use testNG parallel execution.

Kindly check or share the node configuration details.

Thanks
Ravi