无法使用 Java/Selenium 脚本读取 excel sheet 中存在的所有数据

Unable to read all the data existing in excel sheet using Java/Selenium script

我在从 excel 文件(基本上包含测试数据形式的登录凭据)读取数据时遇到问题,尤其是当我使用 TestNG 时。当我 运行 作为普通 java 程序时,相同的代码工作正常:

Issue Details: [Utils] [ERROR] [Error]
java.lang.ArrayIndexOutOfBoundsException: 2

下面是代码:

注:
一种。我从 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(测试Class--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 抛出。

请将条件更改如下

for (int i = 0; i < rowCount; i++) 

例如,如果excel只有三行两列,那么数据对象将被创建为Object[][] data = new Object[3][2];。它最多可以容纳 3 行。但是第一个 for 循环将从索引 0 到 3(总共 4 行)执行。当它试图插入第 4 行记录时,然后 ArrayIndexOutOfBoundsException 正在抛出。

修改后的代码:

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();
}