eclipse TestNG中的DataProvider从excel传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14386937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataProvider in TestNG to pass data from excel
提问by Anji R
I started learning Selenium2 (WebDriver) with Eclipse and TestNG. I have a question about DataProvider. I have a login page having user, password and login button for example. I have written a test in TestNG. I have used pageobject for UI objects (have separate class) and actual test in another class.
我开始使用 Eclipse 和 TestNG 学习 Selenium2 (WebDriver)。我有一个关于 DataProvider 的问题。例如,我有一个包含用户、密码和登录按钮的登录页面。我已经在 TestNG 中编写了一个测试。我已经将 pageobject 用于 UI 对象(具有单独的类)并在另一个类中进行实际测试。
Here glogin is a class and login is the function where finding elements and sending keys is done and this is called in another class gtest(which is main test) which has TestNG annotations.
这里 glogin 是一个类,login 是查找元素和发送密钥的函数,它在另一个具有 TestNG 注释的类 gtest(主测试)中调用。
I access that class in main script which will take values.
我在主脚本中访问该类,该类将采用值。
@test(Dataprovide = "test")
public void glogin(String user, String pass)
{
glogin log1 = new login;
log1.login(user,pass);
}
I have the following excel sheet
我有以下excel表
user pass
John Smith
Carson Black
Carla ck23
test test4
When I use dataprovider and get data from the excel sheet as array and use it in Test then the following error is displayed:
当我使用 dataprovider 并从 Excel 工作表中获取数据作为数组并在测试中使用它时,会显示以下错误:
org.testng.TestNGException:
The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2
at org.testng.internal.Invoker.injectParameters(Invoker.java:1337)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1225)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:128)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.run(TestNG.java:1036)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Any help is really appreciated.
任何帮助都非常感谢。
Here is code for method annotated with Dataprovider and
这是使用 Dataprovider 注释的方法的代码和
@DataProvider(name="test")
public Object[][] createdata1()throws Exception
{
Object[][] retobj = getexcel();
return retobj;
}
private String[][] getexcel() throws Exception
{
// TODO Auto-generated method stub
String[][] tabarray = null;
try {
Workbook wb1 = Workbook.getWorkbook(new
File("F:/testdata.xls"));
Sheet sheet = wb1.getSheet("userlogin");
Cell tablestart = sheet.findCell("login");
int startrow = tablestart.getRow();
int startcol = tablestart.getColumn();
Cell tableend = sheet.findCell("login",startcol+1,startrow+1,
100, 64000, false);
int endrow = tableend.getRow();
int endcol = tableend.getColumn();
System.out.println("startRow="+startrow+", endRow="+endrow+",
" + "startCol="+startcol+", endCol="+endcol);
tabarray = new String[endrow - startrow + 1][endcol -
startcol + 1];
int ci = 0;
for(int i = startrow +1 ;i<endrow;i++,ci++)
{
int cj = 0;
for(int j = startcol + 1;j<endcol;j++,cj++)
{
tabarray[ci][cj] = sheet.getCell(j,
i).getContents();
System.out.println(tabarray[ci][cj]);
}
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tabarray;
}
test(Dataprovider = "test")
public void glogins(String user, String pass)
{
glogin log1 = new glogin(driver);
log1.login(user,pass);
}
When I executed the test, I received data from excel as
当我执行测试时,我收到了来自 excel 的数据
john
smith
carson
Black
carla
ck23
test
test4
as output
作为输出
回答by Cedric Beust
Isn't the error message self-explanatory?
错误消息不是不言自明的吗?
The data provider is trying to pass 4 parameters but the method plus.gmail#glogin takes 2
Use a debugger and figure out why your data provider is returning 4 parameters instead of just 2.
使用调试器找出为什么您的数据提供程序返回 4 个参数而不是 2 个。
回答by KalyanMuthiah
Use
用
tabarray = new String[endrow - 1][endcol - 1]; instead of //tabarray = new String[endrow - startrow + 1][endcol - startcol + 1];
Because you intended to return only 2*2 array
因为你打算只返回 2*2 数组
回答by balu
Replace the line
更换线路
tabarray = new String[endrow - startrow + 1][endcol - startcol + 1];
with
和
tabarray = new String[endrow - startrow - 1][endcol - startcol - 1];
回答by Anoop
I agree with the response posted by Kalyan; the reason being the first and last column of the data provider excel sheet is also counted in as arguments / parameters by the function; therefore please use tabArray=new String[endRow-startRow-1][endCol-startCol-1];
Hope this helps and happy testing...
我同意 Kalyan 发布的回复;原因是数据提供者 excel 表的第一列和最后一列也被函数计为参数/参数;因此请使用tabArray=new String[endRow-startRow-1][endCol-startCol-1];
希望这会有所帮助并祝您测试愉快...