Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but designed to cover a wider range of test categories: unit, functional, end-to-end, integration, etc. It’s particularly popular in the Java ecosystem for testing, including Selenium tests. TestNG offers powerful features, such as annotations, flexible test configuration, and powerful test execution, which make it a good fit for Selenium testing.

Key Features of TestNG

  1. Annotations: TestNG provides various annotations that help in managing the test lifecycle.
  2. Test Configuration: You can easily configure tests using XML files.
  3. Parallel Execution: TestNG allows running tests in parallel, which reduces execution time.
  4. Dependency Testing: You can specify dependencies among test methods.
  5. Grouping of Tests: It allows grouping of test methods for better organization.
  6. Data-Driven Testing: TestNG supports data-driven testing using @DataProvider.

Key Annotations in TestNG

  • @Test: Marks a method as a test method.
  • @BeforeSuite, @AfterSuite: Execute methods before/after all tests in the suite.
  • @BeforeTest, @AfterTest: Execute methods before/after any test method.
  • @BeforeClass, @AfterClass: Execute methods before/after the first method in the current class is invoked.
  • @BeforeMethod, @AfterMethod: Execute methods before/after each test method.
  • @DataProvider: Used to provide data for test methods.

Example of TestNG in Selenium

Here’s an example demonstrating how to use TestNG with Selenium WebDriver in a basic test scenario.

Step 1: Setup Maven Project

Add the following dependencies in your pom.xml:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

Step 2: Create a TestNG Test Class

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GoogleSearchTest {
    private WebDriver driver;

    @BeforeClass
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void searchTest() {
        driver.get("https://www.google.com");
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys("TestNG with Selenium");
        searchBox.submit();
        String title = driver.getTitle();
        assert title.contains("TestNG with Selenium");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

Step 3: Create a TestNG XML File

Create an XML file to configure and run your tests.

				
					<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Test Suite">
    <test name="Google Search Test">
        <classes>
            <class name="com.example.tests.GoogleSearchTest"/>
        </classes>
    </test>
</suite>

				
			

Step 4: Run the Test

You can run the test in multiple ways:

  1. Using the TestNG plugin in your IDE (like IntelliJ IDEA or Eclipse).
  2. Using the command line:
				
					mvn test

				
			

3. Running the XML file directly by right-clicking and selecting “Run As -> TestNG Suite” in your IDE.

Advanced Features

Data-Driven Testing with @DataProvider

				
					import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SearchDataProviderTest {
    @DataProvider(name = "searchData")
    public Object[][] createData() {
        return new Object[][]{
                {"TestNG with Selenium"},
                {"Selenium WebDriver"},
                {"Java Selenium"}
        };
    }

    @Test(dataProvider = "searchData")
    public void searchTest(String query) {
        // WebDriver code to perform search with the query
    }
}

				
			

Parallel Execution

Configure parallel execution in your TestNG XML file.

				
					<suite name="Selenium Test Suite" parallel="tests" thread-count="3">
    <test name="Test1">
        <classes>
            <class name="com.example.tests.TestClass1"/>
        </classes>
    </test>
    <test name="Test2">
        <classes>
            <class name="com.example.tests.TestClass2"/>
        </classes>
    </test>
</suite>

				
			

TestNG is a robust framework that enhances the capabilities of Selenium WebDriver for testing. Its rich feature set, including annotations, flexible configuration, parallel execution, and data-driven testing, makes it a powerful tool for automating web applications testing.

Scroll to Top