Click here to Skip to main content
15,844,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have one utility class as DataManager.java where I have written a method to read data from an excel sheet.

public static XSSFWorkbook xssfWorkbook;
public static XSSFSheet xssfSheet;

public static Object[][] readUserCredentials(String sheetName) {//return table data(2 dimensional object array)

try {
FileInputStream file = new FileInputStream(System.getProperty("user.dir") + "/TestData/Data.xlsx");
xssfWorkbook = new XSSFWorkbook(file);
xssfSheet = xssfWorkbook.getSheet(sheetName);
int totalRows = xssfSheet.getLastRowNum();
int totalCols = xssfSheet.getRow(1).getLastCellNum();

Object[][] data = new Object[xssfSheet.getLastRowNum()][xssfSheet.getRow(0).getLastCellNum()];
for (int r = 0; r < totalRows; r++) {
for (int c = 0; c < totalCols; c++) {
data[r][c] = xssfSheet.getRow(r + 1).getCell(c).toString();
 }
 }
xssfWorkbook.close();
file.close();
return data;

} catch (IOException e) {
        throw new RuntimeException(e);
}

}
My test case is in Tests package -> Login package -> LoginTest.java

@Test(dataProvider = "LoginCredentials")
public void Login(String email, String password){
LandingPage landingPage = new LandingPage();
landingPage.goToWebCentral();
LoginPage loginPage = new LoginPage();
loginPage.typeEmail(email);
loginPage.typePassword(password);

}

@DataProvider(name = "LoginCredentials")
public Object[][] getData(){
Object[][]loginData =  DataManager.readUserCredentials("Login");
return loginData;
}


What I have tried:

This gives a NullPointerException and it does not run the test case multiple times. Reading data part works properly.

How can I run the same test case with mltiple data in an excel by reading the excel, get email and password, enter credentials to login to account
Posted

1 solution

Use @BeforeMethod annotation instead of @BeforeClass as @BeforeMethod invokes multiple times before execution of each test method and @BeforeClass annotation will run only once before starting all test cases.

https://www.javatpoint.com/testng-beforemethod-annotation
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900