关键字驱动框架入门教程-11-测试结果报告

      上一篇,我们介绍了如何在框架中处理异常。简单来说就是没一个方法里面都需要使用try-catch语句。而且我们注意到只要定义的static变量bResult变成了false,说明就出现了异常。本篇我们就是来讨论下如何处理当bResult变成了false之后的后续步骤。简单一句话说,如果bResult的值变成了false,我们就在Excel对应用例位置标识failed的标记,如果是ture,我们就可以标记pass。这样一来,我们通过pass和failed就得到了一个简单的测试结果的报告。

 

      由于是关键字驱动框架,这里无法像POM那样引入TestNG生成更美观的html格式的报告。本篇说的测试结果报告,就是如何实现动态在excel文件里面对每个测试用例或者测试步骤进行标记pass还是failed。如何做呢?


 

1.在Test Cases表中,在Runmode右边新建一列,命名为Results

2.通过在Test Steps表中新建一个Results列

3. 在常量类文件添加两个地方的Results的变量

// 第一个是测试用例结果标记列的索引,第二个是测试步骤里面的结果列索引
    public static final int Col_Result =3 ;
    public static final int Col_TestStepResult =5 ;

4. 在常量类文件添加两个变量,记录结果状态的PASS 和FAIL

// 结果状态标记
    public static final String KEYWORD_FAIL = "FAIL";
    public static final String KEYWORD_PASS = "PASS";

5. 在ExcelUtilt.java中新增一个方法,用来填写结果状态

 下面代码的Cell,需要在定义,(private static XSSFRow Row;),具体看后面贴的代码

 // 构造一个往单元格写数据的方法,主要是用来写结果pass还是fail
    public static void setCellData(String Result,  int RowNum, int ColNum, String SheetName) throws Exception    {
        try{

            ExcelWSheet = ExcelWBook.getSheet(SheetName);
            Row  = ExcelWSheet.getRow(RowNum);
            Cell = Row.getCell(ColNum);
            if (Cell == null) {
                Cell = Row.createCell(ColNum);
                Cell.setCellValue(Result);
            } else {
                Cell.setCellValue(Result);
            }
            // Constant variables Test Data path and Test Data file name
            FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData);
            ExcelWBook.write(fileOut);
            //fileOut.flush();
            fileOut.close();
            ExcelWBook = new XSSFWorkbook(new FileInputStream(Constants.Path_TestData));
        }catch(Exception e){
            DriverScript.bResult = false;
        }
    }

 ExcelUtilt.java的完整代码如下

package utility;

