Keyword Framework (一)-- Java

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lb245557472/article/details/84928909

目录

简介

关键词驱动测试,又称表驱动测试或基于动作词的测试,是一种既适合手工测试又适合自动测试的软件测试方法。这种方法将测试用例的文档——包括要使用的数据——与测试用例执行方式的规定分离开来。因此,它将测试创建过程分成两个不同的阶段:设计和开发阶段,以及执行阶段。关键词驱动框架是一种功能自动化测试框架,也称为表驱动测试或基于动作词的测试。关键字驱动框架的基本工作是将测试用例划分为四个不同的部分。第一步称为测试步骤,第二步是测试步骤的对象,第三步是对测试对象的操作,第四步是测试对象的数据。

  • 测试步骤:它是测试步骤的一个非常小的描述,或者是将要对测试对象执行的操作的描述。
  • 测试对象:它是Web页面对象/元素的名称,如用户名和密码。
  • 动作:是动作的名称,动作将在任何对象上执行,如点击、打开浏览器、输入等。
  • 测试数据:数据可以是对象执行任何操作所需的任何值,例如Username字段的Username值

以下是完成框架运行所需的常用组件:

  • Excel表格:这个表格保存了测试用例、测试步骤、测试对象和操作等测试所需的关键字驱动的大部分数据。
  • 对象存储库:属性文件用于存储web应用程序的html元素属性,该属性文件将与测试中的对象链接
  • 关键字函数库:在关键字驱动框架中,函数文件起着重要的作用,因为它存储了动作的工作,这样每个动作都可以从这个文件中调用
  • 数据表:Excel文件,用于存储对象执行某些操作所需的数据值
  • 执行引擎:Test是我们在关键字框架中唯一的测试脚本,它包含了从Excel表格、函数库和属性文件中驱动测试的所有代码。
    在这里插入图片描述

优势

  • 较少的技术专业知识:一旦框架建立起来,手工测试人员或非技术测试人员可以很容易地为自动化编写测试脚本。
  • 易于理解:由于它是在Excel表中维护的,并且没有公开代码,所以测试脚本易于阅读和理解。关键字和操作与手工测试用例非常相似,因此更容易编写和维护。
  • 尽早开始:您可以在应用程序交付之前开始构建关键字驱动的测试用例,因为可以在后期轻松地设置对象存储库。使用从需求或其他文档中收集的信息,可以创建与相应的手工测试过程相对应的关键字数据表
  • 组件的可重用性:通过在关键字驱动下实现模块化,可以进一步提高组件的可重用性。
  • 代码的可重用性:由于关键字驱动框架中只有一个执行引擎,可保证代码可重用性

搭建框架步骤

先决条件:

  • Java环境
  • IDEA工具
  • selenium

executionEngine

  1. 首先创建一个maven的项目
  2. 创建executionEngine package,在该package下新建一个DriverScript的Java 类

    package executionEngine;
    
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class DriverScript {
            private static WebDriver driver = null;
    	     public static void main(String[] args) {
            driver = new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.store.demoqa.com");
    
            driver.findElement(By.xpath(".//*[@id='account']/a")).click();
            driver.findElement(By.id("log")).sendKeys("testuser_3"); 
            driver.findElement(By.id("pwd")).sendKeys("Test@123");
            driver.findElement(By.id("login")).click();
            driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();
            driver.quit();
                }
        }

dataEngine

  1. 创建dataEngine package,在该package下新建一个DataEngine的Excel文件
  2. 打开将Excel中的sheet1重命名为TestCase
  3. 在TestCase表中创建以下几列,像下图一样:
    · TestCase ID
    · TestScenario ID
    · Description
    · Action_Keyword
    在这里插入图片描述

