Parameterization in Selenium Java Using TestNG
When building robust test automation frameworks using Selenium and Java, one of the most crucial aspects is parameterization—the ability to run the same test multiple times with different sets of input data. This not only makes your tests more scalable and efficient but also ensures better coverage with fewer lines of code.
TestNG, a popular testing framework in the Java ecosystem, provides built-in support for parameterization, making it easy to pass data into test methods at runtime. In this blog, we’ll explore how to implement parameterization in Selenium Java using TestNG and look at different ways to supply test data.
🔍 Why Parameterization?
Imagine a scenario where you want to test the login functionality of a web application with multiple sets of credentials. Without parameterization, you’d need to write multiple test methods or duplicate code — which is inefficient and hard to maintain.
With parameterization, you can:
Run the same test logic with different inputs
Reduce code duplication
Increase test coverage
Easily manage data-driven testing
🧪 Methods of Parameterization in TestNG
TestNG supports several ways to pass parameters:
@Parameters Annotation (XML-Based)
@DataProvider Annotation (Code-Based)
@Factory Annotation (Advanced Use)
Let’s go through the most common and effective ones.
✅ Using @Parameters Annotation
This method allows you to pass parameters from the testng.xml file to your test method.
Step 1: Define Parameters in testng.xml
xml
<suite name="Suite">
<test name="Login Test">
<parameter name="username" value="admin"/>
<parameter name="password" value="admin123"/>
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
Step 2: Use @Parameters in Your Test Class
java
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class LoginTest {
@Test
@Parameters({"username", "password"})
public void login(String username, String password) {
System.out.println("Username: " + username);
System.out.println("Password: " + password);
// Selenium login steps here
}
}
✅ Using @DataProvider Annotation
@DataProvider is a more flexible, programmatic way to supply multiple sets of data to a single test method.
Step 1: Create a Data Provider Method
java
import org.testng.annotations.DataProvider;
public class TestData {
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"admin", "admin123"},
{"user1", "pass1"},
{"user2", "pass2"}
};
}
}'
Step 2: Use @DataProvider in Your Test Method
java
import org.testng.annotations.Test;
public class LoginTest {
@Test(dataProvider = "loginData", dataProviderClass = TestData.class)
public void login(String username, String password) {
System.out.println("Username: " + username);
System.out.println("Password: " + password);
// Selenium login automation
}
}
This test will run three times — once for each set of credentials provided by the getLoginData() method.
📌 Benefits of Using DataProvider
No need to touch XML files
Can pull data from external sources like Excel, JSON, or databases
Reusable and modular
Supports complex data types
Final Thoughts
Parameterization in Selenium using TestNG is an essential feature for creating scalable and maintainable test automation frameworks. While @Parameters is simple and great for a small set of data, @DataProvider is the preferred choice for data-driven testing, especially when working with large or dynamic data sets.
By effectively using parameterization, you ensure your tests are DRY (Don’t Repeat Yourself), flexible, and easier to manage as your application grows. Whether you're testing login credentials, form submissions, or search filters, TestNG’s parameterization makes your Selenium automation more powerful and productive.
Learn Selenium with Java Training
Read More: Setting Up Log4j Logging in Selenium Java Projects
Read More: Handling Multiple Windows in Selenium WebDriver Java
Read More: Using Assertions in Selenium Java with TestNG
Visit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment