我试图在eclipse中运行此代码作为maven项目,但当我试图将测试运行为TestNG时,我遇到了错误。下面是我的所有代码:
实效(包)ExcelReader . java
package util;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
//Global Variables
public String path;
public FileInputStream fis = null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row = null;
private XSSFCell cell = null;
// Constructor to initialize variables
public ExcelReader(String path) {
this.path = path;
try {
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to call the value
public String getCellData(String sheetName, String colName, int rowNum) {
// For Sheet
int index = workbook.getSheetIndex(sheetName);
int col_Num = 0;
sheet = workbook.getSheetAt(index);
// For Row
row = sheet.getRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim())) {
col_Num = i;
}
}
// For Column
row = sheet.getRow(rowNum - 1);
cell = row.getCell(col_Num);
return cell.getStringCellValue();
}
}
src/测试AddCustomerTest
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import page.AddCustomersPage;
import page.DashboardPage;
import page.LoginPage;
import util.BrowserFactory;
import util.ExcelReader;
public class AddCustomerTest {
WebDriver driver;
ExcelReader exlread = new ExcelReader("TestData\Test.xlsx");
String userName = exlread.getCellData("LoginInfo", "UserName", 2);
String password = exlread.getCellData("LoginInfo", "Password", 2);
String fullname = exlread.getCellData("AddContactInfo", "FullName", 2);
String companyName = exlread.getCellData("AddContactInfo", "CompanyName", 2);
String email = exlread.getCellData("AddContactInfo", "Email", 2);
String phoneNum = exlread.getCellData("AddContactInfo", "Phone", 2);
String addresss = exlread.getCellData("AddContactInfo", "Address", 2);
String country = exlread.getCellData("AddContactInfo", "Country", 2);
String city = exlread.getCellData("AddContactInfo", "City", 2);
String state = exlread.getCellData("AddContactInfo", "State", 2);
String zip = exlread.getCellData("AddContactInfo", "Zip", 2);
@Test
public void validUserShouldBeAbleToAddCustomer() {
driver = BrowserFactory.init();
LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);
loginpage.enterUserName("demo@techfios.com");
loginpage.enterPassword("abc123");
loginpage.clickSignInButton();
DashboardPage dashboardpage = PageFactory.initElements(driver, DashboardPage.class);
dashboardpage.validateDashboardPage();
AddCustomersPage addcustomer = PageFactory.initElements(driver, AddCustomersPage.class);
addcustomer.clickOnCustomerButton();
addcustomer.clickOnADDCustomerButton();
addcustomer.enterFullName(fullname);
addcustomer.enterCompanyName("companyName");
addcustomer.enterEmail("email");
addcustomer.EnterPhoneNumber("phoneNum");
addcustomer.EnterAddress("addresss");
addcustomer.EnterCountry(country);
addcustomer.EnterCity("city");
addcustomer.EnterCountry(state);
addcustomer.EnterCountry(zip);
addcustomer.clickSaveButton();
addcustomer.clickListCustomerButton();
// BrowserFactory.tearDown();
}
}
误差[RemoteTestNG] detected TestNG version 7.3.0
org.testng.TestNGException:
Cannot instantiate class test.AddCustomerTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193)
at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113)
at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:109)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:167)
at org.testng.TestClass.getInstances(TestClass.java:102)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82)
at org.testng.TestClass.init(TestClass.java:74)
at org.testng.TestClass.<init>(TestClass.java:39)
at org.testng.TestRunner.initMethods(TestRunner.java:457)
at org.testng.TestRunner.init(TestRunner.java:336)
at org.testng.TestRunner.init(TestRunner.java:289)
at org.testng.TestRunner.<init>(TestRunner.java:220)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676)
at org.testng.SuiteRunner.init(SuiteRunner.java:178)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:112)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1306)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1282)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1131)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 27 more
Caused by: java.lang.IllegalArgumentException: Sheet index (-1) is out of range (0..1)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.validateSheetIndex(XSSFWorkbook.java:1453)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:1129)
at util.ExcelReader.getCellData(ExcelReader.java:38)
at test.AddCustomerTest.<init>(AddCustomerTest.java:18)
... 32 more
答案如下:
Caused by: java.lang.IllegalArgumentException: Sheet index (-1) is out of range (0..1)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.validateSheetIndex(XSSFWorkbook.java:1453)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:1129)
at util.ExcelReader.getCellData(ExcelReader.java:38)
at test.AddCustomerTest.<init>(AddCustomerTest.java:18)
... 32 more
这意味着当你的类在AddCustomerTest.java:18
行被静态初始化时。它进入你的ExcelReader
类,试图通过其名称获取表的索引但它返回-1
这意味着(如果你看文档)它无法找到给定名称为LoginInfo
的表.