Reading test data from a CSV file using JUnit

We saw a simple data-driven test using JUnit and TestNG. The test data was hardcoded in the test script code. This could become difficult to maintain. It is recommended that we store the test data separately from the test scripts.

Often we use data from the production environment for testing. This data can be exported in CSV format. We will use OpenCSV library to read a CSV file. For more details on OpenCSV, headon to http://opencsv.sourceforge.net/.

In this recipe, we will read data from a CSV file and use this data to execute the test script.

Getting ready

To begin, follow these steps:

  • Add OpenCSV dependency to the Maven pom.xml file:
    <dependency>
      <groupId>com.opencsv</groupId>
      <artifactId>opencsv</artifactId>
      <version>3.4</version>
      <scope>test</scope>
    </dependency>
    
  • Prepare a CSV file with the required data.

How to do it...

Let's create a new JUnit test and CSV parametrization, as shown in the following example:

package com.secookbook.examples.chapter07;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import org.junit.runners.Parameterized.Parameters;

import com.opencsv.CSVReader;

import static org.junit.Assert.*;

import java.io.*;
import java.util.*;

@RunWith(Parameterized.class)
public class CsvTestData {

  private static WebDriver driver;

  private String height;
  private String weight;
  private String bmi;
  private String bmiCategory;

  @Parameters
  public static List<String[]> testData() throws IOException {
    return getTestData("./src/test/resources/testdata/data.csv");
  }

  public CsvTestData(String height, String weight, String bmi,
      String bmiCategory) {
    this.height = height;
    this.weight = weight;
    this.bmi = bmi;
    this.bmiCategory = bmiCategory;
  }

  public static List<String[]> getTestData(String fileName)
      throws IOException {
    CSVReader reader = new CSVReader(new FileReader(fileName));
    List<String[]> myEntries = reader.readAll();
    reader.close();
    return myEntries;
  }

  @BeforeClass
  public static void setUp() throws Exception {
    // Create a new instance of the Firefox driver
    driver = new FirefoxDriver();
    driver.get("http://cookbook.seleniumacademy.com/bmicalculator.html");
  }

  @Test
  public void testBMICalculator() throws Exception {
    WebElement heightField = driver.findElement(By.name("heightCMS"));
    heightField.clear();
    heightField.sendKeys(height);

    WebElement weightField = driver.findElement(By.name("weightKg"));
    weightField.clear();
    weightField.sendKeys(weight);

    WebElement calculateButton = driver.findElement(By.id("Calculate"));
    calculateButton.click();

    WebElement bmiLabel = driver.findElement(By.name("bmi"));
    assertEquals(bmi, bmiLabel.getAttribute("value"));

    WebElement bmiCategoryLabel = driver.findElement(By.name("bmi_category"));
    assertEquals(bmiCategory, bmiCategoryLabel.getAttribute("value"));
  }

  @AfterClass
  public static void tearDown() throws Exception {
    // Close the browser
    driver.quit();
  }
}

How it works...

When the test is executed, the testData() method will call the getTestData() helper method by passing the path of the CSV file. Inside the getTestData() method, the BufferedReader class from the java.io namespace is used to read the file line by line. Lines are then split into an array of strings using the comma delimiter. This array is then added to Collection or ArrayList, either of which is then returned to the testData() method.

For each row in the test data collection returned by the testData() method, the test runner will instantiate the test case class, passing the test data as parameters to the test class constructor, and it will then execute all the tests in the test class.

See also

  • The Creating a data-driven test using JUnit recipe
  • The Reading test data from an Excel file using JUnit and Apache POI recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.221