Open In App

Why to use Selenium with TestNG?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium is one of the most widely used tools for automating web browsers, but when combined with TestNG, its capabilities are enhanced significantly. TestNG is a powerful testing framework that provides structure, flexibility, and comprehensive reporting to Selenium tests.

Integrating Selenium with TestNG allows for efficient test case management, parallel execution, and data-driven testing, making it a preferred choice for developers and testers alike.

TestNG Framework Features

TestNG provides a wide array of features that make it an ideal companion for Selenium:

  • Annotations: Simplifies the creation and management of test cases.
  • Test Configuration: Provides @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, and other annotations to configure tests at various levels.
  • Parallel Execution: Supports running tests in parallel, reducing execution time.
  • Data-Driven Testing: Facilitates running the same test with different data sets.
  • Grouping of Tests: Allows categorization of test cases, making it easier to manage large test suites.
  • Reporting: Generates detailed HTML reports, making it easier to track test execution and results.

These features make TestNG a robust and flexible testing framework, particularly when integrated with Selenium.

Approach/Algorithm to Solve this Problem

To understand why Selenium with TestNG is beneficial, let's break down the approach:

  1. Set Up WebDriver: Use WebDriverManager to set up the browser driver, ensuring compatibility and avoiding manual setup.
  2. Initialize the WebDriver: Create an instance of WebDriver (e.g., ChromeDriver).
  3. Configure Test Cases: Use TestNG annotations like @BeforeTest, @Test, and @AfterTest to structure and configure test cases.
  4. Execute the Test: Automate a simple scenario, such as searching on Google, and validate the outcome.
  5. Generate Reports: After the test execution, view the TestNG-generated reports to analyze the results.

Example

Below is an example of how to automate a Google search using Selenium WebDriver and TestNG.1. Set Up Dependencies

First, include the necessary dependencies in your Maven pom.xml file:

XML
<project xmlns="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.tests</groupId>
    <artifactId>GoogleSearchTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Selenium WebDriver Dependency -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.14.0</version>
        </dependency>

        <!-- WebDriverManager Dependency -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.5.3</version>
        </dependency>

        <!-- TestNG Dependency -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.7.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- Surefire Plugin for running TestNG tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


GoogleSearchTest.java

Java
package com.example.tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GoogleSearchTest {
    WebDriver driver;

    @BeforeTest
    public void setUp() {
        // Set up ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();

        // Initialize ChromeDriver instance
        driver = new ChromeDriver();

        // Maximize the browser window
        driver.manage().window().maximize();

        // Navigate to Google.com
        driver.get("https://www.google.com/");
    }

    @Test
    public void searchTest() throws InterruptedException {
        // Find the search box element by name and enter the search query
        driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");

        // Simulate hitting the Enter key
        driver.findElement(By.name("q")).submit();

        // Pause execution for a few seconds to see the results
        Thread.sleep(3000);

        // Validate the search result page title contains the search query
        String pageTitle = driver.getTitle();
        System.out.println("Page title is: " + pageTitle);

        // Basic validation
        if (pageTitle.contains("Selenium WebDriver")) {
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed!");
        }
    }

    @AfterTest
    public void tearDown() {
        // Close the browser
        driver.quit();
    }
}


Output:

output
GoogleSearchTest.java output

Selenium Code without TestNG

Without TestNG, the structure and execution of your Selenium tests would be less organized and less flexible. Below is an example of the same Google search test without using TestNG:

Java
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class GoogleSearchWithoutTestNG {
    public static void main(String[] args) throws InterruptedException {
        // Set up ChromeDriver using WebDriverManager
        WebDriverManager.chromedriver().setup();

        // Initialize ChromeDriver instance
        WebDriver driver = new ChromeDriver();

        // Maximize the browser window
        driver.manage().window().maximize();

        // Navigate to Google.com
        driver.get("https://www.google.com/");

        // Find the search box element by name and enter the search query
        driver.findElement(By.name("q")).sendKeys("Selenium WebDriver");

        // Simulate hitting the Enter key
        driver.findElement(By.name("q")).submit();

        // Pause execution for a few seconds to see the results
        Thread.sleep(3000);

        // Validate the search result page title contains the search query
        String pageTitle = driver.getTitle();
        System.out.println("Page title is: " + pageTitle);

        // Basic validation
        if (pageTitle.contains("Selenium WebDriver")) {
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed!");
        }

        // Close the browser
        driver.quit();
    }
}


Output:

GoogleSearchWithoutTestNG-output
GoogleSearchWithoutTestNG output

Conclusion

Using Selenium with TestNG offers a seamless way to organize, execute, and report your automated tests. The combination of Selenium's robust browser automation and TestNG's advanced testing features results in a powerful framework that boosts productivity and ensures more reliable test outcomes. Whether you're working on a small project or a large-scale application, Selenium with TestNG is the go-to solution for efficient and effective test automation.


Article Tags :

Similar Reads