将测试结果写回到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 wdbList = new ArrayList<>();
每个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 通过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 getAuthorizationHeader() {
Object token = UserData.VARS.get("${token}");
HashMap 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 map = JSONObject.parseObject(expectedResult, Map.class);
Set 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;
}