Java自动化测试(回写与断言 17)

时间:2022-07-23
本文章向大家介绍Java自动化测试(回写与断言 17),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

回写

将测试结果写回到Excel中

定义回写数据对象

package com.zhongxin.pojo;

public class WriteBackData {
    private int sheetIndex;
    private int rowNum;
    private int cellNum;
    private String content;

    public WriteBackData(int sheetIndex, int rowNum, int cellNum, String content) {
        this.sheetIndex = sheetIndex;
        this.rowNum = rowNum;
        this.cellNum = cellNum;
        this.content = content;
    }

    public WriteBackData() {
    }

    @Override
    public String toString() {
        return "WriteBackData{" +
                "sheetIndex=" + sheetIndex +
                ", rowNum=" + rowNum +
                ", cellNum=" + cellNum +
                ", content='" + content + ''' +
                '}';
    }

    public int getSheetIndex() {
        return sheetIndex;
    }

    public void setSheetIndex(int sheetIndex) {
        this.sheetIndex = sheetIndex;
    }

    public int getRowNum() {
        return rowNum;
    }

    public void setRowNum(int rowNum) {
        this.rowNum = rowNum;
    }

    public int getCellNum() {
        return cellNum;
    }

    public void setCellNum(int cellNum) {
        this.cellNum = cellNum;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

批量回写操作代码

使用到类似之前Excel到写入操作代码:

public static void batchWrite() throws Exception {
    //回写的逻辑:遍历wdbList集合,取出sheetIndex,rowNum,cellNum,content
    FileInputStream fis = new FileInputStream("src/test/resources/cases_v3.xlsx");
    Workbook sheets = WorkbookFactory.create(fis);
    for (WriteBackData wdb : wdbList) {
        int sheetIndex = wdb.getSheetIndex();
        int rowNum = wdb.getRowNum();
        int cellNum = wdb.getCellNum();
        String content = wdb.getContent();

        Sheet sheet = sheets.getSheetAt(sheetIndex);
        Row row = sheet.getRow(rowNum);
        Cell cell = row.getCell(cellNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
        cell.setCellValue(content);
    }
    FileOutputStream fos = new FileOutputStream("src/test/resources/cases_v3.xlsx");
    sheets.write(fos);
    fis.close();
    fos.close();
}

com.zhongxin.utils.ExcelUtils中需要增加一个wdbList,用于存储运行测试时候每次产生的结果

public static List<WriteBackData> wdbList = new ArrayList<>();

case中新增写入测试结果

每个case执行的最后阶段增加

WriteBackData wdb = new WriteBackData(sheetIndex, caseInfo.getId(), 8, responseBody);
ExcelUtils.wdbList.add(wdb);

全部测试完成后批量写入结果

使用注解AfterSuite在全部测试结束后将结果写入Excel

@AfterSuite
public void finish() throws Exception {
    ExcelUtils.batchWrite();
}

提取父类

将共性代码放到父类BaseCase

package com.zhongxin.cases;

import com.zhongxin.pojo.WriteBackData;
import com.zhongxin.utils.ExcelUtils;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class BaseCase {
    public int sheetIndex;

    @BeforeClass
    @Parameters({"sheetIndex"})
    public void beforeClass(int sheetIndex) {
        this.sheetIndex = sheetIndex;
    }


    /**
     * 添加回写对象到回写集合中
     */
    public void addWriteBackData(int sheetIndex, int rowNum, int cellNum, String content) {
        WriteBackData wdb = new WriteBackData(sheetIndex, rowNum, cellNum, content);
        ExcelUtils.wdbList.add(wdb);
    }

    @AfterSuite
    public void finish() throws Exception {
        ExcelUtils.batchWrite();
    }
}

提取responseBody参数到UserData中的封装

/**
 * 从responseBody 通过Jsonpath取出对应参数,存到UserData中
 */
public void getParams(String responseBody, String jsonPathExpression, String userDataKey) {
    Object token = JSONPath.read(responseBody, jsonPathExpression);
    if (token != null) {
        UserData.VARS.put(userDataKey, token);
    }
}

提取返回鉴权头

public HashMap<String, String> getAuthorizationHeader() {
    Object token = UserData.VARS.get("${token}");
    HashMap<String, String> headers = new HashMap<>();
    headers.put("Authorization", "Bearer " + token);
    headers.putAll(UserData.DEFAULT_HEADERS);
    return headers;
}

断言

/**
 * 接口响应断言
 * @param expectedResult 断言的期望值
 * @param responseBody   接口响应内容
 * @return 接口响应断言结果
 */
public boolean responseAssert(String expectedResult, String responseBody) {
    Map<String, Object> map = JSONObject.parseObject(expectedResult, Map.class);
    Set<String> keySet = map.keySet();
    boolean reponseAssertFlag = true;
    for (String actualExpression : keySet) {
        Object expectedValue = map.get(actualExpression);
        Object actualValue = JSONPath.read(responseBody, actualExpression);
        if (!expectedValue.equals(actualValue)) {
            reponseAssertFlag = false;
            break;
        }
    }
    System.out.println("断言结果:" + reponseAssertFlag);
    return reponseAssertFlag;
}