• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
金大鑫要坚持
博客园    首页    新随笔    联系   管理    订阅  订阅

【测试文章搬运】用selenium并从excel里面读取测试数据——采用数据驱动测试自动化框架

Selenium Framework Creation And Accessing Test Data From Excel – Selenium Tutorial

在本系列的当前教程中,我们将为您提供一个示例框架,用于存储测试数据及其 Excel 操作的 Excel。同样,我们将继续前进并引入新的策略和资源来完善我们的框架。

让我们学习一下:

  • 使用示例项目的框架创建策略。
  • 访问存储在外部数据源中的测试数据。

接下来,我们将从创建项目层次结构的描述开始,以分离各个项目组件。

请参阅下图,了解为示例项目创建的项目层次结构。您可以按照我们在之前的教程中使用的相同流程,在 Eclipse 中轻松创建以下 Java 项目。

Selenium 框架

Selenium 项目文件夹结构 – 演练

#1) src –该文件夹包含所有测试脚本、泛型、读取器和实用程序。所有这些资源都只是简单的 Java 类。在源 (src) 文件夹下,我们创建了一个文件夹层次结构。

a) 测试 – “测试”文件夹主要由两个部分组成 – 测试套件和代表被测应用程序各个模块的文件夹。因此,每个文件夹都包含与其关联的模块特定的测试脚本。

Testsuite 在逻辑上结合了多个测试脚本。因此,用户可以标记测试套件中他/她希望在后续运行中执行的任意测试脚本的条目。

b) 实用程序 – “实用程序”文件夹由各种泛型、常量、读取器和用于实现用户定义异常的类组成。实用程序下的每个文件夹都有其重要意义。

  • Excel 阅读器:已创建一个通用的公共类,用于从 Excel 表中读取测试数据(输入参数和预期结果)
  • EnvironmentConstants:该文件夹是 Java 类的集成,用于存储引用路径和其他环境详细信息的静态变量。这些详细信息可以是应用程序 URL、数据库 URL、数据库凭据以及正在使用的任何第三方工具的 URL。可以为不同的环境(开发、生产、测试​​、主、从等)设置不同的应用程序 URL。
  • DataSetters:该文件夹包含实现从 Excel 获取的测试数据的 getter 和 setter 的类。为了加载多组测试数据,我们创建了 ArrayLists。
  • UserRoles:该文件夹包含为本能用户处理基于角色的访问标准(如果有)的类。
  • FunctionLibrary:该文件夹由包含可在多个类之间共享和使用的函数和方法的类组成。很多时候,我们应该在实际测试执行之前和之后执行某些程序,例如登录应用程序、设置环境、与角色相关的活动、数据操作、写入结果以及为其他方法生成前置/后置条件的方法。因为我们对所有或大部分测试脚本执行这些活动。因此,始终建议为此类活动创建一个单独的类,而不是在每个测试脚本中重复编码它们。
    • 先决条件方法
    • 后置条件方法

通常,我们应该在实际测试执行之前和之后执行某些程序,例如登录应用程序、设置环境、与用户角色相关的活动、数据操作、写入结果、为其他方法生成前置/后置条件的方法。

由于我们对所有或大多数测试脚本执行这些活动,因此始终建议为此类活动创建一个单独的类,而不是在每个测试脚本中重复编码。

常用方法

与前置条件和后置条件一样,可能有多个方法和函数可供多个测试脚本使用。因此,这些方法被组合在一个类中。测试脚本可以使用公共类的对象来访问这些方法。

#2) excel 文件 – excel 文件被视为测试脚本执行的数据源/数据提供者。这些文件将测试数据存储到键值对中。请注意,我们为每个测试脚本创建一个单独的 Excel 表,即每个测试脚本都有自己的测试数据文件。

为了保持可追溯性,测试脚本的名称与相应的测试数据文件或 Excel 表保持相同。请查看下面的示例测试数据格式:

测试数据格式:

Selenium 框架 2

每列代表一个键,每行代表一个测试数据/值。指定多行以使用多个数据集执行相同的测试脚本。请注意,测试数据格式完全由用户定义。因此,您可以根据您的要求自定义测试数据文件。

#3)库 -该文件夹充当所有必需的 jar 文件、库、驱动程序等的存储库/工件,以成功构建测试环境并执行测试脚本。 请参阅下图以查看我们将在项目中使用的库。

