自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

rest-assured對response結果的導出

這裡將繼續研究rest-assured對response結果的導出獲取,現有一個登錄接口auth/oauth/token,接口的部分返回值如下:

<code>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"
}
...
}/<code>

語法演示


注:因這篇文章主要目的是演示返回值的導出功能,所以演示代碼中將省略請求參數部分,如需瞭解可參考文章往期文章。


extract().path()
extract是我們獲取返回值的核心,通過它來指明後面需要獲取的內容,path()中的語法同斷言時的JsonPath一致,例如我們現在要獲取user_id,寫法如下:

<code>@Test
void 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);
}/<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

extract().asString()有時候我們可能需要獲取ResponseBody中的多個值,例如我們現在想要獲取返回體body中的dept_id和user_id,我們就可以利用extract().asString()先將響應結果以json字符串的形式保存下來,再一一根據需要獲取,具體寫法如下:

<code>@Test
void 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"));
}/<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出


注:這裡用到的from是rest-assured的,不要導錯包了:import static io.restassured.path.json.JsonPath.from;


extract().response()上面都是對響應體的結果進行導出,但是實際工作中我們的需求遠不止於此,我們可能還需要響應頭等信息,例如一些接口的Token、就可能會在響應信息的Header中返回;

這個時候就可以利用extract().response()來講所有的response信息都保存成一個Response對象:

<code>@Test
void 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);
} /<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

然後在利用各種Response.get方法來獲取。

1)獲取所有的Headers

<code>@Test
void 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());
} /<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

2)獲取某一個header值類似key,value的結構,使用getHeader("headerName")即可,例如我們這裡要獲取Content-type的值:

<code>@Test
void 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"));
}/<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

3)獲取status line——getStatusLine()

<code>@Test
void 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());
}/<code>

運行結果:

自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出

4)獲取status code——getStatusCode()

<code>@Test
void 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());
}/<code>

運行結果:


自動化測試框架 RESTAssured 實踐(三):對 Response 結果導出


5)獲取cookies——getCookies()、getCookie(“cookieName”)rest-assured還為我們提供了方便的獲取cookie的方法;因本例中無cookies返回,所以僅展示代碼語法,有需要的可自行測試或參考官方文檔

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


分享到:


相關文章: