While planning a test, parameterizing the tests is unavoidable. We will utilize Apache POI - Excel JAR's to accomplish the equivalent. It encourages us peruse and compose into Excel.
Download JAR
Stage 1 − Navigate to the URL - https://poi.apache.org/download.html and download the ZIP design.
Stage 2 − Click on the Mirror Link to download the JAR's.
Stage 3 − Unzip the substance to an envelope.
Stage 4 − Unzipped substance would be shown as demonstrated as follows.
Stage 5 − Now make another venture and include all the 'Outside JARs' under 'poi-3.10.FINAL' envelope.
Stage 6 − Now include all the 'Outside JARs' under the 'ooxml-lib' envelope.
Stage 7 − Now include all the 'Outside JARs' under the 'lib' envelope.
Stage 8 − The Added JAR is shown as demonstrated as follows.
Stage 9 − The Package Explorer is shown as demonstrated as follows. Aside from that, include 'WebDriver' related JAR's
Parameterization
For show, we will parameterize the percent number cruncher test.
Stage 1 − We will parameterize all the information sources required for percent number cruncher utilizing Excel. The planned Excel is demonstrated as follows.
Stage 2 − Execute all the percent number cruncher capacities for all the predefined parameters.
Stage 3 − Let us make nonexclusive techniques to get to the Excel record utilizing the imported JARs. These strategies assist us with getting a specific cell information or to set a specific cell information, and so forth.
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class ExcelUtils {
private XSSFSheet ExcelWSheet;
private XSSFWorkbook ExcelWBook;
//Constructor to connect to the Excel with sheetname and Path
public Excelutils(String Path, String SheetName) throws Exception {
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
} catch (Exception e) {
throw (e);
}
}
//This method is to set the rowcount of the excel.
public int excel_get_rows() throws Exception {
try {
return ExcelWSheet.getPhysicalNumberOfRows();
} catch (Exception e) {
throw (e);
}
}
//This method to get the data and get the value as strings.
public String getCellDataasstring(int RowNum, int ColNum) throws Exception {
try {
String CellData =
ExcelWSheet.getRow(RowNum).getCell(ColNum).getStringCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
} catch (Exception e) {
return "Errors in Getting Cell Data";
}
}
//This method to get the data and get the value as number.
public double getCellDataasnumber(int RowNum, int ColNum) throws Exception {
try {
double CellData =
ExcelWSheet.getRow(RowNum).getCell(ColNum).getNumericCellValue();
System.out.println("The value of CellData " + CellData);
return CellData;
} catch (Exception e) {
return 000.00;
}
}
}
Stage 4 − Now include a principle technique which will get to the Excel strategies that we have created.
public class xldemo {
public static void main(String[] args) throws Exception {
ExcelUtils dd = new ExcelUtils ("C:\\Book1.xlsx","Sheet1");
System.out.println("The Row count is " + dd.excel_get_rows());
dd.getCellDataasnumber(1, 1);
dd.getCellDataasnumber(1, 2);
dd.getCellDataasnumber(1, 3);
dd.getCellDataasnumber(2, 1);
dd.getCellDataasnumber(2, 2);
dd.getCellDataasnumber(2, 3);
dd.getCellDataasnumber(3, 1);
dd.getCellDataasnumber(3, 2);
dd.getCellDataasnumber(3, 3);
}
}
Output
After Executing the content, the yield is shown in the comfort as demonstrated as follows.