【问题标题】:Unable to read all the data existing in excel sheet using Java/Selenium script无法使用 Java/Selenium 脚本读取 excel 工作表中存在的所有数据
【发布时间】:2019-01-23 03:54:07
【问题描述】:

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

问题详情:[Utils] [ERROR] [Error]
java.lang.ArrayIndexOutOfBoundsException: 2

下面是代码:

注意:
一种。我从 excel 文件中读取数据,然后通过 DataProvider 注释将其传递给测试方法。 湾。 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(Test 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();

}

}

【问题讨论】:

    标签: selenium testng indexoutofboundsexception data-driven-tests


    【解决方案1】:

    您已经使用 rowCount 和 colCount 创建了数据对象。

    Object[][] data = new Object[rowCount][colCount];
    

    但是在for循环中添加了for (int i = 0; i &lt;= 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();
    }
    

    【讨论】:

    • 您好,我根据您的建议进行了更改,发现 ArrayIndexOutOfBounds 异常问题已解决。但是,代码不会获取最后一行中存在的数据。请注意,excel 数据有 3 行和 2 列,代码可以从中获取前两行,而最后一行没有被获取。征求您对此的建议。谢谢, Sandesh.S
    • 您在 Excel 中总共有多少行,包括标题?可以分享rowCount 的值吗?
    • @Sandesh:请检查编辑后的答案。它会解决你的两个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2023-03-23
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多