Selenium 框架 3

#4) logs –该文件夹包含一个.txt 文件,用于存储每次执行时的日志信息。

#5)测试材料 -该文件夹包含需要上传的实际测试数据(如果有)。当我们遇到需要用户上传文件、文档、图片、报告等的测试场景时,此文件夹就会出现。

#6) build.xml – “Ant Server”使用 XML 文件来自动化整个构建过程。

#7) log4j.xml –该 xml 文件由名为“Log4j”的基于 Java 的实用程序使用来生成执行日志。

注意:我们将在接下来的教程中详细研究日志、用户定义的异常和 Ant。因此,如果您对这些概念感到困惑,请不要惊慌。

现在,让我们进一步了解访问 Excel 文件并将测试数据填充到测试脚本中的现象。

为了更容易理解该过程,我们将该过程分解为以下步骤。

测试数据创建

步骤 1:第一步是创建用于执行测试脚本的测试数据。考虑到上述测试数据格式,让我们创建一个名为“TestScript1”的 Excel 文件。在元素中提供值。

Excel 文件

步骤#2:下一步是下载一个名为“Java excel 库”(jxl)的标准基于 Java 的 API/库,以便能够访问已创建的 Excel 操作通用方法。

步骤#3:创建一个名为“ExcelReader.java”的通用 Excel 阅读器类。将以下代码复制到 ExcelReader.java 中。

 
package Utilities;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
  
/**
 * This is a utility class created to read the excel test data file before performing the test steps.
 * This class loads the excel file and
 * reads its column entries.
 *
 */
  
public class ExcelReader {
                /**
                 * The worksheet to read in Excel file
                 */
  
                public static Sheet wrksheet;
                /**
                 * The Excel file to read
                 */
  
                public static Workbook wrkbook = null;
                /**
                 * Store the column data
                 */
  
                public static Hashtable<String, Integer> dict = new Hashtable<String, Integer>();
                /**
                 * Create a Constructor
                 *
                 * @param ExcelSheetPath
                 * @throws BiffException
                 * @throws WeblivException
                 */
  
                public ExcelReader(String ExcelSheetPath) throws IOException, BiffException {
  
                            // Initialize
                                try {
                                               wrkbook = Workbook.getWorkbook(new File(ExcelSheetPath));
                                               wrksheet = wrkbook.getSheet("Sheet1");
                                } catch (IOException e) {
                                                throw new IOException();
                                }
                }
                /**
                 * Returns the Number of Rows
                 *
                 * @return Rows
                 */
  
                public static int RowCount() {
                                return wrksheet.getRows();
                }
                /**
                 * Returns the Cell value by taking row and Column values as argument
                 *
                 * @param column
                 * @param row
                 * @return Cell contents
                 */
  
                public static String ReadCell(int column, int row) {
                                return wrksheet.getCell(column, row).getContents();
                }
                /**
                * Create Column Dictionary to hold all the Column Names
                 */
                public static void ColumnDictionary() {
                                // Iterate through all the columns in the Excel sheet and store the
                                // value in Hashtable
                                for (int col = 0; col < wrksheet.getColumns(); col++) {
                                                dict.put(ReadCell(col, 0), col);
                                }
                }
                /**
                 * Read Column Names
                 *
                 * @param colName
                 * @return value
                 */
  
                public static int GetCell(String colName) {
                                try {
                                                int value;
                                                value = ((Integer) dict.get(colName)).intValue();
                                                return value;
                                } catch (NullPointerException e) {
                                                return (0);
                                }
                }
}

步骤#4:创建一个通用类——“CommonMethods.java”。在类中创建一个通用方法,使用在 ExcelReader.java 中实现的方法从 Excel 表中读取单元格。

 
 
/**
* Read the test data from excel file
*
* @param data The TestData data object
*/
  
