TestNG With Selenium

TestNG, as the name suggest, is the next generation test automation tool that fills the gaps in the JUnit framework. The basic idea behind the inception of these tools was to cut down the unit testing efforts during the Java development.

However, these tools are now being utilized by the software testers for test-driven development. Since TestNG is the new entrant, it brings a list of cool features like TestNG annotations, parallel execution, test case management, effective reporting and much more.

Interestingly, there is a TestNG plugin that we can integrate with the Eclipse IDE and instantly start creating TestNG projects in Eclipse.


How to Install TestNG in Eclipse IDE

Just to talk a little about the TestNG features such as annotations, they are like the small hooks yet so powerful where you can put instructions to control the flow of a test case, a group of test cases or the whole test suite.

Next is the intelligent reporting which comes up with the following key benefits.

  • Tracking of the application performance,
  • Enhance debugging,
  • Quick identification of the defects,
  • Aging and the historical comparison of the data.


I’ve stated earlier that I’ll present you the two ways to install TestNG in Eclipse IDE.  Let’s now get through each of the methods one by one.

  1. Install TestNG in Eclipse directly from the Eclipse Marketplace.
  2. Installing TestNG Plugin in Eclipse using the “Install New Software…” feature.

Before you move to the next section, please make sure you have got the right version of Eclipse software installed.

In case, you are uncertain or don’t have the software then use the below link to download Eclipse.


How To Install TestNG In Eclipse IDE?

1- Install TestNG In Eclipse Directly From The Eclipse Marketplace.

All the new Eclipse IDE versions are now coming up with the Eclipse marketplace plugin. It’s the easier and quicker way to install any third party plugin in Eclipse including the TestNG plugin. Eclipse Marketplace gives you a rich interface to search and install the plugins.

You can access this plugin from the help menu available on the main toolbar. Click on the “Eclipse Marketplace…” option listed under the help menu.

In the “Find” input field, write “TestNG” and press enter to search. The search will land you the desired TestNG plugin listed in the result section.

There you’ll see an “Install” button with every result that appears in the search. Press it to continue installing the TestNG for Eclipse.

You might think that when it’s so easy to install TestNG then what is the use of mentioning the old and lengthy ways to achieve the same.

We are listing the legacy TestNG installation methods because there are people who still use the old Eclipse versions. Additionally, there are IT companies who works in the restricted network environment where an automatic TestNG installation is not possible.

2- Installing TestNG Plugin In Eclipse Using The “Install New Software…” Feature.

First step:

Open the Eclipse IDE, from the main menu bar, select the Help menu and click on the “Install New Software…” option. It’ll launch an install popup window.

Second step:

