What is TestNG?
The TestNG testing framework is superior to previous frameworks such as JUnit and NUnit. TestNG is more adaptable and expandable, with capabilities that facilitate the creation and execution of tests.
Installing and Configuring TestNG in Eclipse
Here’s how to install TestNG on your system to ensure you can execute your tests efficiently.
Install TestNG
Use the following command to install TestNG:
> Sudo apt-get install TestNG
Add TestNG to Your Project
You can add TestNG to your project by adding this dependency to your pom.xml.file:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.12.3</version></dependency>
Configure TestNG in Your Project
Configuring TestNG into your project will enable testing. To do this, add the following XML to your pom.xml.file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile/></suiteXmlFiles></configuration></plugin></plugins></build>
How to run tests with TestNG
The – java org.testng.TestNG – command line will run your tests in the same order specified in the testng.xml.file. Further, to select different XML files, you can use the -Dtestng.xml option. For instance, you would use the following command to run tests in your smoke-test suite:
- java org.testng.TestNG -Dtestng.xml=smoke-tests.xml testng.xml
The -suitename option can also help you run multiple test suites simultaneously. For example, if you wished to run your smoke-test and regression-test test suites, you would run the following command:
- java org.testng.TestNG -suitenameSmokeTests,RegressionTests testng.xml
Use -test names or -groups to run a group of tests or a specific test. To run “myFirstTest” and “mySecondTest,” use the command:
- java org.testng.TestNG -testnamesmyFirstTest,mySecondTest testng.xml
The -groupsof option runs all tests in a specific group. To run all tests in the “smoke-tests” group, run the command:
- java org.testng.TestNG -groupsof smoke-tests testng.xml
You can use the <class> tag’s groups attribute to specify a test class group. To add a test class to the “smoke-tests” group, use the following XML:
<test>
<classes>
<class name=”MyTestClass” groups=”smoke-tests”/>
</classes>
</test>
Putting together a test suite with TestNG is a simple process. To configure your tests, you must generate a testng.xml file and define the settings. Once complete, you can execute your tests using the – java org.testng.TestNG – command-line. In addition, TestNG enables you to select the sequence in which it will run your tests and how it will categorize them. Because of this, it is simple to develop exhaustive test suites that can be executed and maintained effectively.
Using Appium With Selenium Grid
Scaling Up Parallel Testing
There is a limit to the maximum number of real or virtual devices that can run on a single system. For example, you cannot run over 100 Appium tests in parallel on a single Appium server. For this you will need Selenium testing. Eventually, you will need multiple automation servers running simultaneously, making your test script complex. Additionally, your test script must know the host and port of each server. If not, it can start multiple tests on a server and overload it.
Selenium Grid
Selenium Grid helps mitigate this complexity. Selenium Grid is a proxy server and a special-purpose load balancer that connects with the WebDriver protocol and manages connections. Since Appium also connects to the WebDriver protocol, Selenium automation testing and the Selenium Grid works well with Appium.
Starting Up the Grid
Here’s how you can set up the Gird:
- Download the latest Selenium Server.
- To ensure you have a working Chrome desktop automation, get the latest Chromedriver.
- Have an Andriod emulator running with the laster version of Google Chrome.
- Open four separate terminal windows.
Here’s what you run in the windows:
- The Grid hub
java -jar /path/to/selenium-server-standalone.jar -role hub
- The iOS node
appium -p 4723 –nodeconfig /path/to/nodeconfig-ios.json
- The Android node
appium -p 4733 –nodeconfig /path/to/nodeconfig-android.json
- The Chrome desktop node
java -Dwebdriver.chrome.driver=”/path/to/chromedriver” -jar /path/to/selenium-server-standalone.jar -role node -nodeConfig /path/to/nodeconfig-chrome.json
Get all the additional nodeconfig JSON files here.
nodeconfig-android.json:
{
“capabilities”:
[{
“browserName”: “Chrome”,
“platformVersion”: “26”,
“maxInstances”: 1,
“platformName”: “Android”,
“automationName”: “UiAutomator2”,
“deviceName”: “Android Emulator”
}
],
“configuration”:
{
“cleanUpCycle”:2000,
“timeout”:30000,
“proxy”: “org.openqa.grid.selenium.proxy.DefaultRemoteProxy”,
“maxSession”: 1,
“register”: true,
“registerCycle”: 5000,
“hubPort”: 4444,
“hubHost”: “127.0.0.1”,
“hubProtocol”: “http”
}
}
nodeconfig-chrome.json:
{
“capabilities”:
[{
“browserName”: “chrome”,
“browserVersion”: “71”,
“platformName”: “MAC”,
“maxInstances”: 1
}
],
“cleanUpCycle”:2000,
“timeout”:30000,
“proxy”: “org.openqa.grid.selenium.proxy.DefaultRemoteProxy”,
“maxSession”: 1,
“register”: true,
“registerCycle”: 5000,
“hubPort”: 4444,
“hubHost”: “127.0.0.1”,
“hubProtocol”: “http”
}
With this, the setup is complete. Once you’ve set up everything, you will receive messages in the Grid console informing you of the nodes that have checked in. It will look something like this:
13:48:04.514 INFO [DefaultGridRegistry.add] – Registered a node http://0.0.0.0:4733
Conclusion
We have only covered the basics of using Appium with Selenium and TestNG. HeadSpin supports Appium mobile testing and works with Jonathan Lipps, Sr. Director of Automation Technologies and Appium’s architect. HeadSpin is also a contributor to Appium.
Additionally, the HeadSpin Platform enables mobile app testing, empowering its users to connect to real SIM-enabled devices worldwide.