Response结果处理

extract().path()

extract是我们获取返回值的核心,通过它来指明后面需要获取的内容,path()**中的语法同断言时的JsonPath**一致

extract().asString()

有时候我们可能需要获取ResponseBody中的多个值, 利用**extract().asString()**先将响应结果以json字符串的形式保存下来,再一一根据需要获取

extract().getBody().prettyPrint();

获取响应体的消息体进行json格式化的输出

extract().toString()

获取消息体对象

extract().response()

利用extract().response()来讲所有的response信息都保存成一个Response对象,然后在利用各种Response.get方法来获取:

  • 获取所有的Headers:response.getHeaders()

  • 获取某一个header值:response.getHeader(“Content-Type”)

  • 获取status line:response.getStatusLine()

  • 获取status code:response.getStatusCode()

  • 获取cookies: response.getCookies()、response.getCookie(“cookieName”)

rest-assured jsonPath的使用

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
61
62
63
64
package com.test.response;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.oauth2;

/**
* Response消息的获取和解析
*
* @author jingLv
* @date 2020/10/13
*/
class TestGithubApiResponse {

/**
* 设置RestAssured全局配置
*/
@BeforeAll
static void setUp() {
RestAssured.baseURI = "https://api.github.com";
RestAssured.authentication = oauth2("c37acfc546c8be44948e702171d657ad39681795");
}

@Test
void getParseResponse() {
// 获取接口的response
Response response = given().
pathParam("owner", "jinglv").
pathParam("repo", "api-auto-for-java").
when().
get("/repos/{owner}/{repo}");
String resBody = response.getBody().asString();
String resBodyInfo = response.getBody().toString();
System.out.println("消息体:" + resBody);
System.out.println("消息体对象:" + resBodyInfo);

// 响应消息体json格式化输出
response.getBody().prettyPrint();

System.out.println("响应的头信息" + response.getHeaders());
System.out.println("响应状态:" + response.getHeader("status"));
System.out.println("cookie信息:" + response.getCookies());
System.out.println("响应值:" + response.getStatusLine());
System.out.println("响应码:" + response.getStatusCode());
System.out.println("响应的内容类型:" + response.getContentType());
System.out.println("接口响应时间(ms):" + response.getTime());
System.out.println("接口响应时间(s):" + response.getTimeIn(TimeUnit.SECONDS));

// Rest-Assured jsonPath的使用
JsonPath jsonPath = new JsonPath(resBody);
System.out.println("repo ID:" + jsonPath.get("id"));
// 设置根节点为owner
jsonPath.setRoot("owner");
System.out.println("owner ID:" + jsonPath.get("id"));
}
}

获取日志到文件并结合 Allure 报告进行展示

需求与方向

1.问题产生

在使用 Rest-assured 集合 Allure 运行完用例之后,查看生成的报告信息如下:

我们可以看到在生成的报告中只有断言信息,而没有请求的日志信息,而当我们的用例失败时,特别是接口失败时,请求日志是分析原因的第一手资源。

2. 需求产生

其实Rest-assured是有请求日志的,可以通过在given()then()后面加上.log().all()来打印全部的日志信息:

那么问题来了,如何将这打印出来的日志信息都”转移”到 Allure 报告中呢?并且能和用例一一对应起来。

3.思路产生

首先来看一下 Allure 报告可以如何展示日志,在学习 Allure 的过程中发现 Allure有添加附件展示的功能,那么我就直接想到将日志能存入文件然后添加到报告附件不就可以了吗?由此,Allure 端的解决方向确定。

接下来就是要想法办将 Rest-assured 产生的日志存入文件了;

整体思路:

【Rest-assured打印日志】- 【Rest-assured日志存入文件】- 【文件以附件形式传入Allure】- 【Allure展示附件日志】

Allure附件

先看一下Allure 添加附件的两种方法:

@Attachment:在方法上添加注解@Attachment,方法的返回值就会作为附件上传,可添加展示文本和附件类型

1
@Attachment(value = "Page screenshot", type = "image/png")

Allure.addAttachment:通过addAttachment方法指定要添加的附件和展示信息

1
2
3
4
5
6
7
public static void addHttpLogToAllure() {
try {
Allure.addAttachment("接口请求响应日志", new FileInputStream("src/main/resources/test.log"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

保存日志