import config.Constants;
import executionEngine.DriverScript;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class ExcelUtils {

    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;


    // 设置Excel文件路径,方便读取到文件
    public static void setExcelFile(String Path) throws Exception {
        try {
            FileInputStream ExcelFile = new FileInputStream(Path);
            ExcelWBook = new XSSFWorkbook(ExcelFile);
        } catch (Exception e){
            Log.error("Class Utils | Method setExcelFile | Exception desc : "+e.getMessage());
            DriverScript.bResult = false;
        }
    }

    // 读取Excel文件单元格数据
    // 新增sheet name参数,这样就可以去读取Test Steps和Test Cases两个工作表的单元格数据
    public static String getCellData(int RowNum, int ColNum, String SheetName ) throws Exception{
        try{
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            String CellData = Cell.getStringCellValue();
            return CellData;
        }catch (Exception e){
            Log.error("Class Utils | Method getCellData | Exception desc : "+e.getMessage());
            DriverScript.bResult = false;
            return"";
        }
    }

    //得到一共多少行数据
    public static int getRowCount(String SheetName){
        int iNumber=0;
        try {
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
            iNumber=ExcelWSheet.getLastRowNum()+1;
        } catch (Exception e){
            Log.error("Class Utils | Method getRowCount | Exception desc : "+e.getMessage());
            DriverScript.bResult = false;
        }
        return iNumber;
    }

    //得到测试用例的行号
    public static int getRowContains(String sTestCaseName, int colNum,String SheetName) throws Exception{
        int iRowNum=0;
        try {
            //ExcelWSheet = ExcelWBook.getSheet(SheetName);
            int rowCount = ExcelUtils.getRowCount(SheetName);
            for (; iRowNum<rowCount; iRowNum++){
                if  (ExcelUtils.getCellData(iRowNum,colNum,SheetName).equalsIgnoreCase(sTestCaseName)){
                    break;
                }
            }
        } catch (Exception e){
            Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
            DriverScript.bResult = false;
        }
        return iRowNum;
    }

    //计算一个测试用例有多少个步骤
    public static int getTestStepsCount(String SheetName, String sTestCaseID, int iTestCaseStart) throws Exception{
        try {
            for(int i=iTestCaseStart;i<=ExcelUtils.getRowCount(SheetName);i++){
                if(!sTestCaseID.equals(ExcelUtils.getCellData(i, Constants.Col_TestCaseID, SheetName))){
                    int number = i;
                    return number;
                }
            }
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
            int number=ExcelWSheet.getLastRowNum()+1;
            return number;
        } catch (Exception e){
            Log.error("Class Utils | Method getRowContains | Exception desc : "+e.getMessage());
            DriverScript.bResult = false;
            return 0;
        }
    }

    // 构造一个往单元格写数据的方法,主要是用来写结果pass还是fail
    public static void setCellData(String Result,  int RowNum, int ColNum, String SheetName) throws Exception    {
        try{

            ExcelWSheet = ExcelWBook.getSheet(SheetName);
            Row  = ExcelWSheet.getRow(RowNum);
            Cell = Row.getCell(ColNum);
            if (Cell == null) {
                Cell = Row.createCell(ColNum);
                Cell.setCellValue(Result);
            } else {
                Cell.setCellValue(Result);
            }
            // Constant variables Test Data path and Test Data file name
            FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData);
            ExcelWBook.write(fileOut);
            //fileOut.flush();
            fileOut.close();
            ExcelWBook = new XSSFWorkbook(new FileInputStream(Constants.Path_TestData));
        }catch(Exception e){
            DriverScript.bResult = false;
        }
    }

}

6. 修改DriverScript.java

package executionEngine;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import config.ActionsKeywords;
import config.Constants;
import org.apache.log4j.xml.DOMConfigurator;
import utility.ExcelUtils;
import utility.Log;

import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Properties;


/**
 * create by Anthony on 2018/1/30
 */
public class DriverScript {

    // 声明一个public static的类对象,所以我们可以在main方法范围之外去使用。
    public static ActionsKeywords actionsKeywords;
    public static String sActionKeyword;
    // 下面是返回类型是方法,这里用到反射类
    public static Method method[];
    // 新建一个Properties对象
    public static Properties OR;
    public static String sPageObject;

    public static int iTestStep;
    public static int iTestLastStep;
    public static String sTestCaseID;
    public static String sRunMode;
    public static boolean bResult;

    // 这里我们初始化'ActionsKeywords'类的一个对象
    public DriverScript() throws NoSuchMethodException, SecurityException{
        actionsKeywords = new ActionsKeywords();
        method = actionsKeywords.getClass().getMethods();
    }

    public static void main(String[] args) throws Exception {
        // 这样一定要加,否则报log4j初始化的警告
        DOMConfigurator.configure("log4j.xml");
        ExcelUtils.setExcelFile(Constants.Path_TestData);
        // 创建一个文件输入流对象,参数来源外部OR.txt文件
        FileInputStream fs = new FileInputStream(Constants.Path_OR);
        // 创建一个Properties对象
        OR = new Properties(System.getProperties());
        // 加载全部对象仓库文件
        OR.load(fs);
        // 执行用例
        DriverScript startEngine = new DriverScript();
        startEngine.execute_TestCase();
    }

