Appium Parallel Execution using Java Thread – Android

Appium Parallel Execution using Java Thread – Android

In this article we will explain the process of Appium Parallel Execution using Java Thread and Runnable interface in a detailed step by step manner. You will learn how you can run your Appium test scripts sequentially or in parallel on multiple android devices (or multiple emulators) connected to a single machine using USB ports. Before we start, have a look at the below video where we are opening an app and performing couple of actions on it simultaneously on two different android devices. What you see in the video is what we will try to achieve by the end of this article.

Dear readers, we have more articles on Appium. Have a look and see if you find anything useful. There’s lot more going on at our Facebook page. Follow us there to get the latest updates

In this article, we will provide the detailed steps and the code with which you can achieve parallel or simultaneous execution on multiple android devices.


(adsbygoogle = window.adsbygoogle || []).push({});

Background – Why this article?

I have had good exposure to web based automation with tools such as QTP and Selenium, but my journey with mobile test automation with Appium is fairly new. Although Appium has a very good user support, with a lot of enthusiastic users helping everyone with their queries, but I found that a lot of documentation is very technical. And due to its technical nature it becomes bit hard for a newbie like me to understand and implement these concepts. Hence this article, where I will try to explain the process of Appium parallel execution in a detailed step by step manner.

Prerequisites – Expectations from the Target Audience

Even though this article explains Appium parallel execution from a newbies perspective, but the target audience needs to have a very basic understanding of Appium. Since this article is bit of an advanced topic, or to put it differently, this is not a topic that a person would try to explore during their first few days with Appium learning, so we expect the readers –

  • have a basic understanding of mobile test automation and how Appium fits in
  • have done the necessary Appium setup on their machines (Appium server, ADB etc)
  • are able to create and run some basic test scripts on mobile applications
  • are aware of the basics of Java and Selenium

Appium Parallel Execution – Setup that we have used

The setup that we have used for Appium is as follows –

  • Laptop : Windows 10
  • Appium Server : Version 1.6.5
  • Mobile Devices : Nexus 6P and Samsung Galaxy S7, both running Android 6.0.1
  • Connection Method : Both mobile devices connected to the same laptop via USB ports

Appium Parallel ExecutionAppium Parallel Execution

Appium Setup for Parallel or Sequential Execution

To execute your scripts on multiple devices, you would need to work at two different places – one is at Appium Server and the other one is in the Java code. In this section, we will first see the work we need to do at Appium Server side.

Important Note: There should be one Appium server instance started for each device you want to run your test scripts on. For example, if you want to run your tests in 3 different android devices, you would need to start 3 different instances of Appium Servers. In our case, we would need 2 Appium Servers, as we would be running our script in 2 different devices.


(adsbygoogle = window.adsbygoogle || []).push({});

