使用Excel数据驱动多用户登录时遇No Class Found错误求助
问题描述
尝试通过Excel表格作为数据提供者实现应用多用户登录时,出现No class found错误。
环境信息
- Selenium版本:selenium-server-standalone-3.41.59.jar
- TestNG版本:7.4.0
- 已添加Apache POI相关Excel依赖及对应库(依赖截图如下):

相关代码
MultiUserLogin类
package ui; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class MultiUserLogin { public static WebDriver driver; public WebDriverWait wait; String appURL1 = "https://www.google.com"; @BeforeClass public void testSetup() { System.setProperty("webdriver.gecko.driver","E:/Geckdriver/geckodriver.exe"); driver=new FirefoxDriver(); driver.manage().window().maximize(); wait = new WebDriverWait(driver, 5); } @Test(dataProvider="LoginData") public void VerifyValidLogin(String Username, String Password,String Company) { driver.navigate().to(appURL1); driver.findElement(By.id("userId")).sendKeys(Username); driver.findElement(By.id("password")).sendKeys(Password); driver.findElement(By.id("company")).sendKeys(Company); driver.findElement(By.linkText("Login")).click(); } @DataProvider(name="LoginData") public Object[][] LoginData() throws Exception { Object[][] testObjArray = ExcelUtils.getTableArray("E:\\TestNG\\TestData\\TestData.xlsx","Sheet1"); return (testObjArray); } }
ExcelUtils类
package ui; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.poi.ss.usermodel.CellType; 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 ExcelUtils { private static XSSFSheet ExcelWSheet; private static XSSFWorkbook ExcelWBook; private static XSSFCell Cell; private static XSSFRow Row; public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception { String[][] tabArray = null; try { FileInputStream ExcelFile = new FileInputStream(FilePath); // Access the required test data sheet ExcelWBook = new XSSFWorkbook(ExcelFile); ExcelWSheet = ExcelWBook.getSheet(SheetName); int startRow = 1; int startCol = 1; int ci,cj; int totalRows = ExcelWSheet.getLastRowNum(); // function as well to get Column count int totalCols = 2; tabArray=new String[totalRows][totalCols]; ci=0; for (int i=startRow;i<=totalRows;i++, ci++) { cj=0; for (int j=startCol;j<=totalCols;j++, cj++){ tabArray[ci][cj]=getCellData(i,j); System.out.println(tabArray[ci][cj]); } } } catch (FileNotFoundException e){ System.out.println("Could not read the Excel sheet"); e.printStackTrace(); } catch (IOException e){ System.out.println("Could not read the Excel sheet"); e.printStackTrace(); } return(tabArray); } public static String getCellData(int RowNum, int ColNum) throws Exception { try{ Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); CellType dataType= Cell.getCellType(); if (dataType == CellType.STRING) { return ""; }else{ String CellData = Cell.getStringCellValue(); return CellData; } }catch (Exception e){ System.out.println(e.getMessage()); throw (e); } } }
错误解决建议
1. 检查依赖完整性与类路径
No class found核心原因是依赖缺失或类未被正确加载:
- 确认所有Apache POI相关依赖(
poi.jar、poi-ooxml.jar、poi-ooxml-schemas.jar、commons-codec.jar、commons-collections4.jar、commons-io.jar、curvesapi.jar、xmlbeans.jar)都已正确添加到项目类路径,且版本与Selenium、TestNG兼容。 - 若使用Maven/Gradle,直接通过依赖管理工具引入POI,避免手动加包的版本冲突问题:
Maven依赖示例:<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
2. 修正文件路径转义
Java中文件路径的反斜杠需要转义,原代码中的"E:\TestNG\TestData\TestData.xlsx"会导致解析错误,改为"E:\\TestNG\\TestData\\TestData.xlsx"。
3. 清理编译缓存并验证包结构
- 执行IDE的Clean + Build操作,清除编译缓存,确保
ExcelUtils类的字节码文件被正确生成。 - 确认
ExcelUtils类和getTableArray方法均为public修饰,且与MultiUserLogin类在同一个ui包下,保证调用权限正常。
4. 修复ExcelUtils的单元格读取逻辑
原getCellData方法逻辑颠倒,会导致类型转换异常,修正后代码:
public static String getCellData(int RowNum, int ColNum) throws Exception { try{ Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum); if (Cell == null) { return ""; } CellType dataType = Cell.getCellType(); if (dataType == CellType.STRING) { return Cell.getStringCellValue(); } else if (dataType == CellType.NUMERIC) { return String.valueOf(Cell.getNumericCellValue()); } else if (dataType == CellType.BOOLEAN) { return String.valueOf(Cell.getBooleanCellValue()); } else { return ""; } }catch (Exception e){ System.out.println(e.getMessage()); throw e; } }
5. 匹配数据提供者与测试方法的参数数量
VerifyValidLogin方法接收3个参数,但原ExcelUtils中totalCols设为2,返回的数组列数不匹配,将totalCols改为3:
int totalCols = 3; tabArray=new String[totalRows][totalCols];
内容的提问来源于stack exchange,提问作者vinay pv
相关产品推荐
相关产品推荐

