我在从 Excel 文件(基本上包含测试数据形式的登录凭据)读取数据时遇到问题,特别是当我使用 TestNG
时。当我将其作为普通 java
程序运行时,相同的代码可以正常工作:
Issue Details: [Utils] [ERROR] [Error]
java.lang.ArrayIndexOutOfBoundsException: 2
下面是代码:
注意:
A。我从 Excel 文件中读取数据,然后通过 DataProvider 注释将其传递到测试方法。
b. Excel 包含两列,即用户名和密码
FetchData.java:
enter code here
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class FetchData {
public Object[][] getData() throws EncryptedDocumentException,
InvalidFormatException, IOException {
FileInputStream fis = new FileInputStream(
"E:\\test.xlsx");
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheetAt(0);
int rowCount = sh.getLastRowNum();
int colCount = sh.getRow(0).getLastCellNum();
System.out.println("Row Count: " + rowCount);
System.out.println("Column Count: " + colCount);
Object[][] data = new Object[rowCount][colCount];
for (int i = 0; i <= rowCount; i++) {
for (int j = 0; j < colCount; j++) {
data[i][j] = sh.getRow(i).getCell(j).getStringCellValue();
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
return data;
}
}
LoginTests.java(测试类--TestNG):
import java.io.IOException;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTests {
static WebDriver driver;
ChromeOptions options;
FetchData fd;
@BeforeMethod
public void setUp() {
}
@DataProvider
public Object[][] testData() throws EncryptedDocumentException,
InvalidFormatException, IOException {
fd = new FetchData();
Object[][] data = fd.getData();
return data;
}
@Test(dataProvider = "testData")
public void test(String username, String password) {
System.setProperty("webdriver.chrome.driver",
"E:\\chromedriver.exe");
options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("https://www.facebook.com");
WebElement uname = driver.findElement(By
.xpath("//input[@type='email']"));
WebElement pwd = driver.findElement(By
.xpath("(//input[@type='password'])[1]"));
WebElement submit = driver.findElement(By
.xpath("//input[@value='Log In']"));
uname.sendKeys(username);
pwd.sendKeys(password);
submit.click();
}
}
最佳答案
您已使用 rowCount 和 colCount 创建了数据对象。
Object[][] data = new Object[rowCount][colCount];
但是在for循环中添加了像for (int i = 0; i <= rowCount; i++)
这样的条件因此,这个循环将尝试再执行一次,因此 ArrayIndexOutOfBoundsException
正在 throw 。
请更改以下条件
for (int i = 0; i < rowCount; i++)
例如,如果 Excel 只有 3 行 2 列,则数据对象将创建为 Object[][] data = new Object[3][2];
。它最多可容纳 3 行。但第一个for
循环将从索引0到3(总共4行)执行。当它尝试插入第 4 行记录时,则 ArrayIndexOutOfBoundsException
正在 throw 。
修改后的代码:
Object[][] data = new Object[rowCount][colCount];
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
data[i][j] = sh.getRow(i).getCell(j).getStringCellValue();
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
编辑:
getLastRowNum()
方法是从0开始的。 Refer the Doc 。因此,您需要如下修改代码,以便获取没有 ArrayIndexOutOfBoundsException
的所有行数据。
修改后的代码:
//Rowcount+1 needs to be added inorder to read the last row
Object[][] data = new Object[rowCount+1][colCount];
for (int i = 0; i <= rowCount; i++) {
for (int j = 0; j < colCount; j++) {
data[i][j] = sh.getRow(i).getCell(j).getStringCellValue();
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
关于selenium - 无法使用 Java/Selenium 脚本读取 Excel 工作表中存在的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51872792/