I am using an Excel driver to get UserIDs and Passwords from my spreadsheet. I am creating the Excel Driver object and using a method of the Excel Driver class to get the data and store it as Map<String,Object>> testData.

This is implemented in my DataProvider method. I am having trouble passing the values to my main Test method.

I am able to get values for 1 row at a time and pass to the main test method, but I would like to be able to pass all 3 rows of user credentials.

// 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}};
}

1 Answers

0
Andres Martínez On

On your code, each row is represented as an array of objects, so you have to return a List <Object[]> to get all the rows.

@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};
}