TestNG Annotations in Selenium WebDriver

Last Updated : 11 Aug, 2025

TestNG is a testing framework widely used in Selenium WebDriver for automation testing. It provides a wide range of annotations that help in organizing and controlling the flow of test cases. These annotations give extra information about our methods or classes and start with the '@' symbol.

Here are the Types of TestNG Annotations:

  1. BeforeSuite: @BeforeSuite is executed before the execution of all the test cases inside a TestNG Suite.
  2. AfterSuite: @AfterSuite is executed after the execution of all the test cases inside a TestNG Suite.
  3. BeforeTest: @BeforeTest is executed before the execution of all the @test annotated methods inside a TestNG Suite
  4. AfterTest: @AfterTest is executed after the execution of all the @test annotated methods inside a TestNG Suite.
  5. BeforeClass: @BeforeClass is executed before all the methods of the current class start their execution.
  6. AfterClass: @AfterClass is executed after all the methods of the current class finish their execution.
  7. BeforeMethod: @BeforeMethod is executed before each test method within a test class. Suppose there are n test methods within a test class; then n times the @BeforeMethod annotated method will be invoked.
  8. AfterMethod: @AfterMethod is executed after each test method within a test class. Suppose there are n test methods within a test class; then n times the @AfterMethod annotated method will be invoked.
  9. BeforeGroups: When you annotate a method with @BeforeGroups, TestNG ensures that this method is invoked before any test method belonging to the specified groups is executed.
  10. AfterGroup: @AfterGroups should be executed after all the test methods belonging to a specified group have been run.

Here we include the Selenium example with TestNG Annotations:

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

Step 3: After creating the Maven Project, the project exploration will look like the image below.

1
Project Structure

Step 4: Create a TestNG Class that contains all TestNG Annotations with selenium setup.

TestNGSeleniumExample. Java

Java
package TestNGAnnotations;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

public class TestNGSeleniumExample {
    
    WebDriver driver;
    
    // @BeforeSuite - This will run before the suite begins
    @BeforeSuite
    public void beforeSuite() {
        System.out.println("Before Suite: Running once before the test suite.");
    }
    
    // @BeforeTest - This will run before any test is executed
    @BeforeTest
    public void beforeTest() {
        System.out.println("Before Test: Running before any test.");
    }

    // @BeforeClass - This will run before the first test method in the class
    @BeforeClass
    public void beforeClass() {
        System.out.println("Before Class: Running once before the first test method.");
    }
    
    // @BeforeMethod - This will run before each @Test method
    @BeforeMethod
    public void setup() {
        System.out.println("Before Method: Setting up WebDriver.");
        // Initialize WebDriver
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\change the path of the chromedriver\\drivers\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    
    // @Test - This is the test method where actual test steps are written
    @Test
    public void testGoogleTitle() {
        driver.get("https://www.google.com");
        String title = driver.getTitle();
        System.out.println("Title of the page is: " + title);
        // You can add assertions here to validate the title
    }

    // @Test - Another test method
    @Test
    public void testFacebookTitle() {
        driver.get("https://www.facebook.com");
        String title = driver.getTitle();
        System.out.println("Title of the page is: " + title);
        // You can add assertions here to validate the title
    }
    
    // @AfterMethod - This will run after each @Test method
    @AfterMethod
    public void tearDown() {
        System.out.println("After Method: Closing the browser.");
        driver.quit();
    }
    
    // @AfterClass - This will run after all the test methods in this class
    @AfterClass
    public void afterClass() {
        System.out.println("After Class: All tests in this class have finished.");
    }
    
    // @AfterTest - This will run after all the tests in the test tag are executed
    @AfterTest
    public void afterTest() {
        System.out.println("After Test: Running after all the tests.");
    }

    // @AfterSuite - This will run after the suite finishes
    @AfterSuite
    public void afterSuite() {
        System.out.println("After Suite: Running once after the test suite.");
    }
}


These annotations help manage test setup and teardown in a specific order: suite → test → class → method. They ensure proper execution and resource management.

Step 5: Run the TestNGSeleniumExample. Java with TestNG Test.

output-of-Selenium-testNG-annotations
Output of Selenium testNG annotations

TestNG annotations are important for organizing and controlling the flow of test cases in Selenium WebDriver automation. They provide a structured approach to executing tests, allowing for setup, teardown, and configuration at various levels such as suite, test, class, and method.

Comment
Article Tags:

Explore