下面是我简单的Selenium TestNG程序,用于从excel表的第二行中提取姓氏,名字和DOB,并在数据提供者的帮助下在@Test批注中显示相同的名字。使用@BeforeTest批注在@Test之前打开浏览器。
@Test(dataProvider="getData")
public void processExcel(String lastName, String firstName, String DOB) throws InterruptedException
{
System.out.println(lastName);
}
@DataProvider
public Object[][] getData()
{
int colCount = 3;
int rowNum = 1;
String sheetName = "Excel_Sheet";
int index = workbook.getSheetIndex(sheetName);
Object[][] data= new Object[rowNum][colCount];
for(int i = 1;i<colCount;i++)
{
sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum);
cell = row.getCell(i);
if(cell.getCellType()==Cell.CELL_TYPE_STRING)
data[rowNum][i]= cell.getStringCellValue();
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)
data[rowNum][i]= String.valueOf(cell.getNumericCellValue());
else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
data[rowNum][i]= "";
//data[rowNum][i]=excel.getCellData(sheetName,rowNum,i);
}
return data;
}
但是我遇到了以下异常,并相信它与在数据提供程序中使用Array有关,尝试了不同的想法,但仍然使我受阻,因此感谢任何帮助。谢谢
[Utils] [ERROR] [Error] java.lang.ArrayIndexOutOfBoundsException: 1
at com.hie.HH_Regression.ADTA01_HH.getData(ADTA01_HH.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:924)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)