Parametrization with Data-Driven Frameworks in a Spring Boot Selenium Setup: Efficient Test Scripting

Introduction

Parametrization is a useful technique that enables writing basic test scripts and reusing them for multiple data sets. It allows testing multiple scenarios with different inputs, instead of writing multiple test scripts for each case. Data-driven frameworks are designed to implement parametrization by inputting the data from external files like CSV, Excel, etc. In this tutorial, we will go through the concept of parametrization with data-driven frameworks in a Spring Boot Selenium setup.

Prerequisites

  • Basic knowledge of Java programming language

  • Basic knowledge of Spring Boot framework and Selenium WebDriver

Steps for Implementation

Step 1: Create a TestScript

First, we need to create a test script with a test method that we will reuse for multiple data sets. Here we create a basic login test method inside a Spring Boot test class:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class LoginTest {

    @Autowired
    private WebDriver driver;

    @Test
    public void testLogin() {
        driver.get("https://example.com");
        WebElement username = driver.findElement(By.id("username"));
        username.sendKeys("testuser");
        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("testpassword");
        WebElement submit = driver.findElement(By.xpath("//input[@type='submit']"));
        submit.click();
        Assert.assertTrue(driver.getCurrentUrl().equals("https://example.com/home"));
    }
}

Here we use the @RunWith annotation for running the test Spring Boot context along with Selenium WebDriver, and the @SpringBootTest annotation for specifying the application port. We also use @Autowired to inject the WebDriver instance into the class.

Step 2: Create an External File with Test Data

Create an external file (e.g., CSV or Excel) with test data. For example, we can create a CSV file named "testdata.csv" with data for the username and password fields:

username,password
user1,pass1
user2,pass2
user3,pass3

Step 3: Implement the Data-Driven Framework

In this step, we will use a data-driven framework to read the data from the external file and execute the test script with different data sets.

First, we will add a data provider to our test script that reads the data from the external file:

@DataProvider(name="loginData")
public Object[][] loginDataProvider() {
    String csvPath = "testdata.csv";
    String line;
    String[] data;
    List<Object[]> testData = new ArrayList<Object[]>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(csvPath));
        while ((line = br.readLine()) != null) {
            data = line.split(",");
            testData.add(data);
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return testData.toArray(new Object[0][]);
}

Here, we use the @DataProvider annotation to provide the inputs to the test method. We read the CSV file and create a list of Object[] arrays for each row in the CSV file.

Next, we modify our test method to accept parameters from the data provider:

@Test(dataProvider="loginData")
public void testLogin(String username, String password) {
    driver.get("https://example.com");
    WebElement usernameField = driver.findElement(By.id("username"));
    usernameField.sendKeys(username);
    WebElement passwordField = driver.findElement(By.id("password"));
    passwordField.sendKeys(password);
    WebElement submitButton = driver.findElement(By.xpath("//input[@type='submit']"));
    submitButton.click();
    Assert.assertTrue(driver.getCurrentUrl().equals("https://example.com/home"));
}

The test method now accepts two parameters, username and password, and uses them in place of the hardcoded values.

Step 4: Configure TestNG for Spring Boot

TestNG in a Spring Boot setup needs appropriate configurations to work. To configure it, create a TestNG xml file named testng.xml with the following contents:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Spring Boot Test Suite">
  <test name="Spring Boot Login Test" >
    <classes>
      <class name="com.example.LoginTest" />
    </classes>
  </test>
</suite>

This file specifies the test classes to be executed in the test suite.

Step 5: Execute the Test Script

Now we can execute our test script with multiple data sets using the external file data. We can use the following command to execute the test script:

shell script mvn test -Dsurefire.suiteXmlFiles=testng.xml

This command triggers the Maven build and executes our test script using TestNG with the specified XML configuration.

Conclusion

In this tutorial, we've covered how to implement parametrization with data-driven frameworks in a Spring Boot Selenium setup. Data-driven frameworks allow us to write basic test scripts and reuse them for multiple data sets, enabling us to test various scenarios with different inputs. By combining the Spring Boot framework with Selenium WebDriver and TestNG, we can create efficient and organized test scripts.

Did you find this article valuable?

Support Rishav Trivedi by becoming a sponsor. Any amount is appreciated!