Selenium-parameterizing-using-excel

提供:Dev Guides
移動先:案内検索

Selenium-Excelを使用したデータ駆動

テストの設計中、テストのパラメーター化は避けられません。 同じことを達成するために、Apache POI-Excel JARを使用します。 Excelの読み取りと書き込みに役立ちます。

JARをダウンロード

selenium_ide_152

  • ステップ2 *-ミラーリンクをクリックしてJARをダウンロードします。

selenium_ide_153

  • ステップ3 *-内容をフォルダーに解凍します。

selenium_ide_154

  • ステップ4 *-解凍されたコンテンツは次のように表示されます。

selenium_ide_155

  • ステップ5 *-新しいプロジェクトを作成し、すべての「外部JAR」を「poi-3.10.FINAL」フォルダーの下に追加します。

selenium_ide_147

  • ステップ6 *-「ooxml-lib」フォルダーの下にすべての「外部JAR」を追加します。

selenium_ide_148

  • ステップ7 *-「lib」フォルダーの下にすべての「外部JAR」を追加します。

selenium_ide_149

  • ステップ8 *-以下に示すように、追加されたJARが表示されます。

selenium_ide_150

  • ステップ9 *-パッケージエクスプローラーが次のように表示されます。 それとは別に、「WebDriver」関連のJARを追加します

selenium_ide_151

パラメータ化

デモンストレーションのために、パーセント計算機テストをパラメーター化します。

  • ステップ1 *-Excelを使用して、パーセント計算に必要なすべての入力をパラメーター化します。 設計されたExcelを以下に示します。

selenium_ide_156

  • ステップ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);
   }

}

出力

スクリプトを実行すると、次のように出力がコンソールに表示されます。

Selenium IDE 157