Introduction
Selenium WebDriver is a popular tool for automating web applications. However, Selenium alone does not provide a complete test management framework for organising, executing and reporting automated test cases.
This is where TestNG becomes useful.
TestNG is a Java testing framework inspired by JUnit and NUnit. It provides features such as annotations, assertions, test prioritisation, grouping, parameterisation, dependencies and parallel execution.
When combined with Selenium WebDriver, TestNG helps automation testers build structured, maintainable and reusable test automation suites.
In this tutorial, we will understand TestNG concepts and learn how to integrate TestNG with Selenium using practical Java examples.
1. What Is TestNG?
TestNG stands for Test Next Generation. It is a testing framework designed to simplify test organisation and execution.
It is commonly used in Selenium automation projects to:
Organise test cases.
Control test execution order.
Run tests in groups.
Validate expected and actual results.
Execute tests with different data sets.
Support parallel execution.
Generate execution reports.
TestNG is particularly useful when developing a Selenium automation framework for a real-world application.
2. Installing TestNG in a Selenium Java Project
You can use TestNG with Eclipse and Maven.
Step 1: Create a Maven Project
Open Eclipse and create a new Maven project.
Step 2: Add Dependencies
Add Selenium and TestNG dependencies to your pom.xml file.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.38.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>These are example dependency versions. Use compatible current versions when setting up a new project.
Step 3: Install the TestNG Eclipse Plugin
In Eclipse:
Open the Eclipse Marketplace.
Search for TestNG.
Install the TestNG plugin.
Restart Eclipse if prompted.
You can also execute TestNG tests through Maven without relying on the Eclipse plugin.
3. Understanding TestNG Annotations
Annotations are one of the most important features of TestNG. They control when different test methods and configuration methods execute.
Common TestNG annotations include:
| Annotation | Purpose |
|---|---|
@BeforeSuite | Runs before the test suite |
@AfterSuite | Runs after the test suite |
@BeforeTest | Runs before the relevant XML test |
@AfterTest | Runs after the relevant XML test |
@BeforeClass | Runs before the first test method in a class |
@AfterClass | Runs after all test methods in a class |
@BeforeMethod | Runs before each test method |
@AfterMethod | Runs after each test method |
@Test | Marks a test method |
Example of TestNG Annotations
import org.testng.annotations.*;
public class AnnotationExample {
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Test Method");
}
@Test
public void testLogin() {
System.out.println("Executing Login Test");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Test Method");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}
}The annotations help testers control test setup, execution and cleanup.
4. Writing a Selenium TestNG Test Case
Let us create a simple Selenium test that opens a webpage and verifies its title.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class SeleniumTestNGExample {
WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com");
}
@Test
public void verifyPageTitle() {
String actualTitle = driver.getTitle();
Assert.assertEquals(
actualTitle,
"Example Domain"
);
}
@AfterMethod(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}In this example:
@BeforeMethodlaunches the browser and opens the webpage.@Testexecutes the validation.Assert.assertEquals()compares the actual and expected page titles.@AfterMethodcloses the browser even if the test fails.
5. TestNG Assertions
Assertions are used to verify whether an application behaves as expected.
TestNG provides several assertion methods.
Assert Equals
Assert.assertEquals(actual, expected);Checks whether two values are equal.
Assert True
Assert.assertTrue(driver.getTitle().contains("Example"));Checks whether a condition is true.
Assert False
Assert.assertFalse(driver.getTitle().contains("Error"));Checks whether a condition is false.
Assert Not Null
Assert.assertNotNull(driver);Checks whether an object is not null.
Hard Assertions and Soft Assertions
A hard assertion stops the current test method when an assertion fails.
Soft assertions allow execution to continue after an assertion failure. The tester must call assertAll() to report accumulated failures.
import org.testng.asserts.SoftAssert;
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actualTitle, expectedTitle);
softAssert.assertTrue(driver.getCurrentUrl().contains("example"));
softAssert.assertAll();6. TestNG Priority
TestNG allows test methods to be assigned priorities.
@Test(priority = 1)
public void loginTest() {
System.out.println("Login Test");
}
@Test(priority = 2)
public void dashboardTest() {
System.out.println("Dashboard Test");
}
@Test(priority = 3)
public void logoutTest() {
System.out.println("Logout Test");
}Lower numeric priorities execute before higher numeric priorities under normal TestNG execution.
However, priority should not be used as a substitute for proper test independence or dependency management.
7. TestNG Groups
Groups allow related test cases to be organised and executed together.
@Test(groups = {"smoke"})
public void loginTest() {
System.out.println("Smoke Test");
}
@Test(groups = {"regression"})
public void paymentTest() {
System.out.println("Regression Test");
}You can select groups for execution through a TestNG XML configuration file.
8. TestNG XML Configuration
A TestNG XML file helps control which test classes, methods or groups should execute.
Example testng.xml:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="AutomationSuite">
<test name="LoginTests">
<classes>
<class name="SeleniumTestNGExample"/>
</classes>
</test>
</suite>You can run this configuration as a TestNG Suite from Eclipse or through a compatible Maven TestNG setup.
9. TestNG Parameterisation
Parameterisation allows the same test method to execute with different input values.
TestNG supports parameters through @Parameters and data-driven execution through @DataProvider.
Using DataProvider
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"user1", "pass1"},
{"user2", "pass2"},
{"user3", "pass3"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
System.out.println(username);
System.out.println(password);
}This approach is useful for testing login functionality with multiple combinations of credentials.
10. Parallel Execution in TestNG
TestNG supports parallel execution through XML configuration.
Example:
<suite name="ParallelSuite" parallel="methods" thread-count="3">
<test name="AutomationTests">
<classes>
<class name="SeleniumTestNGExample"/>
</classes>
</test>
</suite>Parallel execution can reduce overall test duration. However, Selenium tests must use properly isolated WebDriver instances, commonly through ThreadLocal or another safe driver-management approach, to avoid browser-session conflicts.
11. TestNG Reports
TestNG generates execution reports after running tests.
Common output files include:
test-output/index.htmltest-output/emailable-report.html
These reports help testers review:
Passed test cases.
Failed test cases.
Skipped tests.
Execution details.
Error information.
For larger automation projects, TestNG reports can also be integrated with reporting tools such as Allure or Extent Reports.
12. Best Practices for Selenium TestNG Frameworks
Keep test methods focused on one validation objective.
Use meaningful test method names.
Separate browser setup and cleanup from test logic.
Use assertions to validate actual application behaviour.
Use DataProviders for multiple test inputs.
Organise smoke and regression tests into groups.
Avoid unnecessary dependencies between test cases.
Use isolated WebDriver instances for parallel execution.
Maintain reusable methods and Page Object Model classes.
Integrate automated execution with CI/CD pipelines.
Frequently Asked Questions
1. What is TestNG in Selenium?
TestNG is a Java testing framework used with Selenium WebDriver to organise, execute and report automated test cases.
2. What are the most important TestNG annotations?
Common annotations include @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeSuite and @AfterSuite.
3. What is the difference between hard and soft assertions?
Hard assertions stop the current test method after a failure. Soft assertions collect failures and report them when assertAll() is called.
4. What is DataProvider in TestNG?
@DataProvider supplies multiple sets of test data to a test method.
5. Can TestNG execute Selenium tests in parallel?
Yes. TestNG supports parallel execution at suite, test, class and method levels, provided the automation framework manages browser sessions safely.
6. Is TestNG required for Selenium?
No. Selenium can be used with other test frameworks, but TestNG is a popular choice for Java-based automation projects.
Learn Selenium Automation Testing with Palium Skills
Palium Skills provides practical automation testing training for freshers, manual testers and working professionals.
The Selenium and Playwright training curriculum can include:
Selenium WebDriver with Java and Python.
TestNG annotations, assertions and DataProviders.
XPath and CSS Selector techniques.
Page Object Model and automation framework development.
API testing and integration.
Maven, Git and Jenkins.
Real-time automation projects.
Interview preparation and career guidance.
Training modes: Classroom sessions at South Kolkata and Salt Lake centres, and instructor-led live online training for learners across India.
Contact Palium Skills
Website: www.paliumskills.com
Call: 8420594969 / 7044871915
WhatsApp: 9903130500
Email: info@paliumskills.com
South Kolkata Centre: 1st Floor, Sheeba Bhavan, 1/22 Poddar Nagar (Near South City Mall), Kolkata – 700068.
Salt Lake Centre: 5th Floor, RDB Boulevard, Block GP, Salt Lake Electronic Complex, Kolkata – 700091.
Build practical Selenium automation and software testing skills with Palium Skills through classroom and live online training.