首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Selenium DataProvider -如何在HashMap中添加Excel表格中的所有值

Selenium DataProvider -如何在HashMap中添加Excel表格中的所有值
EN

Stack Overflow用户
提问于 2019-05-22 01:23:33
回答 1查看 397关注 0票数 -1

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

代码语言:javascript
复制
TestCase_ID || File Format || File Name || File Path || .... n
===============================================================
TC_01       || Document    || selenium.pdf || C://selenium.pdf
===============================================================

下面是我使用的excel迭代代码:

代码语言:javascript
复制
  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");

    }

}

输出:

代码语言:javascript
复制
TC_01
search key  at Col: 0
search key Found at Row: 1

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-22 01:38:00

如前所述,我将在下面为您提供工作代码。

代码语言:javascript
复制
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;
  }
}

要验证的测试类

代码语言:javascript
复制
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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56243493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档