使用 TestNG 数据提供程序从多个表获取数据
我使用 Selenium RC 和 TestNG 来做某种关键字驱动的测试。出于同样的目的,我想从 Excel 工作表中读取一些数据并使用它。这里我使用了 TestNG 的数据提供程序注释。但我的问题是我想要从多个表中读取数据并仅使用一个数据提供程序在单个测试方法中使用它们。但是我遇到了问题。有人可以帮助我这样做吗?
谢谢
这是我的代码:
@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")}
};
}
这是我的测试方法:
@Test (dataProvider = ("DP1"))
public void testallpivot(String Command, String Target, String Value) throws Exception {
//Test Code here
}
但是此代码显示数组索引越界异常.. 有人请帮助我......
这就是 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);
}
这是堆栈跟踪......
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不包含堆栈跟踪以及 getTableArray() 返回的内容(应该是 Object[]),我们无法为您提供太多帮助。
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:
如果您在执行代码时打开 Excel 工作表,则会发现此错误。请确保在执行测试用例之前保存并关闭 Excel 文档。
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.