What is TestNG and How to use TestNG in Selenium | TestNG Selenium Tutorial
By: Dhawal Joshi
Tags: Selenium, Automation Testing, Automation, Testng, Selenium Webdriver

TestNG Features

What is TestNG

TestNG is a Testing framework which helps in effective execution of automated test cases. TestNG in selenium could be understood as a layer which not only facilitates in test execution management (for eg. parallel test execution, setting the test execution priority, creating separate test suites as per requirement etc.) but also in Reporting, logging, data provider (parameterization) etc. as described in the above figure. In this TestNG selenium tutorial, we will look upon all the TestNG features mentioned in the figure.

Please visit HERE to read with proper format and more content

Eclipse does not ship with TestNG, hence we will first install TestNG in eclipse. Navigate to Help -> Install New Software

This will open a new Install window (see below). Locate the address bar next to Work With: and enter the following url:

Click on Add button and select the TestNG checkbox.

Once you have made your selections as in the above image, click on Next and start with the TestNG installation.

If you are prompted to enter name after you click on Add button, provide any name, preferrably TestNG.

To verify the installation is successful, right click on any of the existing package and select New -> Other. This will open Select a Wizard pop up window. Enter TestNG in the Wizards search box and it should display the TestNG Class.

Before we start writing our test scripts using TestNG it is important to remember below points regarding TestNG:

TestNG is basically a Class.

In TestNG classes, we only write our test scripts and other annotations (more on it after the next few lines).

All other logic and helper classes should be written outside the TestNG class.

We can create a testng.xml to facilitate execution of multiple classes in one go. This feature is discussed in detail under Annotations section.

Above TestNG attributes should become more clear once we start writing our tests.

Select File -> New -> Java Project and create a new java project SeleniumTestNG. Proceed with steps 1, 2 and 3 as described in the following figure.

You must have already observed @Test keyword in our TestNG class. This keyword is known as an annotation and is always followed by a method or function. This method should be the code for a test case. A TestNG class may have multiple @Test annotations, and thus multiple methods (multiple test scripts).

Suppose we have to automate 3 manual test cases: Login, Search and Logout. To make things simple, we will only print some text under these tests and going forward will scale this class to give you an actual picture of how TestNG is used in real projects.

Update the default code with the following code:

package testpkg;

import org.testng.annotations.Test;

public class NewTest {

@Test

public void loginTest(){

System.out.println("Under Login Test");

} @Test

public void searchTest(){

System.out.println("Under Search Test");

} @Test

public void logoutTest(){

System.out.println("Under Logout Test");

} }

If you get any error for testng package, mouse hover the testng import line (second line) and select Add TestNG Library option.

To run the TestNG class, just right click and select Run As -> TestNG Test

Since we have 3 @Test in our TestNG class, results on console are displayed as PASSED three times for each @Test (method). Also, the execution of the test cases is not in the order they are written. logoutTest() is written in the last but it gets executed before searchTest(). By default, TestNG tests run in alphabetical order.

There is one extra tab when we execute TestNG class, Results of running class <className>. This tab presents the execution summary in more user-friendly way. Click on this tab and explore yourself. Since we have kept our test scripts simple and they all pass, there is not much in TestNG Results tab.

TestNG generates a default report after test execution is completed. TestNG report is quite readable and we get this functionality out of the box from TestNG.

To generate TestNG report for the above project once the execution is completed, select the project, right-click and select Refresh. A new folder test-output will be created under the project.

If you open the index.html file in Chrome, it will display a complete execution report for the TestNG class we have created. Explore the report yourself and see what all it offers.

TestNG offers some more annotations which makes tester’s life easy. Consider an example where you have to close the browser and open it after each @Test. TestNG extends its help in rescuing us from this regressive task.

The annotation @BeforeMethod will be executed before each @Test. Similarly, @AfterMethod will be executed after the execution of @Test. Following code will make things more clear:

package testpkg;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test; public class NewTest {

@BeforeMethod

public void openBrowser() {

System.out.println("Open Browser");

} @Test

public void loginTest(){

System.out.println("Under Login Test");

} @Test

public void searchTest(){

System.out.println("Under Search Test");

} @Test

public void logoutTest(){

System.out.println("Under Logout Test");

} @AfterMethod

public void closeBrowser() {

System.out.println("Close Browser");

}

}

New annotations are highlighted in the above code. We already know that each @Test represents one test case and each test case one method is written. Code written in @BeforeMethod annotation will always run before each @Test is executed. Similarly, @AfterMethod will be executed after each @Test. For the above example, @BeforeMethod and @AfterMethod code will be executed 3 times each. The output of above program is:

Since TestNG contains the @Test annotation or methods which represent a test TestNG class is also known as Test class. Suppose we need to fetch some values from an excel sheet before starting our tests, we will utilize @BeforeTest and @AfterTest annotations.

@BeforeTest method will be executed before any test and even before @BeforeMethod annotation. Similarly, @AfterTest method will be executed after all @Test and @AfterMethod are executed.

Unlike @BeforeMethod and @AfterMethod these annotations will be executed only once. Changes to the code are in bold:

package testpkg;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test; public class NewTest {

@BeforeTest

public void beforeTestAnnotation() {

System.out.println("Fetching data from excel");

} @BeforeMethod

public void openBrowser() {

System.out.println("Open Browser");

} @Test

public void loginTest(){

System.out.println("Under Login Test");

} @Test

public void searchTest(){

System.out.println("Under Search Test");

} @Test

public void logoutTest(){

System.out.println("Under Logout Test");

} @AfterMethod

public void closeBrowser() {

System.out.println("Close Browser");

} @AfterTest

public void afterTestAnnotation() {

System.out.println("Closing the excel file");

}

}

Console output after executing the above script:

It is evident from the above output that @BeforeTest and @AfterTest are executed only once.

For the full article, please visit codeshut

Get My Free eBook on Selenium POM framework

Create your own POM framework from scratch using a live website. Visit HERE to get your own free copy.