Selenio - Data Driven utilizzando Excel
Durante la progettazione di un test, la parametrizzazione dei test è inevitabile. Utilizzeremo Apache POI - Excel JAR per ottenere lo stesso risultato. Ci aiuta a leggere e scrivere in Excel.
Scarica JAR
Step 1 - Vai all'URL - https://poi.apache.org/download.html e scarica il formato ZIP.

Step 2 - Fare clic sul collegamento Mirror per scaricare i JAR.

Step 3 - Decomprimere il contenuto in una cartella.

Step 4 - I contenuti decompressi verranno visualizzati come mostrato di seguito.

Step 5 - Ora crea un nuovo progetto e aggiungi tutti i "JAR esterni" nella cartella "poi-3.10.FINAL".

Step 6 - Ora aggiungi tutti i "JAR esterni" nella cartella "ooxml-lib".

Step 7 - Ora aggiungi tutti i "JAR esterni" nella cartella "lib".

Step 8 - Il JAR aggiunto viene visualizzato come mostrato di seguito.

Step 9- Il Package Explorer viene visualizzato come mostrato di seguito. Oltre a questo, aggiungi i JAR correlati a "WebDriver"

Parametrizzazione
A scopo dimostrativo, parametrizzeremo il test del calcolatore percentuale.
Step 1- Parametrizzeremo tutti gli input richiesti per il calcolatore percentuale utilizzando Excel. L'Excel progettato è mostrato di seguito.

Step 2 - Esegue tutte le funzioni di calcolatrice percentuale per tutti i parametri specificati.
Step 3- Creiamo metodi generici per accedere al file Excel utilizzando i JAR importati. Questi metodi ci aiutano a ottenere dati di una cella particolare o per impostare i dati di una cella particolare, ecc.
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 - Ora aggiungi un metodo principale che accederà ai metodi Excel che abbiamo sviluppato.
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);
}
}
Produzione
All'esecuzione dello script, l'output viene visualizzato nella console come mostrato di seguito.
