Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Parameterized tests in Selenium with Java allow you to run the same test with different sets of input data. This is particularly useful for data-driven testing, where you want to verify the behavior of your application with various inputs.

In Java, you can use JUnit or TestNG to implement parameterized tests. Below are examples using both frameworks.

Using JUnit for Parameterized Tests

1. Adding Dependencies

First, ensure you have the necessary dependencies in your pom.xml if you’re using Maven:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
    </dependency>
</dependencies>

				
			

2. Writing Parameterized Test

Here’s an example of how to write a parameterized test using JUnit:

				
					import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class ParameterizedTest {

    private String username;
    private String password;

    public ParameterizedTest(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Parameters
    public static Collection<Object[]> testData() {
        return Arrays.asList(new Object[][]{
                {"user1", "pass1"},
                {"user2", "pass2"},
                {"user3", "pass3"}
        });
    }

    @Test
    public void testLogin() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("http://example.com/login");

        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("loginButton")).click();

        // Add assertions here
        driver.quit();
    }
}

				
			

Using TestNG for Parameterized Tests

1. Adding Dependencies

First, add the necessary dependencies to your ‘pom.xml‘ if you’re using Maven:

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Writing Parameterized Test

Here’s an example of how to write a parameterized test using TestNG:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParameterizedTestNGTest {

    WebDriver driver;

    @BeforeMethod
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @DataProvider(name = "loginData")
    public Object[][] loginData() {
        return new Object[][]{
                {"user1", "pass1"},
                {"user2", "pass2"},
                {"user3", "pass3"}
        };
    }

    @Test(dataProvider = "loginData")
    public void testLogin(String username, String password) {
        driver.get("http://example.com/login");

        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("loginButton")).click();

        // Add assertions here
    }

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

				
			

Key Points

  • JUnit uses the ‘@RunWith(Parameterized.class)‘ and ‘@Parameters‘ annotations to set up parameterized tests.
  • TestNG uses the ‘@DataProvider‘ annotation to provide data sets and the ‘dataProvider‘ attribute in the’ @Test‘ annotation to specify the data provider method.
  • Both frameworks allow you to run the same test with different inputs, making it easier to perform data-driven testing.

These examples demonstrate how to set up and execute parameterized tests in Selenium with Java using both JUnit and TestNG.

Scroll to Top