利器

上一篇文章中介绍了rest-assured对返回结果的断言,最后说明了对于Response结果导出的需求。可查看往期文章进行查看。

HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 13 Jan 2020 02:15:11 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Expires: 0
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
X-XSS-Protection: 1; mode=block
Pragma: no-cache
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Proxy-Connection: keep-alive{"code": 1,"msg": null,"data": {"tenant_id": 6,"userType": "1","dept_id": 0,"user_id": 6,"username": "xxx","jti": "afeb93f8-e4e4-4c15-955b-90cee130c4c7","access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.exxxzciLCJjbGllbnRfaWQiOiJzeXN0ZW0iLCJ1c2VybmFtZSI6InFpbnpoZW4ifQ.6NQmjJp_9XSveOaATNLjtTktWe6_WjHY0o9NbBUdDx8","expires_in": 9999999,"token_type": "bearer"}...}
@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().path("data.user_id");System.out.println("返回id的值是:"+id);}

运行结果:

extract().asString()
有时候我们可能需要获取ResponseBody中的多个值,例如我们现在想要获取返回体body中的dept_id和user_id,我们就可以利用extract().asString()先将响应结果以json字符串的形式保存下来,再一一根据需要获取,具体写法如下:

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().asString();System.out.println("返回body的值是:"+json);System.out.println("获取user_id的值是:"+ from(json).get("data.user_id"));System.out.println("获取dept_id的值是:"+ from(json).get("data.dept_id"));}

运行结果:

extract().response()
上面都是对响应体的结果进行导出,但是实际工作中我们的需求远不止于此,我们可能还需要响应头等信息,例如一些接口的Token、就可能会在响应信息的Header中返回;
这个时候就可以利用extract().response()来讲所有的response信息都保存成一个Response对象:

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().response();System.out.println("返回response是:"+response);} 

运行结果:

然后在利用各种Response.get方法来获取。
1)获取所有的Headers

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().response();System.out.println("返回headers是:\n"+response.getHeaders());}

运行结果:

2)获取某一个header值
类似key,value的结构,使用getHeader(“headerName”)即可,例如我们这里要获取Content-type的值:

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().response();System.out.println("返回Content-Type是:\n"+response.getHeader("Content-Type"));}

运行结果:

3)获取status line——getStatusLine()

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().response();System.out.println("返回StatusLine是:\n"+response.getStatusLine());}

运行结果:

4)获取status code——getStatusCode()

@Test
void login(){.. .when().log().all().post(".133/auth/oauth/token").then().log().all().statusCode(200).body("code",equalTo(1)).extract().response();System.out.println("返回StatusCode是:\n"+response.getStatusCode());}

运行结果:

5)获取cookies——getCookies()、getCookie(“cookieName”)
rest-assured还为我们提供了方便的获取cookie的方法;因本例中无cookies返回,所以仅展示代码语法,有需要的可自行测试或参考官方文档

// Get all cookies as simple name-value pairs
Map<String, String> allCookies = response.getCookies();
// Get a single cookie value:
String cookieValue = response.getCookie("cookieName");

上述这些已几乎可满足日常工作所需,如有需要可在官网进一步研究,官网还提供了获取同名多值的header和cookie等方法:

相关参考链接:
RESTAssured 官方文档:

对于想系统进阶提升测试开发技能的同学,推荐霍格沃兹测试学院出品的 《测试开发从入门到高级实战》系统进阶班课程。