Executing Appium Tests Parallelly On Multiple Android Devices

Executing automated tests simultaneously in a distributed fashion is a great way to optimize execution time. This article illustrates how to use of Appium test automation tool on multiple physical devices simultaneously.Appium uses TestNG and Selenium Grid for Parallel test execution on multiple mobile devices.

Note: Currently Parallel test execution on mobile devices work only for Android Platform.

Before we start thinking about what it means to the Testing industry, let us agree on the definition, to be on the same page!

What do you need?

  • Selenium Standalone Server jar
  • Hub Configuration json File
  • Node Configuration Json Files
  • Android devices (2 or more with api level >19)

Appium – Appium is an open source test automation tool developed and supported by Sauce Labs to automate native and hybrid mobile apps. It uses JSON wire protocol internally to interact with iOS and Android native apps using the Selenium WebDriver.

Hub Setup

Launch command prompt and navigate to location where selenium standalone jar file is located

And run the following command to launch Selenium Hub with json file.

java –jar selenium-standalone-server 2.50.1.jar –role hub -hubConfig path\to\hubconfig.json

Below is the json file content for hub

{ "host": null, "port": 4444, "newSessionWaitTimeout": -1, "servlets" : [], "prioritizer": null, "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher", "throwOnCapabilityNotPresent": true, "nodePolling": 5000, "cleanUpCycle": 5000, "timeout": 300000, "maxSession": 5 }

 

Node Setup

Start Appium servers as Nodes using node configuration json files. Each node will hold the information of android devices (Android version, Appium URL…)

Node1

Following is the Json file content for node1, here URL refers the Http protocol that will listen on given Appium server port number (e.g. http://0.0.0.0:4723/wd/hub).

 { "capabilities": [ { "browserName": "ANDROID", "device": "GT-I9300", "version":"4.3", "maxInstances": 1, "platform":"ANDROID" } ], "configuration": { "cleanUpCycle":2000, "timeout":10000, "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", "url":"http://0.0.0.0:4723/wd/hub", "maxSession": 4, "port": 4723, "host": "0.0.0.0", "register": true, "registerCycle": 5000, "hubPort": 4444, "hubHost": "localhost" } } 

Launch a new command prompt and type the following line:

appium –nodeconfig path\to\nodeconfig1.json -p 4723 –bp 5723

Note: here –p refers to the main Appium port to listen to given number (e.g. 4723) & -bp refers to the Appium bootstrap port to use on device to talk to Appium (-bp will work only for Android).

Node2

Following is the Json file content for node2,

 { "capabilities": [ { "browserName": "ANDROID", "device": "Nexus 5", "version":"4.4.2", "maxInstances": 1, "platform":"ANDROID" } ], "configuration": { "cleanUpCycle":2000, "timeout":10000, "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", "url":"http://0.0.0.0:4724/wd/hub", "maxSession": 4, "port": 4724, "host": "0.0.0.0", "register": true, "registerCycle": 5000, "hubPort": 4444, "hubHost": "localhost" } } 

Similarly for node2, launch a new command prompt and type the following:

appium –nodeconfig path\to\nodeconfig2.json -p 4724 –bp 5724

Parallel Execution with TestNG

Create a new java class and a new method inside it. Annotate the method with @Test, @parameters. Provide optional values with @Optional annotation (we can override the values at the time of calling testing.xml.).

The following is the list of parameters being passed.

App – (.apk) file Location,
deviceName – Name of the device (e.g. LG Nexus 5),
deviceVersion – Android OS version (e.g. 4.4) ,
udid – Unique Device Identifier.

The following sample of Java class will perform simple actions on ISTQB hybrid app on android devices.

 package com.testSuite; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.*; public class SampleGridTest { private AndroidDriver driver; DesiredCapabilities capabilities = new DesiredCapabilities(); String app= "D:\\sampleApp\\net.one97.xxxxx.apk"; @Parameters({"deviceName","version","udid", "url"}) @Test public void appiumGridTest(@Optional("Galaxy SIII") String deviceName, @Optional("4.3") String version, @Optional("xxxxxxxxxxx") String udid, @Optional("http://0.0.0.0:4723/wd/hub") String url) { capabilities.setCapability("app", app); capabilities.setCapability("deviceName", deviceName); capabilities.setCapability("deviceVersion",version); capabilities.setCapability("udid", udid); try{ driver = new AndroidDriver(new URL(url), capabilities); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement swipeField = driver.findElement(By.id("net.one97.xxxxx:id/help_overlay")); /* Click on Swipe Field and No thanks button if present*/ if(swipeField.isDisplayed()){ swipeField.click(); } WebElement NoThanks_Option = driver.findElement(By.name("No, thanks")); if(NoThanks_Option.isDisplayed()){ NoThanks_Option.click(); } /*Clicking on Account,Wallet and Home Buttons*/ driver.findElement(By.name("Account")).click(); driver.findElement(By.name("Wallet")).click(); driver.findElement(By.name("Login to xxxxx")).click(); driver.findElement(By.name("Home")).click(); }catch (MalformedURLException e) { e.printStackTrace();} driver.quit(); } } 

Create a TestNG.xml file in the project root folder, copy the following content to it and update the values per your device specifications.

Right click on TestNG.xml file -> click on Run As -> TestNG Suite.

Appium, an open-source test automation tool is designed to meet mobile automation needs on iOS and Android platforms. ZenQ teams are well-versed in executing Appium Tests parallelly on multiple Android devices. Our clients have experienced increase in efficiency and reduced turnaround time in test automation.