Reading test data from an Excel file using JUnit and Apache POI

To maintain test cases and test data, Microsoft Excel is the favorite tool used by testers. Compared to the CSV file format, Excel gives numerous features and a structured way to store data. A tester can create and maintain tables of test data in an Excel spreadsheet easily.

In this recipe, we will use an Excel spreadsheet as your data source. We will use the Apache POI API, developed by the Apache Foundation, to manipulate the Excel spreadsheet. This recipe also implements some negative test handling.

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 an Excel spreadsheet with the required data

We will also need a SpreadsheetData helper class to read the Excel spreadsheets. This is available in the source code bundle for this book. This class supports both the old .xls and newer .xlsx formats.

How to do it...

Let's create a test that uses Excel spreadsheet test data for parameterization, as shown in the following code example:

package com.secookbook.examples.chapter07;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;
import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;

@RunWith(Parameterized.class)
public class ExcelTestData {

  private static WebDriver driver;

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

  @Parameters
  public static Collection testData() throws Exception {
    InputStream spreadsheet = new FileInputStream("./src/test/resources/testdata/Data.xlsx");
    return new SpreadsheetData(spreadsheet).getData();
  }

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

  @BeforeClass
  public static void setUp() throws Exception {

    // Create a new instance of the Chrome driver
    driver = new ChromeDriver();
    driver.get("http://cookbook.seleniumacademy.com/bmicalculator.html");
  }

  @Test
  public void testBMICalculator() throws Exception {
    WebElement heightField = driver.findElement(By.name("heightCMS"));
    heightField.clear();
    if (!height.equals("<Blank>")) {
      heightField.sendKeys(this.height);
    }

    WebElement weightField = driver.findElement(By.name("weightKg"));
    weightField.clear();
    if (!weight.equals("<Blank>")) {
      weightField.sendKeys(this.weight);
    }

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

    if (error.equals("<Blank>")) {
      WebElement bmiField = driver.findElement(By.name("bmi"));
      assertEquals(this.bmi, bmiField.getAttribute("value"));

      WebElement bmiCategoryField = driver.findElement(By.name("bmi_category"));
      assertEquals(this.bmiCategory,
          bmiCategoryField.getAttribute("value"));
    } else {
      WebElement errorLabel = driver.findElement(By.id("error"));
      assertEquals(this.error, errorLabel.getText());
    }
  }

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

How it works...

When the test is executed, the testData() method will create an instance of the SpreadsheetData class. The SpreadsheetData class reads the contents of the Excel spreadsheet row by row in a collection and returns this collection back to the testData() method:

InputStream spreadsheet = new FileInputStream("./src/test/resources/testdata/Data.xlsx");
return new SpreadsheetData(spreadsheet).getData();

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 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 a CSV file using JUnit recipe
..................Content has been hidden....................

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