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

HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 13 Jan 2020 02:15:11 GMTContent-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedExpires: 0Cache-Control: no-cache, no-store, max-age=0, must-revalidateX-XSS-Protection: 1; mode=blockPragma: no-cacheX-Frame-Options: DENYX-Content-Type-Options: nosniffProxy-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"        }        ... }
@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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字符串的模式保留下来,再一一依据须要获取,具体写法如下:

@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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对象:

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

运行后果:

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

@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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的值:

@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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()

@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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()

@Testvoid login(){     .. .     when()             .log().all().post("http://47.xxx.xxx.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 pairsMap<String, String> allCookies = response.getCookies();// Get a single cookie value:String cookieValue = response.getCookie("cookieName");

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

相干参考链接:
RESTAssured 官网文档:
https://github.com/rest-assur...

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