我正在使用Eclipse IDE,java和selenium webdriver。 我有一个webform,我想每次使用唯一值循环多次。值将来自.xlsx文件。对于第一次迭代,值将来自第1列,第1行。第2次迭代,值将来自第1列,第2行等。 我的问题是 - 是否可以编写代码以允许每次都使用下一个值?
以下是我尝试过的 - 创建一个数组来保存值的位置。
for (int Count = 5; Count <=10; Count++) {
Utility.ExcelUtils.setExcelFile(Utility.Constant.Path_TestData + Utility.Constant.File_SellerNetwork30,"Sheet1");
String[] sStoreNumbers = {Utility.ExcelUtils.getCellData};
sStoreNumbers[0] = Utility.ExcelUtils.getCellData(1, 1);
sStoreNumbers[1] = Utility.ExcelUtils.getCellData(2, 1);
sStoreNumbers[2] = Utility.ExcelUtils.getCellData(3, 1);
SellerNetworkSetupPage.txt_StoreNum(driver).sendKeys(sStoreNumbers);
SellerNetworkSetupPage.btn_Add(driver).click();
if (Count ==10) {
break;}
这仅适用于第一个。第二个和第三个字符串抛出一个ArrayIndexOutOfBoundsException:
java.lang.ArrayIndexOutOfBoundsException: 1 at StoreOptionsPage.StoreOptions_SellerNetworkCreate30TEST.mymaintest(StoreOptio ns_SellerNetworkCreate30TEST.java:94) 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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我正在寻找有关如何解决/改善此问题的建议。非常感谢任何帮助。
答案 0 :(得分:1)
您正在获取索引超出绑定的异常,因为数组sStoreNumbers仅填充了一个元素,因此数组的长度为1,并且元素的索引位置为0.您尝试将数据存储在索引位置1和2超出范围。您应该按如下方式修改代码:
for (int Count = 5; Count <=10; Count++) {
Utility.ExcelUtils.setExcelFile(Utility.Constant.Path_TestData + Utility.Constant.File_SellerNetwork30,"Sheet1");
String[] sStoreNumbers = new String[3];// modification to code is here.
sStoreNumbers[0] = Utility.ExcelUtils.getCellData(1, 1);
sStoreNumbers[1] = Utility.ExcelUtils.getCellData(2, 1);
sStoreNumbers[2] = Utility.ExcelUtils.getCellData(3, 1);
SellerNetworkSetupPage.txt_StoreNum(driver).sendKeys(sStoreNumbers);
SellerNetworkSetupPage.btn_Add(driver).click();
if (Count ==10) {
break;}