Selenium DataProvider - 如何在 HashMap 中添加来自 Excel Sheet 的所有值

Selenium DataProvider - How to add all the values from Excel Sheet in HashMap

我已经编写了一些测试脚本,我需要将测试数据发送到这些脚本以执行脚本。我编写了一个代码来迭代 excel 并找到给定的字符串以及该字符串所在的行号和列号。这是我希望使用的测试数据格式:

TestCase_ID || File Format || File Name || File Path || .... n
===============================================================
TC_01       || Document    || selenium.pdf || C://selenium.pdf
===============================================================

这是我正在使用的 excel 迭代代码:

  public class ExcelFileData {

    private String fileFormat; 

    private String fileName; 

    String filepath;

    public static void getCellData(String testCaseName) {

        try {

            FileInputStream file = new FileInputStream(new File("C://TestData_01.xlsx")); 

            @SuppressWarnings("resource")

            XSSFWorkbook workbook = new XSSFWorkbook(file); 

            XSSFSheet sheet = workbook.getSheetAt(0); 

            Iterator<Row> rowIterator = sheet.iterator(); 

                while (rowIterator.hasNext()) { 

                    Row row = rowIterator.next(); 

                    Iterator<Cell> cellIterator = row.cellIterator(); 

                while (cellIterator.hasNext()) { 

                    Cell cell = cellIterator.next(); 

                    if(cell.getCellType() == CellType.STRING && cell.getStringCellValue().equalsIgnoreCase(testCaseName)) {

                        System.out.println(cell.getStringCellValue());

                        System.out.println("search key  at Col: "+cell.getColumnIndex());

                        System.out.println("search key Found at Row: "+cell.getRowIndex());

                        }else {

                            break;
                        }
                    } 

                    System.out.println(""); 
                }       

            }catch (Exception e) {

                e.printStackTrace();
        }

    }

    Map<String, ExcelFileData> excelDataMap = new HashMap();

    public static void main(String args[]) {

        ExcelFileData.getCellData("TC_01");

    }

}

输出:

TC_01
search key  at Col: 0
search key Found at Row: 1

我希望找到关于给定测试用例的数据。与在 中一样,我将传递测试用例 ID(即 TC_01),然后希望迭代该特定行的所有列。我是编程新手,因此想知道如何在 HashMap 中迭代 excel 时放置所有数据,以便我可以将该数据用作我的测试脚本的输入。

正如所讨论的,我在下面为您提供了工作代码。

public class ExcelFileData {
  private String fileFormat;
  private String filePath;

  public String getFileFormat() {
    return fileFormat;
  }

  public void setFileFormat(String fileFormat) {
    this.fileFormat = fileFormat;
  }

  public String getFilePath() {
    return filePath;
  }

  public void setFilePath(String filePath) {
    this.filePath = filePath;
  }
}

测试class验证

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class TestExcelReader {
  public static void main(String[] args) throws Exception {
    FileInputStream file =
        new FileInputStream(
            new File(
                "Some Location in windows\TestData_01.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();

    Map<String, ExcelFileData> excelDataMap = new LinkedHashMap<>();

    while (rowIterator.hasNext()) {
      Row row = rowIterator.next();
      Cell testCaseCell = row.getCell(0); // Test Case Name or Test Case Id

      Cell fileFormatCell = row.getCell(1);
      Cell filePathCell = row.getCell(2);

      ExcelFileData excelFileData = new ExcelFileData();
      if (fileFormatCell != null) excelFileData.setFileFormat(fileFormatCell.getStringCellValue());
      if (filePathCell != null) excelFileData.setFilePath(filePathCell.getStringCellValue());
      if (testCaseCell != null) {
        excelDataMap.put(testCaseCell.getStringCellValue(), excelFileData);
      }
    }

    excelDataMap.forEach(
        (key, value) -> {
          System.out.println("Key as test case Id : " + key);
          System.out.println("File Format : " + value.getFileFormat());
          System.out.println("File Path : " + value.getFilePath());
        });
  }
}

我使用了 java 8 以及 Apache Poi 版本 4.1.0 和 poi-ooxml 4.1.0。