public void readExcelData (TestData data) {
       ArrayList<String> browser = new ArrayList<String>();
       ArrayList<String> username = new ArrayList<String>();
       ArrayList<String> password = new ArrayList<String>();
       ArrayList<String> element1 = new ArrayList<String>();
       ArrayList<String> element2 = new ArrayList<String>();
       ArrayList<String> element3 = new ArrayList<String>();
  
       // Get the data from excel file
       for (int rowCnt = 1; rowCnt < ExcelReader.RowCount(); rowCnt++) {
       browser.add(ExcelReader.ReadCell(ExcelReader.GetCell("Browser"), rowCnt));
       username.add(ExcelReader.ReadCell(ExcelReader.GetCell("User ID"), rowCnt));
                     password.add(ExcelReader.ReadCell(ExcelReader.GetCell("Password"), rowCnt));
                     element1.add(ExcelReader.ReadCell(ExcelReader.GetCell("Element1"), rowCnt));
                     element2.add(ExcelReader.ReadCell(ExcelReader.GetCell("Element2"), rowCnt));
       element3.add(ExcelReader.ReadCell(ExcelReader.GetCell("Element3"), rowCnt));
       }
       data.setBrowser(browser);
       data.setLoginUser(username);
       data.setPassword(password);
       data.setElement1(element1);
       data.setElement2(element2);
       data.setElement3(element3);
       }

步骤#5:创建一个名为“TestData.java”的新 Java 类。此类将充当 Excel 数据的 getter 和 setter。将以下代码复制并粘贴到 TestData.java 类中。

 
package Utilities.dataSetters;
import java.util.ArrayList;
public class TestData {
       private ArrayList<String> loginUser = null;
       private ArrayList<String> password = null;
       private ArrayList<String> browser = null;
       private ArrayList<String> element1 = null;
       private ArrayList<String> element2 = null;
       private ArrayList<String> element3 = null;
       /**
        * @return loginUser
        */
       public ArrayList<String> getLoginUser() {
              return loginUser;
       }
       /**
        * @param loginUser
        */
       public void setLoginUser(ArrayList<String> loginUser) {
              this.loginUser = loginUser;
       }
       /**
        * @return password
        */
       public ArrayList<String> getPassword() {
              return password;
       }
       /**
        * @param password
        */
       public void setPassword(ArrayList<String> password) {
              this.password = password;
       }
       /**
        * @return browser
        */
       public ArrayList<String> getBrowser() {
              return browser;
       }
       /**
        * @param browser
        */
       public void setBrowser(ArrayList<String> browser) {
              this.browser = browser;
       }
       /**
        * @return element1
        */
       public ArrayList<String> getElement1() {
              return element1;
       }
       /**
        * @param element1
       */
       public void setElement1(ArrayList<String> element1) {
              this.element1 = element1;
       }    
       /**
        * @return element2
        */
       public ArrayList<String> getElement2() {
              return element2;
       }
       /**
        * @param element2
        */
       public void setElement2(ArrayList<String> element2) {
              this.element2 = element2;
      }
       /**
        * @return element3
        */
       public ArrayList<String> getElement3() {
              return element3;
       }
       /**
        * @param element3
       */
       public void setElement3(ArrayList<String> element3) {
              this.element3 = element3;
       }    
}

步骤 #6:下一步是在测试脚本中创建“TestData.java”和“CommonMethods.java”Java 类的实例,以访问和填充测试数据。请参阅以下代码片段,了解对象初始化、读取 Excel 数据以及在需要时填充值。

 
// Create Objects
public ExcelReader excelReaderObj;
CommonMethods commonMethodobj = new CommonMethods();
TestData td = new TestData();
  
// Load the excel file for testing
excelReaderObj = new ExcelReader(Path of the excel);
  
// Load the Excel Sheet Col in to Dictionary for use in test cases
excelReaderObj.ColumnDictionary();
  
// Get the data from excel file
commonMethodobj.readExcelData (td);
  
// Populate the username
driver.findElement(By.id("idofElement")).sendKeys(data.getLoginUser().get(0));

因此,将 testData.java 类的实例与 getter 结合使用,可以在脚本中填充任何测试数据值。

结论

本教程主要围绕框架创建和从 Excel 访问测试数据等概念展开。我们使用示例项目让您熟悉框架创建策略。我们简要介绍了框架的各个组件和方面。

为了访问存储在外部数据源中的测试数据,我们使用了基于 Java 的 API - jxl。我们还创建了用于读取 Excel 数据并将其填充到测试脚本中的示例代码。

posted @ 2024-06-23 16:56  金大鑫要坚持  阅读(151)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3