Read data from Excel sheet(java)
I have created to read data from excel sheet to my login web page(username and password) using java for automation.So I want to know why is this code not running as well in eclipse?
I have used TestNg framework and tried to use dataprovider . It shows this error “java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap”
1.Dataprovidertest class
private String filePath = "C:\Users\samudil\eclipse-workspace\ZharaProject\src\Excelsheet\TestData.xlsx";
private String sheetName = "Sheet1";
@Test(dataProvider = "excelData")
public void read(String username, String password) throws InterruptedException {
//handle popup window
Set<String> windowId = driver.getWindowHandles(); // get window id of current window
Iterator<String> itererator = windowId.iterator();
String mainWinID = itererator.next();
String newAdwinID = itererator.next();
driver.switchTo().window(newAdwinID);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.findElement(By.xpath("//input[@id='j_username']")).sendKeys(username);
driver.findElement(By.xpath("//*[@id="j_password"]")).sendKeys(password);
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
}
@DataProvider(name="excelData")
public Object[][] readExcel() throws InvalidFormatException, IOException {
return TestUtil.readExcel(filePath, sheetName);
}
2.TestUtil class
public class TestUtil {
public static Object[][] readExcel(String filePath, String sheetName) throws InvalidFormatException, IOException {
FileInputStream file= new FileInputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum();
int column = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][column];
for (int i = 1; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < column; j++) {
XSSFCell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(cell);
data[i - 1][j] = val;
}
}
return data;
}
}
2
Answers
The exception is very clear. This says the class definition for the class you have used is not found. This is a generic exception and can come any time if you miss the dependencies.
Please add
if you are using maven or simply add the jar file otherwise.
You can get the jar from here: Maven Link
Assuming you have already added all the required dependencies (As per your comment).
Please try out the below steps, one of these may help.
If you still face this issue after performing these steps, you might have a different issue.