config

  1. 创建config package,在该package下新建一个ActionKeywords的Java 类
  2. 代码如下:

    package config;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class ActionKeywords {
    
        public static WebDriver driver;
    
        public static void openBrowser(){
            driver=new ChromeDriver();
        }
    
        public static void navigate(){
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("http://www.store.demoqa.com");
        }
    
        public static void click_MyAccount(){
            driver.findElement(By.xpath(".//*[@id='account']/a")).click();
        }
    
        public static void input_Username(){
            driver.findElement(By.id("log")).sendKeys("testuser_3");
        }
    
        public static void input_Password(){
            driver.findElement(By.id("pwd")).sendKeys("Test@123");
        }
    
        public static void click_Login(){
            driver.findElement(By.id("login")).click();
        }
    
        public static void waitFor() throws Exception{
            Thread.sleep(5000);
        }
    
        public static void click_Logout(){
            driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();
        }
    
        public static void closeBrowser(){
            driver.quit();
        }
        public void test(){
            System.out.println("test123");
        }
    }

utility库

  1. 下载Apache POI的jar,将其加入到项目的引用库中(具体怎么加入jar包参考:这里
  2. 创建utility package,在该package下新建一个ExcelUtils的Java类
  3. 代码如下:

    package utility;
    
    import java.io.FileInputStream;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    public class ExcelUtils {
        private static XSSFSheet ExcelWSheet;
        private static XSSFWorkbook ExcelWBook;
        private static XSSFCell Cell;
        //This method is to set the File path and to open the Excel file
        //Pass Excel Path and SheetName as Arguments to this method
        public static void setExcelFile(String Path,String SheetName) throws Exception {
            FileInputStream ExcelFile = new FileInputStream(Path);
            ExcelWBook = new XSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        }
        //This method is to read the test data from the Excel cell
        //In this we are passing parameters/arguments as Row Num and Col Num
        public static String getCellData(int RowNum, int ColNum) throws Exception{
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            String CellData = Cell.getStringCellValue();
            return CellData;
        }
    }

  1. 更新DriverScript中的代码如下:

    package executionEngine;
    
    import java.util.concurrent.TimeUnit;
    
    import config.ActionKeywords;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import utility.ExcelUtils;
    
    public class DriverScript {
        private static WebDriver driver = null;
        public static void main(String[] args) throws Exception {
    
            // Declaring the path of the Excel file with the name of the Excel file
            String sPath = "src\\test\\java\\dataEngine\\DataEngine.xlsx";
    
            // Here we are passing the Excel path and SheetName as arguments to connect with Excel file
            ExcelUtils.setExcelFile(sPath, "TestCase");
    
            //Hard coded values are used for Excel row & columns for now
            //In later chapters we will replace these hard coded values with varibales
            //This is the loop for reading the values of the column 3 (Action Keyword) row by row
            for (int iRow=1;iRow<=9;iRow++){
                //Storing the value of excel cell in sActionKeyword string variable
                String sActionKeyword = ExcelUtils.getCellData(iRow, 3);
                //Comparing the value of Excel cell with all the project keywords
                if(sActionKeyword.equals("openBrowser")){
                    //This will execute if the excel cell value is 'openBrowser'
                    //Action Keyword is called here to perform action
                    ActionKeywords.openBrowser();}
                else if(sActionKeyword.equals("navigate")){
                    ActionKeywords.navigate();}
                else if(sActionKeyword.equals("click_MyAccount")){
                    ActionKeywords.click_MyAccount();}
                else if(sActionKeyword.equals("input_Username")){
                    ActionKeywords.input_Username();}
                else if(sActionKeyword.equals("input_Password")){
                    ActionKeywords.input_Password();}
                else if(sActionKeyword.equals("click_Login")){
                    ActionKeywords.click_Login();}
                else if(sActionKeyword.equals("waitFor")){
                    ActionKeywords.waitFor();}
                else if(sActionKeyword.equals("click_Logout")){
                    ActionKeywords.click_Logout();}
                else if(sActionKeyword.equals("closeBrowser")){
                    ActionKeywords.closeBrowser();}
    
            }
        }
    }

最终的目录结构如下图:
在这里插入图片描述

总结

至此,我们的初始搭建的框架已经完成,但是只是草稿版,还需要很多的优化,使其更加的高效、健壮,在以下的章节中会介绍加强版的。

后续要做

  1. 利用Java的映射类重写DriverScript
  2. 设置常量文件
  3. 建立对象库
  4. logging
  5. 异常处理
  6. 报告

参考

猜你喜欢

转载自blog.csdn.net/lb245557472/article/details/84928909