开发者

Taking Data From Multiple Table using TestNG Data Provider

I am Using Selenium RC with TestNG to do some kind of keyword driven testing.For the same purpose i want to read some data from an excel sheet and use it.Here I have used Data Provider annotation of TestNG.but My problem is that i want to read data from multiple tables and use them in a single test method using only one data provider.But I am Getting Problem in it.Can somebody Help me in Doing so.

Thanks

Here Is my Code:

@DataProvider(name = "DP1")

public Object[][] createData1() throws Exception {

  return new Object[][] {

    {getTableData.getTableArray(" Xls File Path", "Sheet name", "Table1")},

    {getTableData.getTableArray(" Xls File Path", "Sheet name", "Table2")}

  };

}

This Is My Test Method:

@Test (dataProvider = ("DP1"))

public void testallpivot(String Command, String Target, String Value) throws Exception {

  //Test Code here


}

But This Code is Showing Array Index Out of Bound Exception.. Somebody Please Help me.....

Well this is how getTableArray looks..

public static Object[][] getTableArray(String xlFilePath, String sheetName, String     tableName) throws Exception{
    Object[][] tabArray;

        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName); 
        int startRow,startCol, endRow, endCol,ci,cj;
        Cell tableStart=sheet.findCell(tableName);
        //System.out.println(tableName);
        startRow=tableStart.getRow();
        startCol=tableStart.getColumn();

        Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

        endRow=tableEnd.getRow();
        endCol=tableEnd.getColumn();
开发者_运维问答        System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                "startCol="+startCol+", endCol="+endCol);
        tabArray=new String[endRow-startRow-1][endCol-startCol-1];
        ci=0;

        for (int i=startRow+1;i<endRow;i++,ci++){
            cj=0;
            for (int j=startCol+1;j<endCol;j++,cj++){
                tabArray[ci][cj]=sheet.getCell(j,i).getContents();
            }
        }


    return(tabArray);
}

And this is the Stack Trace.....

java.lang.ArrayIndexOutOfBoundsException: 1
at org.testng.internal.Invoker.injectParameters(Invoker.java:1144)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1020)
atorg.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:137)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:121)
at org.testng.TestRunner.runWorkers(TestRunner.java:953)
at org.testng.TestRunner.privateRun(TestRunner.java:633)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:316)
at org.testng.SuiteRunner.run(SuiteRunner.java:195)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:903)
at org.testng.TestNG.runSuitesLocally(TestNG.java:872)
at org.testng.TestNG.run(TestNG.java:780)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:75)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:127)


We can't help you much if you don't include the stack trace and also what getTableArray() returns (it should be Object[]).


you can return only one array by copying the second in to first. Try something like:

List<Object[]>  retVal = Arrays.asList
    (getTableData.getTableArray(" Xls File Path", "Sheet name", "Table1"));
retVal.addAll(
    Arrays.asList(getTableData.getTableArray(" Xls File Path", "Sheet name", "Table2")));
return retVal.toArray();


If you open the Excel sheet while executing code then this error is found. Please make sure you save and close excel doc before executing test cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