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.
|@BeforeSuite||The annotated method will be run before all tests in this suite have run.|
|@AfterSuite||The annotated method will be run after all tests in this suite have run.|
|@AfterTest||The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.|
|@BeforeGroups||The 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.|
|@AfterGroups||The 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.|
|@BeforeClass||The annotated method will be run before the first test method in the current class is invoked.|
|@AfterClass||The annotated method will be run after all the test methods in the current class have been run.|
|@BeforeMethod||The annotated method will be run before each test method.|
|@AfterMethod||The annotated method will be run after each test method.|
Quick and Easy - Run TestNG Tests for Mobile \ Web with seetest.io
Now that you are all set, with basics of TestNG and Mobile/Browser testing.
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.
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.
The Jars will be built in target directory which can be used set in your classpath for the project.
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.
Automated test using TestNG
TestNG is a feature rich framework but to write a very basic test its a simple task.
Write business logic of the 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
Create a testng.xml
Run the TestNG
Assuming TestNG is set in your classpath, TestNG can be invoked as below
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,
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.
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.
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.
In the above example client is requesting specific Android application on an Android Device.
In the above example client is requesting a chrome browser on that an Android Device .
Create a Driver with desired 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.
Driver creation ideally should be done in a function with "@BeforeClass" TestNG annotation since it can be reused in all the test methods.
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 a Browser:
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.
Open the appium-java-testng project in IntelliJ or Eclipse as Gradle project.
Upload your application to a project manually by following the documentation in Native Applications Testing.
Modify the relevant <platform>.app.name property in src/main/java/resources/seetest.properties with the correct application name. See below.
Create a class under the package io.appium.testng. Please use the template class below to kick start your testing.
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
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.
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.
Run the modified project using modified testng.xml.