Search code examples
javaseleniumtestingautomated-testsdataprovider

Attempting to convert List<List<>> to Object[][]


Currently trying to convert a List> to Object[][] for the means of testing multiple tests from an excel sheet. This is the code I have at the moment.

@DataProvider(name = "Journey")
    public Object[][] generateJourneys() {
    Object[][] o = TestVariables.sourceSheet.toArray();
    return o;
    }    

The DataProvider should end up looking something like

    {
       {"TableEntry1", "TableEntry2", "TableEntry3"},
       {"TableEntry12", "TableEntry13", "TableEntry14"}
    }

I have a separate method to pull the excel file like this.

List<List<String>> sourceSheet = ExcelFileHandler.readFromFile("src/resourceSheet.xls");

So I can pull as the following code to get the first row, second column

  sourceSheet.get(0).get(1);

Solution

  • If I understood the problem, you need to convert List of List to Object[][].

    Try this

    Object[][] convert(List<List<Object>> lists) {
        Object[][] array = new Object[lists.size()][];
        for (int i = 0; i < array.length; i++) {
            array[i] = new Object[lists.get(i).size()];
            lists.get(i).toArray(array[i]);
        }
        return array;
    }