Page tree
Skip to end of metadata
Go to start of metadata

The Basics

TestNG is an open source Java testing framework which is powerful yet easy to use. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc.

Before moving ahead with TestNG, it is important to understand it's basic architecture. Diagram below depicts its architecture at high level.

Below are some terminologies used in above diagram to help us understand more about the architecture.

Suite → Top level of any TestNG project and is maintained as an XML artifact.

Test → Representation of Test which can be one or more in a Suite.

Classes → Also known as TestNG class, contains one or more test methods.

Methods → An Optional container for one or more test methods. These test methods contains actual test code. 

TestNG executes any test suite, following the order defined by the testng.xml. Test developers can plugin their logic in this flow of execution using special configuration Annotations.

Listed below are these Annotations with their description.

@BeforeSuiteThe annotated method will be run before all tests in this suite have run.
@AfterSuiteThe annotated method will be run after all tests in this suite have run.
@AfterTestThe annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
@BeforeGroupsThe list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroupsThe list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClassThe annotated method will be run before the first test method in the current class is invoked. 
@AfterClassThe annotated method will be run after all the test methods in the current class have been run. 
@BeforeMethodThe annotated method will be run before each test method. 
@AfterMethodThe annotated method will be run after each test method.

Quick and Easy - Run TestNG Tests for Mobile \ Web with

Now that you are all set, with basics of TestNG and Mobile/Browser testing.

  1. Find the sample test in our git repository.
  2. Fork the repository.
  3. Edit the test and hit Run

Before you proceed with fetching the sample tests, you will have to do two more things :

  1. If you plan to test a native or hybrid app - Upload your app to your project
  2. Fetch your access key

TestNG - Further Reading - Advanced Concepts

