【发布时间】: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