    private void execute_TestCase() throws Exception {
        //获取测试用例数量
        int iTotalTestCases = ExcelUtils.getRowCount(Constants.Sheet_TestCases);
        //外层for循环,有多少个测试用例就执行多少次循环
        for(int iTestcase=1;iTestcase<=iTotalTestCases;iTestcase++){
            //从Test Case表获取测试ID
            bResult = true;
            sTestCaseID = ExcelUtils.getCellData(iTestcase, Constants.Col_TestCaseID, Constants.Sheet_TestCases);
            //获取当前测试用例的Run Mode的值
            sRunMode = ExcelUtils.getCellData(iTestcase, Constants.Col_RunMode,Constants.Sheet_TestCases);
            // Run Mode的值控制用例是否被执行
            if (sRunMode.equals("Yes")){
                // 只有当Run Mode的单元格数据是Yes,下面代码才会被执行
                iTestStep = ExcelUtils.getRowContains(sTestCaseID, Constants.Col_TestCaseID, Constants.Sheet_TestSteps);
                iTestLastStep = ExcelUtils.getTestStepsCount(Constants.Sheet_TestSteps, sTestCaseID, iTestStep);

                bResult=true;
                //下面这个for循环的次数就等于测试用例的步骤数
                for (;iTestStep <= iTestLastStep;iTestStep++){
                    sActionKeyword = ExcelUtils.getCellData(iTestStep, Constants.Col_ActionKeyword,Constants.Sheet_TestSteps);
                    sPageObject = ExcelUtils.getCellData(iTestStep, Constants.Col_PageObject, Constants.Sheet_TestSteps);
                    execute_Actions();
                    if(bResult==false){
                        //If 'false' then store the test case result as Fail
                        ExcelUtils.setCellData(Constants.KEYWORD_FAIL,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);
                        //End the test case in the logs
                        Log.endTestCase(sTestCaseID);
                        //By this break statement, execution flow will not execute any more test step of the failed test case
                        break;
                    }
                }

                //This will only execute after the last step of the test case, if value is not 'false' at any step
                if(bResult==true){
                    //Storing the result as Pass in the excel sheet
                    ExcelUtils.setCellData(Constants.KEYWORD_PASS,iTestcase,Constants.Col_Result,Constants.Sheet_TestCases);
                    Log.endTestCase(sTestCaseID);
                }
            }
        }
    }

    private static void execute_Actions() throws Exception {

        for(int i=0;i<method.length;i++){

            if(method[i].getName().equals(sActionKeyword)){
                method[i].invoke(actionsKeywords,sPageObject);
                //This code block will execute after every test step
                if(bResult==true){
                    //If the executed test step value is true, Pass the test step in Excel sheet
                    ExcelUtils.setCellData(Constants.KEYWORD_PASS, iTestStep, Constants.Col_TestStepResult, Constants.Sheet_TestSteps);
                    break;
                }else{
                    //If the executed test step value is false, Fail the test step in Excel sheet
                    ExcelUtils.setCellData(Constants.KEYWORD_FAIL, iTestStep, Constants.Col_TestStepResult, Constants.Sheet_TestSteps);
                    //In case of false, the test execution will not reach to last step of closing browser
                    //So it make sense to close the browser before moving on to next test case
                    ActionsKeywords.closeBrowser("");
                    break;
                }
            }
        }
    }

}


运行main方法,发现没有报错,然后去项目打开dataExcel.java文件,截图如下:

为什么这个地方结果标记失败呢,而下面全部子步骤都成功了。

       原因就在于程序读取TestCase ID,Page Object, Action_Keyword这三列的时候,如遇到下一行没有数据,特别是关键字这列不能为空,为空就可能出现上面这样情况。你就会发现前面Test Cases表的第一个用例执行标记为PASS例如改成下面这样,在运行一次main方法。

本篇关于简单的结果标记报告就介绍到这里。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值