gpt4 book ai didi

java - 如何通过 DataProvider 使用 List 对象中的 Excel 数据?

转载 作者:行者123 更新时间:2023-12-02 06:15:34 26 4
gpt4 key购买 nike

我正在使用 Excel 驱动程序从电子表格中获取用户 ID 和密码。我正在创建 Excel Driver 对象并使用 Excel Driver 类的方法来获取数据并将其存储为 Map<String,Object>> testData .

这是在我的 DataProvider 中实现的方法。我在将值传递给我的主要测试方法时遇到问题。

我能够一次获取 1 行的值并传递到主测试方法,但我希望能够传递所有 3 行用户凭据。

// This returns a List with a map that represents every row in spreadsheet

List<Map<String, Object>> testData = excelDriver.getData();


// The following code works to get data for the first row, but I need to understand how to
// return all UserIDs and Passwords (3 rows total) for use in the main Test.

@DataProvider(name="dataProvider")
public Object [] dataProvider() throws IOException {
Map<String, Object> nmap = testData.get(1);
Object userID = nmap.get("USER_ID"); // user ID column header
Object password = nmap.get("Password"); // password column header
return new Object [] {{userID, password}};
}

最佳答案

在您的代码中,每一行都表示为一个对象数组,因此您必须返回 List <Object[]>获取所有行。

@DataProvider(name="dataProvider")
public List<Object[]> dataProvider() {
List<Map<String, Object>> testData = excelDriver.getData();
return testData.stream().map( row -> dataMapper(row)).collect(Collectors.toList());
}

private Object [] dataMapper(Map<String, Object> nmap) {
Object userID = nmap.get("USER_ID"); // user ID column header
Object password = nmap.get("Password"); // password column header
return new Object [] {userID, password};
}

关于java - 如何通过 DataProvider 使用 List<map> 对象中的 Excel 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55874632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com