• 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
广告