Question

I've an ExcelReader class which reads from Excel and populates the appropriate lists.

public static List<Address> addressList;
public static List<User> userList;

Above lists are being populated like this by the ExcelReader:

addressList = new ArrayList<Address>();
Address a = new Address();
a.setAddress1(r.getCell(0).getStringCellValue());
a.setCity(r.getCell(1).getStringCellValue());

I want to use this data in my Selenium test cases. I was planning to use TestNG's @DataProvider tag to feed the test case, but it only accepts Object[][] and Iterator.

Is there a way to convert these lists into an Object[][] format?

I am also open to any suggestions if you prefer to use anything other than @DataProvider.

Thanks in advance

Was it helpful?

Solution

There are lots of ways to do this, but here is an idea. I wrote an example here that does it.

The general gist of the idea is, using the MetaModel api:

public static Object[][] get2ArgArrayFromRows( List<Row> rows ) {
    Object[][] myArray = new Object[rows.size()][2];
    int i = 0;
    SelectItem[] cols = rows.get(0).getSelectItems();
    for ( Row r : rows ) {
        Object[] data = r.getValues();
        for ( int j = 0; j < cols.length; j++ ) {
            if ( data[j] == null ) data[j] = ""; // force empty string where there are NULL values
        }
        myArray[i][0] = cols;
        myArray[i][1] = data;
        i++;
    }
    logger.info( "Row count: " + rows.size() );
    logger.info( "Column names: " + Arrays.toString( cols ) );
    return myArray;
}

public static Object[][] getCsvData( File csvFile ) 
{   
    CsvConfiguration conf = new CsvConfiguration( 1 );
    DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf );
    Schema schema = csvContext.getDefaultSchema();
    Table[] tables = schema.getTables();
    Table table = tables[0]; // a representation of the csv file name including extension
    DataSet dataSet = csvContext.query()
            .from( table )
            .selectAll()
            .where("run").eq("Y")
            .execute();
    List<Row> rows = dataSet.toRows();
    Object[][] myArray = get2ArgArrayFromRows( rows );
    return myArray;
}

Now, this code above is just a ROUGH idea. What you really need to do is merge cols and data into a Map<String,String> object and then pass that as the first argument back to your test, containing all parameters from the CSV file, including browser type. Then, as the second argument, set it like so:

myArray[i][1] = new WebDriverBuilderHelper();

Then, in your @Test annotated method, instantiate the driver:

@Test(dataProvider = "dp") 
public void testIt( Map<String,String> map, WebDriverBuilderHelper wdhelper ) {
    wdhelper.instantiateBrowser( map.get("browser") );
    wdhelper.navigateTo(url);
    ....
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top