• Selenium Video Tutorials

Selenium - 使用Excel進行資料驅動



在設計測試時,引數化測試是不可避免的。我們將使用 Apache POI - Excel JAR 來實現這一點。它可以幫助我們讀取和寫入 Excel。

下載 JAR

步驟 1 - 導航到 URL - https://poi.apache.org/download.html 並下載 ZIP 格式。

selenium_ide_152

步驟 2 - 單擊映象連結下載 JAR。

selenium_ide_153

步驟 3 - 將內容解壓縮到資料夾中。

selenium_ide_154

步驟 4 - 解壓縮後的內容將如下所示。

selenium_ide_155

步驟 5 - 現在建立一個新專案,並在“poi-3.10.FINAL”資料夾下新增所有“外部 JAR”。

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
selenium_test_design_techniques.htm
廣告

© . All rights reserved.