To start using TestNG, you need its libraries. There are two ways you can do it and actually depends on underlying build mechanism of the test suite.

  1. Build and Use the library
    In case the underlying build system of your project does not have capability of Automatic discovery of dependency (Example: Maven or Gradle), then you will have to build TestNG source and then use the Jars.

    $ git clone git://
    $ cd testng
    $ ./build-with-gradle

    The Jars will be built in target directory which can be used set in your classpath for the project.

  2. Configure dependency 

    If the your project uses Gradle or Maven, then TestNG dependency needs to be configured.

    Maven: Configure following in pom.xml.


    Gradle : Configure following in build.gradle.

    repositories {
    dependencies {
        compile 'org.testng:testng:6.10'

Automated test using TestNG

TestNG is a feature rich framework but to write a very basic test its a simple task. 

  1. Write business logic of the test.

    Test Method
    package test;
    public class HelloWorldTest {
    	public void testHelloWorld() {
    		// write business logic for test

    Notice the "@Test" annotation above, this annotation marks a function as test so that TestNG engine runs it as a test without even being configured in testng.xml

  2. Create a testng.xml 

    <suite name="Suite1" verbose="1" >
      <test name="HelloWorldTest" >
           <class name="test.HelloWorldTest" />
  3. Run the TestNG

    Assuming TestNG is set in your classpath, TestNG can be invoked as below

    java org.testng.TestNG testng.xml

Mobile and Browser Testing using TestNG

TestNG at its core is a Java testing framework, but it does not have direct capabilities to test Mobile and Browser based applications. Hence we need Java based libraries in conjunction with TestNG to achieve the same. 

Appium and Selenium software provide Java based client bindings to interact with Appium/Selenium servers which work on Mobile devices and Browsers.

In order to use TestNG in conjunction with Appium and Selenium you will have to do the following,

  1. Download  Java bindings for Appium or Configure Appium/Selenium dependency in build tool.

    Assuming if you are using Gradle as build system, you need to configure dependencies in build.gradle as shown below.

    repositories {
    dependencies {
        compile group: 'io.appium', name: 'java-client', version: '+'
        compile group: 'org.testng', name: 'testng', version: '6.9.10'

    Notice the 'io.appium' group above.

    Note: Best practice is to create the Gradle Project/Maven Project using IntelliJ or Eclipse and configure dependency in build tool.

  2. Implement a TestNG class which uses Appium library.

    Typically, we need to first setup/create a Driver instance for testing an Device or Browser. This is done by creating Desired Capabilities and then using it to create the Dri

    Create Desired Capabilities: 

    Desired Capabilities are features which Client requests to Appium/Selenium server. 

    Desired Capabilities for Mobile
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("appActivity", ".loginActivity");
    capabilities.setCapability("appPackage", "com.experitest.ExperiBank/.LoginActivity");

    In the above example client is requesting specific Android application on an Android Device.

    Desired Capabilities for Browser
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");

    In the above example client is requesting a chrome browser on that an Android Device .

    Create a Driver with desired capabilities

    Create Driver for an Android Device
    AndroidDriver driver = new AndroidDriver<AndroidElement>(new URL(""), capabilities);

    First argument is the appium service and second argument is capabilities object.

    Similarly, browser application can be tested by creating an instance of RemoveWebDriver object.

    Create Driver for Browser
    RemoteWebDriver = new RemoteWebDriver (new URL(""), capabilities);

    Driver creation ideally should be done in a function with "@BeforeClass" TestNG annotation since it can be reused in all the test methods.

    public class EriBankTest {
    	private DesiredCapabilities capabilities = new DesiredCapabilities();
    	private AndroidDriver driver = new AndroidDriver<AndroidElement>();
    	public void setup() {
    		capabilities.setCapability("platformName", "Android");
    		capabilities.setCapability("appActivity", ".loginActivity");
    		capabilities.setCapability("appPackage", "com.experitest.ExperiBank/.LoginActivity");
    		AndroidDriver driver = new AndroidDriver<AndroidElement>(new URL(""), capabilities);
  • Implement Test Methods in TestNG class.

    Once the Driver is created, it can be used to Locate the elements in Mobile application or Browser and test or automate. 

    Test Method for a Mobile application:

    Test Method for Device Application
    public void testLogin() {
        // Locate the elements to test.

    Test Method for a Browser:

    Test Method for Browser
    public void testGetTitle(String title) {

Extending Sample TestNG Project

Now that you have got the sample test from the git repository running, lets try to extend it to run for a your own application instead of EriBank application to a specific targeted device in seetest cloud.

Here are the step by step procedure to achieve that.

  1. Open the appium-java-testng project in IntelliJ or Eclipse as Gradle project.

  2. Upload your application to a project manually by following the documentation in Native Applications Testing.

  3. Modify the relevant <platform> property in src/main/java/resources/ with the correct application name. See below.

    Note : The link Native Applications Testing mentions about the application name which can be used as a value for <platform> property under heading “Upload your Application Manually”.

  4. Create a class under the package io.appium.testng. Please use the template class below to kick start your testing.

    Java Template class for extending tests
    package io.appium.testng;
    import io.appium.java_client.remote.MobileCapabilityType;
    import org.testng.annotations.Optional;
    import org.testng.annotations.Test;
    import utils.SeeTestProperties;
    public class FirstTest extends TestBase {
         * Sets up the default Desired Capabilities.
        protected void initDefaultDesiredCapabilities() {
  "Enter initDefaultDesiredCapabilities");
  "Exit initDefaultDesiredCapabilities");
         * sets the application ("app") capability based on the OS and the property which was defined in the file
         * @param os
        private void setAppCapability(@Optional("android") String os) {
            String appName = os.equals("android") ?
                    properties.getProperty(SeeTestProperties.Names.ANDROID_APP_NAME) :
            appName = String.format("%s%s", "cloud:", appName);
  "Setting up {} as app capability", appName);
            dc.setCapability(MobileCapabilityType.APP, appName);
       private void buildDeviceQuery() {
            this.deviceQuery = String.format("%s and %s","@os='" + os + "'", "@serialnumber='LGH990f2578648'");
            dc.setCapability(SeeTestCapabilityType.DEVICE_QUERY, this.deviceQuery);
  "Device Query in buildDeviceQuery() - " + this.deviceQuery);
        public void firstTest() {
         // add the operations and tests.

    a) firstTest function can be added with any functionality you want to test.

    b) Please Edit @serialnumber in buildDeviceQuery function (i.e. @serialnumber='LGH990f2578648') to any Device ID of your choice by following this video 

  5. Now edit the src/main/java/testng.xml to add following reference of the Test class ( <test name="FirstTest">) we introduced before, at Step 3.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "">
    <suite name="TestNGExample" configfailurepolicy="continue" verbose="2">
       <!--<parameter name = "userName" value="company"/>
       <test name="AndroidTestNGExample">
             <class name="io.appium.testng.AndroidTestNGExampleTest">
                <parameter name = "userName" value="company"/>
                <parameter name = "os" value="android"/>
                   <include name="makePaymentTest" />
                   <include name="balanceTest" >
                      <parameter name = "expectedBalance" value="90"/>
                   <include name="dummyTest()"/>
       <test  name="IOSTestNGExample">
          <parameter name = "os" value="ios"/>
             <class name="io.appium.testng.IOSTestNGExampleTest">
                   <include name="makePaymentTest" />
                   <include name="balanceTest" >
                      <parameter name = "expectedBalance" value="90"/>
       <test name="WebTestNGexample">
             <class name="io.appium.testng.WebTestNGExampleTest">
                <parameter name = "os" value="android"/>
                   <include name="titleTest" >
                      <parameter name = "url" value=""/>
                      <parameter name = "title" value="Google"/>
       <test name="FirstTest">
             <class name="io.appium.testng.FirstTest">
                <parameter name = "os" value="android"/>

    Note :

    • Other tests in the testng.xml are commented because they depend on EriBank application.

    • It's important to have right combination of the parameter name =”os” in the testng.xml and Device ID, i.e the value (android or ios) needs to same as the operating system of Device ID.

  6. Run the modified project using modified testng.xml.

  • No labels