Fundamentals 11 min read

Parameterized Testing with Selenium and JUnit: Techniques and Code Examples

This article explains how automation testers can use JUnit parameterized testing with Selenium to avoid repetitive test scripts, covering annotation‑based and Excel‑driven data sources, and provides complete Java code examples for both approaches.

FunTester
FunTester
FunTester
Parameterized Testing with Selenium and JUnit: Techniques and Code Examples

Automation testers often face repetitive Selenium test cases that require different inputs and environment configurations. To eliminate this redundancy, parameterized testing is introduced, allowing the same test script to run with multiple data sets, thereby saving time and effort.

The article demonstrates how to implement JUnit parameterized testing for Selenium automation, explaining why it is needed when testing across various operating systems, browsers, and data variations. It highlights the drawbacks of hard‑coded values and the benefits of using JUnit’s parameterization features.

Two main approaches for JUnit parameterized tests are covered:

Using the @Parameters annotation to supply Java collections as test data.

Driving tests with an external Excel file, enabling data‑driven testing without modifying the code.

Using @Parameters Annotation

First, a non‑parameterized example is shown, followed by a refactored version that accepts parameters via the @Parameters annotation. The code includes a test class with a constructor, a static method returning a Collection of data, and a test method that receives the parameters.

package FunTester;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class SearchTest {
    private String kyWrd1;
    private String kyWrd2;
    private SearchGoogle searchGoogle;
    public SearchTest(String kyWrd1, String kyWrd2) {
        this.kyWrd1 = kyWrd1;
        this.kyWrd2 = kyWrd2;
    }
    @Before
    public void init() {
        search = new Search();
    }
    @Parameterized.Parameters
    public static Collection data() {
        return Arrays.asList(new Object[][]{{"FunTester","FunTester"},{"JMeter","Selenium"},{"UiAutomator","appium"}});
    }
    @Test
    public void testSearch() {
        searchGoogle.searchKeys(kyWrd1, kyWrd2);
    }
}

A runner class using JUnitCore.runClasses is also provided to execute the parameterized tests.

package parameterizedRun;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class Runner {
    public static void main(String[] args) {
        Result res = JUnitCore.runClasses(SearchTest.class);
        for (Failure fail : res.getFailures()) {
            System.out.println(fail.toString());
        }
        System.out.println(res.wasSuccessful());
    }
}

Excel‑Driven Parameterization

Excel data‑driven testing is introduced as an alternative, where test data is stored in an Excel worksheet and read at runtime. A utility class reads the workbook, and the test method retrieves data via excel.getData(row, column, sheet). This approach decouples test data from code, improving maintainability.

package FunTester;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestSearch {
    WebDriver driver;
    ReadExcel excel = new ReadExcel();
    @Before
    public void startUp() {
        System.out.println("----开始测试----");
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe的path");
        driver = new ChromeDriver();
        driver.get("https://www.****.com/");
        driver.manage().window().maximize();
    }
    @Test
    public void searchKeys() {
        System.out.println("----搜索关键字----");
        WebElement srchBox = driver.findElement(By.name("q"));
        srchBox.sendKeys(excel.getData(0, 1, 0) + "
");
        String title = driver.getTitle();
        System.out.println("The title is : " + title);
    }
    @After
    public void tearDown() {
        System.out.println("----结束测试----");
        driver.quit();
    }
}

The article concludes with references to related tutorials and a list of popular posts from the FunTester community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JUnitExcelparameterized-testingtest-automation
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.