When you start multiple instances of Appium Servers, you would need to make sure that you provide different values for the following –

  • Appium Port
  • Device ID
  • Appium Bootstrap Port
  • ChromeDriver Port (this has to be used only if you are using webviews or chrome browser
  • Selendriod Port (this needs to be used only if you are using selendroid)

In our example, we are not using webviews, chrome browser or selendroid. Hence, we are only concerned with Appium Port, Device ID and Appium Bootstrap Port

Appium Parallel Execution - Important FlagsAppium Parallel Execution - Important Flags

Let us now start with the setup at the Appium end.

Start 2 instances of Appium Server

Follow the steps given below to start two instances of Appium Server by providing different values for Server Port and Bootstrap port.

1. Open Appium Desktop from Start Programs

Open Appium Desktop - Windows 10Open Appium Desktop - Windows 10

2. Go to Advanced tab and enter the following values in the fields as given below –

  • Server Address – 0.0.0.0
  • Server Port – 4723
  • Bootstrap Port – 100
  • Allow Session Override checkbox – Tick

Appium Parallel Execution - Settings for 1st Server InstanceAppium Parallel Execution - Settings for 1st Server Instance

Please note that you can enter any valid port number (valid port number is any number between 0 and 65535) in Bootstrap Port field. Note down that port numbers that you used here, as these will be needed in the test script later on.

3. Start the Appium server. The logs will show that the server is running

Appium Parallel Execution - First server instance startedAppium Parallel Execution - First server instance started

Now, follow the same steps that we mentioned above to start the second instance of Appium Server. Make sure that you give different values of Server Port and Bootstrap Port for this instance.

4. Open a new instance of Appium Desktop and in the Advanced tab, set the values as mentioned below –

  • Server Address – 0.0.0.0
  • Server Port – 5000
  • Bootstrap Port – 200
  • Allow Session Override checkbox – Tick

Appium Desktop - Second server instance startedAppium Desktop - Second server instance started

5. Start this instance of Appium server as well. You can see from the logs that the server would have started

Appium Desktop Parallel Execution - Second Server Instance StartedAppium Desktop Parallel Execution - Second Server Instance Started

This completes the setup at Appium Server end. Let us now move over to the next section where we will connect both the mobile devices to the machine.

<!– Commented by Anish – 11-Sep-2017. Old steps

Starting 2 instances of Appium server

Follow the steps given below to start 2 Appium server instances by providing different values for Appium server, Device ID and Appium Bootstrap ID.

1. Go to Start > All Programs > Appium and then click on Appium to open the Appium server (if you use command prompt to start Appium, then you can do it that way as well)

Open Appium ServerOpen Appium Server

2. Once Appium is opened, click on the Android Settings icon, so that Android Settings window is displayed

3. At the bottom of Android Settings window, you will see the 3 ports – Bootstrap Port, Selendroid Port and Chrome Driver Port. As mentioned before, we will only use Bootstrap port for this example

4. Select the Bootstrap Port checkbox and then enter any valid port number (valid port number is any number between 0 and 65535). In this example, we will use port number 100, as shown in below image

Bootstrap Port - First Appium ServerBootstrap Port - First Appium Server

5. Now click on the General Settings icon, so that General Settings window is displayed

6. Leave the server address as the default value. The Port value can also be left default or you can provide any valid port number. In this case, we will leave the Port number with the default value as shown the below image

Appium Port - First Appium ServerAppium Port - First Appium Server

Note down this port number, as we will need to use this later in the code.

7. Click on the Play icon to launch the Appium server. Once the server is launched, you will see the window like this

First Appium Server LaunchedFirst Appium Server Launched

We will now follow the exact same steps to launch the second Appium server instance. The only change would be to provide different values for Bootstrap port and Appium port.

8. Open a new instance of Appium server from Start > All Programs > Appium and then clicking on Appium

9. Provide a new value for Bootstrap port (should be different from the Bootstrap port value you provided for the first appium server). In this case, we have set the value as 200

Bootstrap Port - Second Appium ServerBootstrap Port - Second Appium Server

10. Leave the server address as default, but provide a different port number. We have used port number as 5000 in this case. Again, note down this port number as you will have to provide this value in Java code

Appium Port - Second Appium ServerAppium Port - Second Appium Server

11. Start this server as well. Now you will have two different Appium server instances running on your machine as shown below. Server 1 is running on Appium port 4723 and Bootstrap port 100, and server 2 is running on Appium port 5000 and Bootstrap port 200

Appium Server - Two InstancesAppium Server - Two Instances

This completes the setup part that you have to do at the Appium side. Let’s now go to the next section
–>

Connect both the mobile devices to the machine

Connect both your devices to your machine via USB. Use the below steps to make sure that both the devices are properly connected –

1. Open command prompt and set the path of the folder where you have ADB

2. Run the command – adb devices. This command should list down both the devices connected to your machine as shown in the below image. If both the devices are displayed, then you are good to go ahead with the next steps (In this scenario, we have added ADB in PATH variable, hence we are executing the command directly from the default folder. If you have not added it in PATH variable, you need to first go to the folder where you have ADB)

ADB Devices CommandADB Devices Command

Also, note down both the Device IDs from command prompt. This Device ID is actually the UDID, which you would need to provide in the code later.

Desired Capabilities logic for launching app on mobile device

In this section, we will show you the main code that you would need to launch the app on your mobile device. In this example, we are using Amazon shopping app as our test app.

The Desired Capabilities code is given below –

AppiumDriver driver;
DesiredCapabilities capabilities = new DesiredCapabilities();
   
//Set the capabilities
capabilities.setCapability("deviceName", "My Nexus");
capabilities.setCapability("udid", "ENUXXXXXXXXXXXXXXX");
capabilities.setCapability("platformVersion", "6.0.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
   
//Launch the app using the appium port number that you provided in the Appium server
try {
    driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    Thread.sleep(10000);
} catch (MalformedURLException e) {
    e.printStackTrace();
}

Couple of important points about the code –

1. deviceName is a mandatory property, but its value can be anything. deviceName is not used to identify your mobile device in anyway

2. udid is not a mandatory property. If you use only one mobile device, then you can run your scripts without this property as well. Since you have only 1 device and that means only 1 Appium server running, so Appium can run the script based on just the port number. However, when you have multiple devices connected to your device, you would need to provide their UDIDs so that Appium can identify the device using the UDID. This UDID is the same that was displayed when you executed adb devices command

If you want to run your code sequentially on both the devices, you just need to write the same code twice and pass different values for Appium port and device id. That way when you run the test case, it will run it on both the devices one after the other.

Code to run test script in parallel on both devices

Below is the code that you can use to launch the app simultaneously on both devices. The program uses Java concurrency concepts such as Thread and Runnable Interface to run the test script in parallel on both devices. Executing the code launches the app in the same way as is shown in the video in the beginning of the article.

The below code uses Selenium Page Factory Model, in the way it identifies the objects in the mobile app. If you are not comfortable with Page Factory Model then can use their own logic. Only important thing that you have to make sure is that you should pass the proper port number and device id, when you implement the Runnable interface.

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class AppiumParallelExecution implements Runnable {
   
    String port;
    String udid;
   
    public AppiumParallelExecution(String portNumber, String udid) {
        this.port = portNumber;
        this.udid = udid;
    }
   
    @AndroidFindBy(id="com.amazon.mShop.android.shopping:id/action_bar_burger_icon")
    private WebElement hamburgerIcon;
   
    @AndroidFindBy(id="com.amazon.mShop.android.shopping:id/gno_greeting_text_view")
    private WebElement hello;
   
    @AndroidFindBy(id="ap_email")
    private WebElement email;
   
    @AndroidFindBy(id="ap_password")
    private WebElement password;
   
    AppiumDriver driver;
    DesiredCapabilities capabilities = new DesiredCapabilities();
   
   
    private void openAppAndPerformSomeActions() {
        capabilities.setCapability("deviceName", "My Mobile Device");
        capabilities.setCapability("udid", udid);
        capabilities.setCapability("platformVersion", "6.0.1");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
        capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
       
        try {
            driver = new AndroidDriver(new URL("http://0.0.0.0:" + port + "/wd/hub"), capabilities);
            Thread.sleep(10000);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
       
        hamburgerIcon.click();
        hello.click();
    }
   
    public static void main(String args[]) {
        Runnable r1 = new AppiumParallelExecution("5000", "ah37kjf982a2b2c1a9"); //device id of first mobile device
        Runnable r2 = new AppiumParallelExecution("4723", "BDE3N1678E001068"); //device id of second mobile device
        new Thread(r1).start();
        new Thread(r2).start();
    }

    @Override
    public void run() {
        openAppAndPerformSomeActions();
    }
}

Try it out and let us know how it works out for you. Also please share with us your feedback on this article, on how we can improve our articles to make it more useful to all our readers.

<!–

–>

Leave a Reply

Your email address will not be published. Required fields are marked *