Selenium-parameterizing-using-excel
提供:Dev Guides
Selenium-Excelを使用したデータ駆動
テストの設計中、テストのパラメーター化は避けられません。 同じことを達成するために、Apache POI-Excel JARを使用します。 Excelの読み取りと書き込みに役立ちます。
JARをダウンロード
- ステップ2 *-ミラーリンクをクリックしてJARをダウンロードします。
- ステップ3 *-内容をフォルダーに解凍します。
- ステップ4 *-解凍されたコンテンツは次のように表示されます。
- ステップ5 *-新しいプロジェクトを作成し、すべての「外部JAR」を「poi-3.10.FINAL」フォルダーの下に追加します。
- ステップ6 *-「ooxml-lib」フォルダーの下にすべての「外部JAR」を追加します。
- ステップ7 *-「lib」フォルダーの下にすべての「外部JAR」を追加します。
- ステップ8 *-以下に示すように、追加されたJARが表示されます。
- ステップ9 *-パッケージエクスプローラーが次のように表示されます。 それとは別に、「WebDriver」関連のJARを追加します
パラメータ化
デモンストレーションのために、パーセント計算機テストをパラメーター化します。
- ステップ1 *-Excelを使用して、パーセント計算に必要なすべての入力をパラメーター化します。 設計されたExcelを以下に示します。
- ステップ2 *-指定されたすべてのパラメーターに対してすべてのパーセント計算機能を実行します。
- ステップ3 *-インポートされたJARを使用してExcelファイルにアクセスするための汎用メソッドを作成しましょう。 これらのメソッドは、特定のセルデータの取得や特定のセルデータの設定などに役立ちます。
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;
}
}
}
- ステップ4 *-ここで、開発したExcelメソッドにアクセスするメインメソッドを追加します。
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);
}
}
出力
スクリプトを実行すると、次のように出力がコンソールに表示されます。