</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

What is TestNG and why is it used with Selenium?

TestNG is a Java testing framework used with Selenium to organize tests, manage dependencies, run in parallel, and generate reports.

Answer

TestNG (Test Next Generation) is a Java testing framework designed for automation testing. It is the most commonly used testing framework alongside Selenium WebDriver.

Why TestNG with Selenium:

  • @Test annotation marks test methods
  • @BeforeMethod and @AfterMethod for setup and teardown
  • Run tests in parallel — reduces execution time significantly
  • Group tests by category (smoke, regression, sanity)
  • Data-driven testing with @DataProvider
  • Prioritize test order with priority attribute
  • XML suite configuration with testng.xml
  • Built-in HTML reporting

Basic TestNG test:

Java
import org.testng.annotations.*;
import org.testng.Assert;

public class LoginTest {
    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://automateqa.online");
    }

    @Test(priority = 1, groups = {"smoke"})
    public void testLoginSuccess() {
        driver.findElement(By.id("username")).sendKeys("admin");
        driver.findElement(By.id("password")).sendKeys("pass123");
        driver.findElement(By.id("loginBtn")).click();
        Assert.assertTrue(driver.getTitle().contains("Dashboard"));
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

Follow AutomateQA

Related Topics