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

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

回写

将测试结果写回到Excel中

定义回写数据对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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到写入操作代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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,用于存储运行测试时候每次产生的结果

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

case中新增写入测试结果

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

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

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

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

1
2
3
4
@AfterSuite
public void finish() throws Exception {
ExcelUtils.batchWrite();
}

提取父类

将共性代码放到父类BaseCase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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中的封装

1
2
3
4
5
6
7
8
9
/**
* 从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);
}
}

提取返回鉴权头

1
2
3
4
5
6
7
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;
}

断言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 接口响应断言
* @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;
}
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!