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:
- ✓
@Testannotation marks test methods - ✓
@BeforeMethodand@AfterMethodfor 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
priorityattribute - ✓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();
}
}
