Integration of Selenium with TestNG


In this tutorial, I’ll create a Framework for the testing of web applications using Selenium Webdriver with TestNG. This framework consists of:-

  1. Selenium
  2. Java 8 or above
  3. TestNG
  4. Maven

Implementation Steps:

  1. Download and Install Java on the system
  2. Download and setup Eclipse IDE on the system
  3. Setup Maven
  4. Create a new Maven Project
  5. Add Selenium and TestNG dependencies to the project
  6. Create a Test file under src/test/java
  7. Run the tests from TestNG.xml
  8. TestNG Report Generation

Detailed Step Description

Step 1- Download and Install Java

Selenium needs Java to be installed on the system to run the tests. Click here to know How to install Java.

Step 2 – Download and setup Eclipse IDE on the system

The Eclipse IDE (integrated development environment) provides strong support for Java developers, which is needed to write Java code. Click here to know How to install Eclipse.

Step 3 – Setup Maven

To build a test framework, we need to add a number of dependencies to the project. It is a very tedious and cumbersome process to add each dependency manually. So, to overcome this problem, we use a build management tool. Maven is a build management tool that is used to define project structure, dependencies, build, and test management. Click here to know How to install Maven.

Step 4 – Create a new Maven Project

Click here to know How to create a Maven project

Below is the Maven project structure. Here,

Group Id – com.example
Artifact Id – Selenium_TestNGDemo
Version – 0.0.1-SNAPSHOT
Package – com. example. Selenium_TestNGDemo

Step 5 – Add Selenium and TestNG dependencies to the project

As this is a Maven project, we can add the dependencies in POM.xml as shown below.

 <!-- -->
<!-- -->

<!-- -->

After the addition of dependencies in pom.xml, the Maven Dependencies folder will be updated automatically with all the JAR file related to the dependencies.

Step 6 – Create a Test file under src/test/java

@BeforeTest – methods under this annotation will be executed prior to the first test case in the TestNG file.

@AfterTest – methods under this annotation will be executed after all test cases in the TestNG file are executed.

@Test – The annotated method is part of a test case.

Description –  You can describe your test case under the description, stating what it does.

description = "This test validates title of login functionality"

Priority – You can prioritize the order of your test methods by defining a priority. Based on the defined priority, the test shall execute in that order.

priority = 0
public class TestNG_Demo {

	WebDriver driver;

	public void setUp() {

		driver = new FirefoxDriver();

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

	@Test(description = "This test validates title of login functionality", priority = 0)
	public void verifyLoginPage() {

		String expectedTitle = driver.findElement(By.xpath("//*[@id='logInPanelHeading']")).getText();

		System.out.println("Title :" + expectedTitle);
		assertTrue(expectedTitle.equalsIgnoreCase("LOGIN Panel"));

	@Test(description = "This test validates  successful login to Home page", priority = 1)
	public void verifyHomePage() {

		System.out.println("Username Entered");

		System.out.println("Password Entered");


		String newPageText = driver.findElement("welcome")).getText();
		System.out.println("newPageText :" + newPageText);
		assertThat(newPageText, containsString("Welcome"));

	public void teardown() {



Step 7 – Test Execution through TestNG

Go to the Runner class and right-click Run As TestNG Test. The tests will run as TestNG tests.

Step 8 – Run the tests from TestNG.xml

Create a TestNG.xml as shown below and run the tests as TestNG.

<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name = "Suite1">
  <test name = "TestNG Demo">
          <class name = "com.example.Selenium_TestNGDemo.TestNG_Demo"/>

Step 9 – TestNG Report Generation

TestNG generates various types of reports under test-output folder like emailable-report.html, index.html, testng-results.xml.

We are interested in the ’emailable-report.html’ report. Open ’emailable-report.html’, as this is an HTML report, and open it with the browser. The below image shows emailable-report.html.

TestNG also produces an index.html report, and it resides under the test-output folder. The below image shows the index.html report. This is the latest theme of the report.

The links present on the left side are clickable. I have clicked the Times link, and you can see the details on the right side.

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s