Selenium: datos basados en Excel
Al diseñar una prueba, la parametrización de las pruebas es inevitable. Usaremos Apache POI - Excel JAR's para lograr lo mismo. Nos ayuda a leer y escribir en Excel.
Descarga JAR
Step 1 - Navega a la URL - https://poi.apache.org/download.html y descargue el formato ZIP.

Step 2 - Haga clic en Mirror Link para descargar los JAR.

Step 3 - Descomprime el contenido en una carpeta.

Step 4 - Los contenidos descomprimidos se mostrarán como se muestra a continuación.

Step 5 - Ahora cree un nuevo proyecto y agregue todos los 'JAR externos' en la carpeta 'poi-3.10.FINAL'.

Step 6 - Ahora agregue todos los 'JAR externos' en la carpeta 'ooxml-lib'.

Step 7 - Ahora agregue todos los 'JAR externos' en la carpeta 'lib'.

Step 8 - El JAR agregado se muestra como se muestra a continuación.

Step 9- El Explorador de paquetes se muestra como se muestra a continuación. Aparte de eso, agregue los JAR relacionados con 'WebDriver'

Parametrización
Para demostración, parametrizaremos la prueba de la calculadora de porcentaje.
Step 1- Parametrizaremos todas las entradas requeridas para la calculadora de porcentaje usando Excel. El Excel diseñado se muestra a continuación.

Step 2 - Ejecute todas las funciones de la calculadora de porcentaje para todos los parámetros especificados.
Step 3- Creemos métodos genéricos para acceder al archivo de Excel utilizando los JAR importados. Estos métodos nos ayudan a obtener datos de una celda en particular o establecer datos de una celda en particular, etc.
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;
}
}
}
Step 4 - Ahora agregue un método principal que accederá a los métodos de Excel que hemos desarrollado.
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);
}
}
Salida
Al ejecutar el script, la salida se muestra en la consola como se muestra a continuación.
