Using TestNG with Selenium: Why and How
✅ Using TestNG with Selenium: Why and How
Selenium is a powerful tool for automating browser actions. But on its own, Selenium doesn’t provide features like test execution flow, grouping, parallel runs, or reporting.
That’s where TestNG comes in.
π What Is TestNG?
TestNG (Test Next Generation) is a testing framework for Java, inspired by JUnit and NUnit, but with more powerful features, such as:
Annotations to control test flow
Flexible test configuration
Test grouping and prioritization
Parallel test execution
HTML/XML reports
Integration with tools like Maven, Jenkins, and CI/CD
π€ Why Use TestNG with Selenium?
Feature Benefit
Test organization Write clean, modular test cases
Test prioritization Control the order of test execution
Data-driven testing Easily run tests with multiple inputs
Parallel execution Speed up testing across browsers/tabs
Reporting Get detailed test reports automatically
π How to Use TestNG with Selenium (Step-by-Step)
π§ 1. Set Up a Selenium + TestNG Project
You can create a Maven project and include the dependencies in pom.xml.
xml
Copy
Edit
<dependencies>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.19.0</version>
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>
✍️ 2. Create a Simple Test Class
java
Copy
Edit
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GoogleTest {
WebDriver driver;
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
@Test
public void checkTitle() {
String title = driver.getTitle();
assert title.contains("Google");
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
π§ͺ 3. Run the Test
You can run your tests:
From your IDE (like IntelliJ or Eclipse)
Using the TestNG XML file for configuration
Via Maven or CI/CD tools (like Jenkins)
π️ 4. Use TestNG XML to Organize Tests
Create a testng.xml file:
xml
Copy
Edit
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="My Test Suite">
<test name="Google Tests">
<classes>
<class name="GoogleTest"/>
</classes>
</test>
</suite>
Run it with:
bash
Copy
Edit
mvn test -DsuiteXmlFile=testng.xml
π Common TestNG Annotations
Annotation Purpose
@BeforeClass Runs once before the first test method
@AfterClass Runs after all test methods are done
@BeforeMethod Runs before each test method
@AfterMethod Runs after each test method
@Test Marks a method as a test case
@DataProvider Supplies data to test methods (for loops)
@Parameters Used for parameterizing test values
π§ Tips & Best Practices
Use @DataProvider for data-driven testing (e.g., login with different users)
Group tests with groups attribute to control test execution
Use assertions (Assert.assertTrue(), Assert.assertEquals()) to validate behavior
Use listeners or ITestListener for custom reporting or logging
π Summary
Tool Purpose
Selenium Automates browser actions
TestNG Manages test execution, flow, and reports
Combining Selenium + TestNG gives you a powerful test automation framework for UI testing, regression, and continuous integration.
Learn Selenium JAVA Training in Hyderabad
Read More
Top 10 Selenium WebDriver Commands Every Beginner Should Know
Understanding XPath and CSS Selectors in Selenium
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment