简体   繁体   English

使用TestNG数据提供程序从多个表中获取数据

[英]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. 我使用Selenium RC和TestNG进行某种关键字驱动的测试。出于同样的目的,我想从Excel工作表中读取一些数据并使用它。在这里,我使用了TestNG的数据提供程序注释,但是我的问题是我我想从多个表中读取数据并仅使用一个数据提供程序以一种测试方法使用它们。但是我遇到了麻烦。有人可以这样做吗?

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.. 好吧,这就是getTableArray的样子。

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)

如果您不包括堆栈跟踪以及getTableArray()返回的内容(应该为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. 如果在执行代码时打开Excel工作表,则会发现此错误。 Please make sure you save and close excel doc before executing test cases. 在执行测试用例之前,请确保您保存并关闭excel文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM