如何用Java Selenium Cucumber实现Excel按指定key查询对应测试值
实现方案
1. 引入Excel读取依赖
Java项目推荐用Apache POI处理Excel,Maven项目在pom.xml中添加如下依赖即可:
<!-- 处理xlsx格式文件 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> <scope>test</scope> </dependency>
如果是Gradle项目,对应调整依赖引入格式即可
2. 封装Excel测试数据工具类
核心逻辑是预加载全量测试数据到内存,用双层Map存储避免重复IO,工具类示例逻辑如下:
public class ExcelTestDataUtil { // 外层key:JIRA单号,内层key:测试数据key,value:对应测试值 private static Map<String, Map<String, String>> testDataCache = new HashMap<>(); // 存储当前执行用例的JIRA单号,ThreadLocal避免多线程执行串数据 private static ThreadLocal<String> currentJiraId = new ThreadLocal<>(); // 测试套件启动时调用一次,加载全量测试数据 public static void loadTestData(String excelPath, String sheetName) throws IOException { try (FileInputStream fis = new FileInputStream(excelPath); XSSFWorkbook workbook = new XSSFWorkbook(fis)) { XSSFSheet sheet = workbook.getSheet(sheetName); // 第一行是表头,存所有测试数据的key XSSFRow headerRow = sheet.getRow(0); int keyCount = headerRow.getLastCellNum(); List<String> keyList = new ArrayList<>(); for (int i = 0; i < keyCount; i++) { keyList.add(headerRow.getCell(i).getStringCellValue().trim()); } // 从第二行开始读每一条用例数据 for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) { XSSFRow dataRow = sheet.getRow(rowNum); String jiraId = dataRow.getCell(keyList.indexOf("JIRA单号")).getStringCellValue().trim(); Map<String, String> rowData = new HashMap<>(); for (int i = 0; i < keyCount; i++) { rowData.put(keyList.get(i), dataRow.getCell(i).getStringCellValue().trim()); } testDataCache.put(jiraId, rowData); } } } // 绑定当前用例的JIRA单号 public static void setCurrentJiraId(String jiraId) { currentJiraId.set(jiraId); } // 对外暴露的查询方法,传入key直接返回对应值 public static String getTestData(String key) { String jiraId = currentJiraId.get(); if (!testDataCache.containsKey(jiraId)) { throw new RuntimeException("未找到JIRA单号为" + jiraId + "的测试数据"); } String value = testDataCache.get(jiraId).get(key); if (value == null) { throw new RuntimeException("JIRA单号" + jiraId + "下未找到key为" + key + "的测试数据"); } return value; } }
3. Cucumber 上下文绑定当前用例JIRA单号
有两种常用绑定方式,选一种即可:
- 方式1:把JIRA单号放在Scenario标签上,比如
@JIRA-XXX-1234,在@Before钩子中解析标签绑定:
@Before public void beforeScenario(Scenario scenario) { // 解析标签获取JIRA单号 Optional<String> jiraTag = scenario.getSourceTagNames().stream().filter(tag -> tag.startsWith("@JIRA-")).findFirst(); jiraTag.ifPresent(tag -> ExcelTestDataUtil.setCurrentJiraId(tag.replace("@", ""))); }
- 方式2:把JIRA单号作为Step入参传入,Step定义如下:
Given("测试用例对应的JIRA单号为{string}", (String jiraId) -> { ExcelTestDataUtil.setCurrentJiraId(jiraId); });
4. Step Definition中调用获取测试数据
需要获取测试值时直接调用工具类的查询方法即可,示例:
When("用户输入账号密码", () -> { // 传入key直接获取对应测试值 String userName = ExcelTestDataUtil.getTestData("user3"); String password = ExcelTestDataUtil.getTestData("Password556"); // 传入Selenium元素做输入 driver.findElement(By.id("username")).sendKeys(userName); driver.findElement(By.id("password")).sendKeys(password); });
注意事项
- 测试数据修改后需要重新触发
loadTestData方法才会生效,调试阶段可以加个参数控制是否每次查询都重读Excel - 要处理非字符串类型的测试数据,比如数字、日期,对应调整工具类中单元格值的读取逻辑即可
- 测试套件执行前要先触发
loadTestData方法,可以放在Cucumber的@BeforeAll钩子或者测试套件的初始化逻辑中
内容的提问来源于stack exchange,提问作者Naidu
相关产品推荐
相关产品推荐