Fill the BEUST site link (http://beust.com/eclipse) in the work field and click the Add button.

Add button action will open a small popup dialog asking for the name of a repository. Assign the value “TestNG” in the name field and press the OK button.

Third step:

After pressing the OK button, Eclipse will query the above link and list the available version of the TestNG plugin.

Fourth step:

Now tick on the “TestNG” item displayed in the list and click on the “Next” button present at the bottom of the popup window.

Fifth step:

Soon after pressing the “Next” button, the plugin manager will check for the required components and dependencies.

If it detects any such requirement, you’ll need to install it first before going with the TestNG installation. Though, you are unlikely to see any error/warning as in most of the cases it completes the TestNG installation without any issue.

Next, in the sequence, you’ll see a license agreement dialog, just click on the “accept…” radio button. Then click on “Finish” to complete the TestNG installation process.

Sixth step:

In this step, you’ll see a security warning which you ignore by clicking the OK button. After this Eclipse will restart to complete the TestNG installation process.


Create and execute Your First TestNG Program:

Now we have done with all the basic setup to get started with the test script creation using TestNG. In this post I will discuss How to create a sample script using TestNG.

Before creating TestNG script let’s understand how TestNG script is differ from normal java program and what are the benefits of using TestNG.

Difference between Java Program and TestNG Script

When we execute Java program and TestNG script then functionality wise nothing will change because our script is performing the same functionality but using TestNG you will get some additional functionality.

Basic Java Program

public class TestJava {

	public static void main(String[] args) {

		System.out.println("Hello java");

Basic TestNG script

import org.testng.annotations.Test;

public class TestNGScript {

	public void appTest() {

		System.out.println("Hello Selenium");

To execute TestNG script we don’t have to write the separate class. We can use simple java class but here we will not write public static void main(String []args) because we are not going to execute this from JVM.

TestNG works with Annotations and annotation can be represented by @ symbol

@Test– is this the main annotation from where TestRunner will start execution.
In other words, you can say @Test is the entry point of the program.

Some benefit of TestNG Script

1- For every single test case you will get 3 reports. These reports generated by TestNG
2-You can check execution time i.e. How much time test case has taken
3-Parallel execution  etc

Creation of First TestNG Script

Let us begin with the creation of TestNG Script in eclipse IDE.

Step-1: Create New Project and Package

First of all, Create a new java project in your eclipse with name = “TestNGDemo” and create package “TestNGPack1” under your project.

Step 2: Add TestNG Library

For adding TestNG library,

Go to your project’s Properties -> Java Build Path -> Libraries Tab.

Click on Add Library button -> Select TestNG from Add Library popup and then click on Next and Finish buttons.

It will add TestNg library in your project as shown in bellow image. Now click on Apply then OK button to close that window.

Step 3 : Create TestNG Class

To add TestNg class

Right click on package ” TestNGPack1”  -> New -> Other. It will open New wizard window as bellow.

Select TestNg from New wizard window and click on Next button.

On next screen, add class name = Program1

It will add new class “Program1” under package “TestNGPack1” as shown in bellow given image.

Step 4: Add Selenium jar files in your project.

To Run WebDriver test, you need to add Selenium jar files in your project.

(Note: Do not add junit jar file as external jar file. it’s not required in TestNG framework).

That’s all. Now you are ready to write your WebDriver test script inside your class.

Step 5: Add/write sample WebDriver test script.
Write your own test script or add bellow given script in your class file.


package TestNGPack1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Program1 {
	WebDriver driver = new FirefoxDriver();

	//@BeforeMethod defines this method has to run before every @Test methods
	public void openbrowser() {

	//@AfterMethod defines this method has to run after every @Test methods
	public void closebrowser() {
		System.out.print("\nBrowser close");

	public void appTest() {
		String title = driver.getTitle();
		System.out.print("Current page title is : " + title);
		System.out.print("\n'DemoTestNG -> TestNGPack1 -> Program1 -> 'appTest' has been executed successfully");

Step 6: Run WebDriver test script with TestNG
Right Click on “Program1” Class -> Run As -> And Click on TestNG Test as shown in bellow given image.

It will run your WebDriver test with TestNG. When execution completed, you will see result as shown in bellow.

Console Output:

[TestNG] Running:

Current page title is : Google
'DemoTestNG -> TestNGPack1 -> Program1 -> 'appTest' has been executed successfully

Browser close[Utils] Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default test.xml
[Utils]   Directory D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true

PASSED: appTest
   Default test
   Tests run: 1, Failures: 0, Skips: 0

Step 7: View test execution HTML report generated by TestNG

HTML Reports:

TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browser and it can also be viewed using Eclipse’s build –in browser support.

To generate the HTML report, follow the below steps:

Step 1: Execute the newly created TestNG class. Refresh the project containing the TestNG class by right clicking on it and selecting “Refresh” option.

Step 2: A folder named as “test-output” shall be generated in the project at the “src” folder level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.

Step 3: The HTML report shall be opened with in the eclipse environment. Refer the below image for the same.

Refresh the page to see the results for fresh executions if any.

Leave a Reply

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