使用rest-assrued框架做接口自動(dòng)化測(cè)試,框架的發(fā)掘來自于一次沙龍的學(xué)習(xí),愿意更深入學(xué)習(xí)的同學(xué)可參考:https://testerhome.com/topics/7060
一、常用方法的類
//常用的post、get、when、given等方法所在類
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
//常用的equalTo、hasItems等斷言方法所在類
org.hamcrest.Matchers.*
//如果要使用Json Schema Validation對(duì)返回的response結(jié)果進(jìn)行結(jié)果判斷,需要導(dǎo)入該類
io.restassured.module.jsv.JsonSchemaValidator.*
#另外,官方文檔說明:為了提高使用rest-assured的效率,推薦靜態(tài)導(dǎo)入以上方法
二、應(yīng)用場(chǎng)景
首先,做了一些接口之后發(fā)現(xiàn)有很多同類型的請(qǐng)求如:get請(qǐng)求,其基本語法都是一致的,只不過在請(qǐng)求的時(shí)候傳遞的參數(shù)有所不同,所以在工程中新建了一個(gè)特定的工具類,將所有的請(qǐng)求都寫到該類中,這樣只需要在該類中區(qū)分不同請(qǐng)求方式、不同請(qǐng)求參數(shù)的請(qǐng)求即可,在測(cè)試主函數(shù)中,我們只需要調(diào)用該類中的相關(guān)應(yīng)方法,傳遞URL、參數(shù)及信cookies等信息。
下面是我工具類的一些方法,或存在不完善的地方也只是初步學(xué)習(xí),諒解:
public class JJApiTools {
/**類型一:
* 請(qǐng)求參數(shù)為json數(shù)據(jù)結(jié)構(gòu)的post請(qǐng)求
*/
public static Response post(String apiPath, String jsonData){
Response response = given().
contentType("application/json;charset=UTF-8").
body(jsonData).
when().log().all().post(apiPath.trim());
return response;
}
/**類型二:
* 請(qǐng)求無參數(shù)結(jié)構(gòu)的post請(qǐng)求
*/
public static Response post(String apiPath){
Response response = given().contentType("application/json; charset=UTF-8").
headers("headers1", "value1", "header2", "value2").
cookies("cookies1","value1", "cookies2", "value2").
expect().statusCode(200).
when().post(apiPath.trim());
return response;
}
/**類型三:
* 請(qǐng)求有多個(gè)參數(shù)結(jié)構(gòu)的post請(qǐng)求
*/
//在請(qǐng)求URL中如果有很多個(gè)參數(shù),可以用map進(jìn)行傳遞,類似于字典
public static Response mapPost(String apiPath, Map map){
Response response = given().params(map).
expect().statusCode(200).
when().post(apiPath);
return response;
}
/**類型三:
* 含有cookie的post請(qǐng)求
*/
public static Response cookiesPost(String apiPath, String cookies){
System.out.println("Cookie = "+cookies);
Response response = given().contentType("application/json; charset=UTF-8").
cookies("web-session", cookies).
expect().statusCode(200).
when().
//log().all().
post(apiPath.trim());
return response;
}
/**類型三:
* get請(qǐng)求
*/
public static Response get(String apiPath) {
Response response = given().contentType("application/json; charset=UTF-8").
//headers("headers1", "value1", "headers2", "values2").
//cookies("cookies1", "values1", "cookies2", "values2").
expect().statusCode(200).
when().get(apiPath.trim());
System.out.println("_________________________________________");
return response;
}
public static Response get(String apiPath, String cookie, Headers headers) {
System.out.println("Cookie = "+cookie);
Response response = given().contentType("application/json; charset=UTF-8").
headers("Headers", headers).
cookies("web-session", cookie).
when().log().all().get(apiPath.trim());
//given().contentType("application/json").when().get("/xxx").then().body();
return response;
}
public static Response get(String apiPath, String cookie) {
System.out.println("Cookie = "+cookie);
Response response = given().contentType("application/json; charset=UTF-8").
cookies("web-session", cookie).
expect().statusCode(200).
when().get(apiPath.trim());
//given().contentType("application/json").when().get("/xxx").then().body();
return response;
}
public static Response get(String apiPath, Map map, String cookie){
Response response = given().
contentType("application/json; charset=UTF-8").
params(map).expect().statusCode(200).
when().get(apiPath.trim());
return response;
}
}
#對(duì)上面方法中的函數(shù)作用記錄一下:
log().all() :會(huì)在控制臺(tái)打印request的相關(guān)信息,出現(xiàn)錯(cuò)誤時(shí)可以用來做調(diào)試
trim():情況URL中的空格
expect().statusCode(200):斷言返回的響應(yīng)碼是不是200
以上不同情況的請(qǐng)求,可以相互組合使用,因?yàn)槟壳拔业慕涌谛枨笠簿瓦@些,我也就考慮到了這些,如果有更多的需求歡迎留言討論!