Back to Top

Getting started with TestNG Selenium

Updated 28 February 2020

TestNG is a testing framework sparked from JUnit and NUnit but advancing some new functionalities that make it more robust and easier to use. TestNG include all levels of tests: unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher. 

TestNG Features

  • Supports Before and after annotations.
  • Parameterization of test methods.
  • Supports Data-Driven Testing using Data providers.
  • Test cases can be arranged and prioritized easily.
  • Supports threat safe environment when executing multiple threads.
  • Easily integrate with various tools and plug-ins like build tools (Ant, Jenkins, Maven etc.), Integrated Development Environment (Eclipse).
  • Facilitates user with effective means of Report Generation using ReportNG.
  • Support for multi-threaded testing.

Installation and Setup

If you are using Eclipse IDE, it’s very easy to install TestNG. Below are the steps to install TestNG.

Step 1: Launch eclipse IDE -> Click on the Help option in the menu -> Select “Eclipse Marketplace” option in the dropdown.


Step 2: Enter “TestNG” in the search field and click on the “Go” button.


Step 3: Now, click on the Install button to install TestNG.

Start your headless eCommerce
Find out More

So, After the Installation Eclipse would recommend you to restart in order to reflect the changes made. After restarting Eclipse, you can verify the TestNG installation.

Go to Window->Preferences.


TestNG: First Test

Let us start with the creation of TestNG project.

Step 1: Click on the File option in the menu -> Click on New -> Select Java Project.


Step 2: Enter the project and click on the “Finish” button.


Step 3: Configure the TestNG library inside the Java project. Go to “Configure Build Path” -> Click on the “Libraries” tab. Click on “Add library” as shown below.


Step 4: Now, select TestNG and click on the Next button. and, click on the Finish button.

The TestNG is now added to the Java project.

NOTE: Add all the JAR files inside the selenium and libs folders.

Creating TestNG File

Step 1: Right-click the src folder of the Java Project and select New > Other…

Select TestNG class and then click Next


Step 2: Now, enter the Source folder, package name and the TestNG class name. Here, we can also select multiple TestNG notations.


Click Finish. The template for your first TestNG file will be created with the default schema

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class NewTest {
  public void f() {
  public void beforeTest() {

  public void afterTest() {


The @Test  annotation is the root of TestNG test cases, tells that the method under it is a ‘test case’. Here, the method f is a test case.

The @BeforeTest annotation tells that the method under it will be executed before the first test case. The @AfterTest annotation tells that the method under it will be executed after the last test case.

Let’s now code an actual test code. In the following code snippet, we will be automating the sign-up flow using annotations.

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;

public class NewTest {

WebDriver driver;

public void testSetup()
System.setProperty("", ".\\Driver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


public void openBrowser()
System.out.println("We are currently on the following URL" +driver.getCurrentUrl());

@Test(description="This method validates the sign up functionality")
public void signUp()
driver.findElement("fullname")).sendKeys("John Doe");
driver.findElement("emailid")).sendKeys("[email protected]");


public void postSignUp()


public void afterClass()


Execute the TestNG script

In order to run the test, follow the below steps:

Right-click the NewTest folder and Select Run as > TestNG Test:


The results of the test will be displayed into two windows the console window and in the TestNG results window:


To generate the HTML report

  1. Right-click the Project and select Refresh. A test-output folder will be created:
  2. Open the test-output folder.
  3. Open on the “emailable-report.html” file

This is all about creating a TestNG test file, and to create a test suite and to run the automated test using TestNG.

Thanks for reading this blog 🙂

Happy testing!!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home