How to run Appium Parallel Execution using TestNG?
Pre-Requisites:
- Have a knowledge on TestNG. Please refer TestNG Tutorial.
- Node.js should be installed on the machine.
Procedure:
- As of now, we have launched Appium using Appium.exe in windows/ Appium.app in MAC. We will Start/Run Appium which is allocated to run only one instance.
- To Run multiple instances we need to Start/Run Multiple Appium servers to do that we will use the help of Node.js.
Start First Instance:
1) Now open Command Prompt change your directory to the Appium installed location C:\Program Files (x86)\Appium\node_modules\appium\bin
3) Now type the command : node appium.js -a 127.0.0.1 -p 1234 -cp 1234 -bp 2345
Noticed that the First instance is created with URL : 0.0.1:1234/wd/hub
Description about Command:
- Node Appium.js: To start the instance with node with Appium.js located in the location too provide Appium server arguments
- For address we have to pass “-a” with the address here address is “127.0.0.1”: “ -a 127.0.0.1 “
- For port we have to pass “-p” with port number here port number is “1234”: “-p 1234 “
- For callbackPort we have to pass “-cp” with port number here port number is “1234”: “-cp 1234 “
- For BootstrapPort we have to pass “-bp” with port number here port number is “2345”: “-bp 2345 “
Start the Second Instance:
1) Follow the same steps but now open up an another command prompt.
2) Provide the command with a different address, port, callbackPort, and BootstrapPort as below command: node appium.js -a 127.0.0.2 -p 3456 -cp 3456 -bp 4567
3) Second instance is created with URL : 0.0.2:3456/wd/hub
Now both the command prompts are running different instances of Appium server.
After having an idea of how to launch multiple Appium server instances. It’s time to get ready with the script.
Here is the code below parameterization in TestNG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package appium_Package; import org.testng.annotations.Test; import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Parameters; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterMethod; public class Appium_TestNG_Sample_One { private static AndroidDriver<MobileElement> driver; @Test public void f() { } @Parameters({ "deviceName_","UDID_","platformVersion_", "URL_" }) @BeforeMethod public void beforeMethod(String deviceName_,String UDID_,String platformVersion_,String URL_) throws MalformedURLException, InterruptedException { File classpathRoot = new File(System.getProperty("user.dir")); File appDir = new File(classpathRoot, "/Apps/Amazon/"); File app = new File(appDir, "in.amazon.mShop.android.shopping.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); capabilities.setCapability("deviceName", deviceName_); capabilities.setCapability("udid", UDID_); capabilities.setCapability("platformVersion", platformVersion_); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("app", app.getAbsolutePath()); capabilities.setCapability("appPackage", "in.amazon.mShop.android.shopping"); capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity"); driver = new AndroidDriver<MobileElement>(new URL("https://"+URL_), capabilities); driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS); Thread.sleep(10000); } @AfterMethod public void afterMethod() { } } |
Now, these parameters is passed from the TestNG.xml with two different tests. Below is the XML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite" parallel="tests" thread-count="2" > <test name="Test1"> <parameter name="deviceName_" value="device_One"/> <parameter name="UDID_" value="UDID_One"/> <parameter name="platformVersion_" value="Version_One"/> <parameter name="URL_" value="127.0.0.1:1234/wd/hub"/> <classes> <class name="appium_Package.Appium_TestNG_Sample_One"/> </classes> </test> <!—Test 1 --> <test name="Test2"> <parameter name="deviceName_" value="device_Two"/> <parameter name="UDID_" value="UDID_Two"/> <parameter name="platformVersion_" value="Version_Two"/> <parameter name="URL_" value="127.0.0.2:3456/wd/hub"/> <classes> <class name="appium_Package.Appium_TestNG_Sample_One"/> </classes> </test> <!—Test 2 --> </suite> <!-- Suite --> |
Now it’s time to connect your two devices (USB/Wifi). Identify whether the devices are connected with system and “ADB” can access it by the following command in the command prompt: “adb devices”. Then after getting connected devices list, Note down the UDID of the devices and provide in the testNG.xml along with Device name, Platform Version and URL of different instances.
That’s it now right click on the TestNG.xml–>RunAs–>TestNG Suite
Watch the execution of the device in both devices.