API自動化測試方式非常多,可謂百花齊放,各種做法有各自的優(yōu)勢,那么今天我們簡單介紹一種,通過Rest Assured 和TestNG 結(jié)合實現(xiàn)API自動化測試。
環(huán)境搭建
新建個maven 工程,直接在pom文件配置如下,導(dǎo)入Rest Assured和TestNG相應(yīng)的包,可以根據(jù)實際情況導(dǎo)入最新版本。
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
Demo 演示
Get Demo 演示
這里我們以簡書首頁的推薦作者API為例,該API為Get請求,請求地址和參數(shù):http://www.itdecent.cn/users/recommended?seen_ids=&count=5&only_unfollowed=true
那么我們用Rest Assured 要模擬這個請求,
首先我們需要先導(dǎo)入靜態(tài)方法:
import static io.restassured.RestAssured.*;
然后我們編寫我們的Case:
@Test()
public void getHttpTest() {
Response response = given()
.get("http://www.itdecent.cn/users/recommended?seen_ids=&count=5&only_unfollowed=true");
// 打印出 response 的body
response.print();
}
https協(xié)議Demo
假設(shè)https協(xié)議,我們以豆瓣搜索圖書API為例,詳細API如下圖所示:

那么上個case我們把參數(shù)直接跟在URL后面發(fā)起請求,下面我們把參數(shù)剝離出來,使用.params(“key”,"value","key","value"....),同時因為是https我們需要加上ssl的配置,讓所有請求支持所有的主機名:
@Test()
public void getHttpsTest() {
Response response = given()
// 配置SSL 讓所有請求支持所有的主機名
.config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
.params("q", "自動化測試", "start", 0, "count", 2)
.get("https://api.douban.com/v2/book/search");
// 打印出 response 的body
response.print();
}
請求的參數(shù)除了可以用.params(“key”,"value","key","value"....)把所有參數(shù)都放在parames()里面,也可以用parame("key","value")形式。
@Test()
public void getHttpsTest2() {
Response response = given()
.config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
.param("q", "自動化測試")
.param("start", 0)
.param("count", 2)
.get("https://api.douban.com/v2/book/search");
// 打印出 response 的body
response.print();
}
Post Demo
POST請求我們以登陸API為例,
請求方法:POST
協(xié)議:HTTP
請求地址:http://XXX.XXXX.com/Home/Login
請求參數(shù):UserName=xxx&Password=********&CheckCode=&Remember=false&LoginCheckCode=7505
請求Content-Type類型為:application/x-www-form-urlencoded
那么實現(xiàn)如下:
@Test
public void postTest2() {
Response response = given()
// 設(shè)置request Content-Type
.contentType("application/x-www-form-urlencoded")
// 請求參數(shù) 放body
.body("UserName=XXXX&Password=XXXXXX&CheckCode=&Remember=false&LoginCheckCode=7505")
// POST 請求
.post("http://XXXX.XXXX.com/Home/Login");
response.print();
}
當(dāng)然如果請求Content-Type類型是:application/x-www-form-urlencoded,我們也可以直接用param()或者params()管理,但是如果是
application/json 則只能用body()管理參數(shù)。
@Test
public void postTest3(){
Response response = given()
.contentType("application/x-www-form-urlencoded")
.param("CheckCode", "")
.param("LoginCheckCode", "3719")
.param("Password", "XXXXX")
.param("Remember", "true")
.param("UserName", "XXXXX")
.post("http://XXXX.XXXXX.com/Home/Login");
response.print();
}
設(shè)置header,cookie
header和cookie的設(shè)置類似param,存在header()、headers()、cookie()、cookies()方法,使用也跟param類似:
Response response = given()
.cookie("cookie","value")
.cookies("cookiename1", "value1", "cookiename2", "value2")
.header("Accept-Encoding:", "gzip, deflate")
.headers("header1","value1","header2","value2")
.get("XXXXXXX");
獲取Response 狀態(tài)碼:StatusCode
獲取狀態(tài)碼,返回int類型:
response.getStatusCode();
獲取Response Cookies
獲取具體的某個cookies:
response.getCookie("cookiesName")
獲取所有的cookies,返回一個map:
Map cookies = response1.getCookies();
Response headers 常用操作
- 獲取所有headers:
Headers headers = response.getHeaders();
- 獲取指定header:
response.getHeader("header name");
// 等同上面方法
Headers headers = response.getHeaders();
headers.get("header name");
- 判斷某個header是否存在,返回boolean類型
Headers headers = response.getHeaders();
headers.hasHeaderWithName("XXX")
- 判斷header是否為空
Headers headers = response.getHeaders();
headers.exist()
獲取Response body
獲取Response body,并轉(zhuǎn)成String類型:
response.getBody().asString()
完整例子跟如下JSON解析一起給出。
解析JSON
Rest Assured 自帶支持對JSON、xml的解析,例如我們還是以豆瓣圖書查詢API為例,假設(shè)我們請求參數(shù)中q= “自動化測試”,start=0,count=5那么我們模擬這個請求并做簡單JSON解析:
@Test
public void jsonParsed() {
Response response = given()
.config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
.params("q", "自動化測試", "start", 0, "count", 5)
.get("https://api.douban.com/v2/book/search");
// 打印出 response 的body
response.print();
int statusCode = response.getStatusCode();
System.out.println("statusCode:" + statusCode);
// 獲取Response 的所有 headers 并輸出
Headers headers = response.getHeaders();
System.out.println(headers.toString());
// 獲取Response中header名為Content-Type的值
String contentType = response.getHeader("Content-Type");
System.out.println("contentType:" + contentType);
// 等同上面方法
System.out.println(headers.get("Content-Type"));
// 校驗?zāi)硞€Header 是否存在
System.out.println(headers.hasHeaderWithName("fasdfaf"));
System.out.println(headers.hasHeaderWithName("Server"));
// 如果Response 的headers不為空則返回true
System.out.println(headers.exist());
Map<String, String> cookiesMap = response.cookies();
for (String key : cookiesMap.keySet()) {
System.out.println(key + ":" + cookiesMap.get(key));
}
System.out.println(response.cookie("bid"));
// 把Response 的body轉(zhuǎn)成string類型
System.out.println(response.getBody().asString());
int count = response.jsonPath().getInt("count");
System.out.println("count:" + count);
// 獲取所有的 subtitle
ArrayList<String> subtitles = response.jsonPath().get("books.subtitle");
for (int i = 0; i < subtitles.size(); i++) {
System.out.println(subtitles.get(i));
}
// 獲取特定某個的subtitle
String subtitle = response.jsonPath().get("books.subtitle[0]");
System.out.println(subtitle);
// 獲取倒數(shù)第二個的subtitle
String subtitle1 = response.jsonPath().get("books.subtitle[-2]");
System.out.println(subtitle1);
// 獲取特定tags底下的所有title
ArrayList<String> tagTitle = response.jsonPath().get("books.tags[2].title");
for (int i = 0; i < tagTitle.size(); i++) {
System.out.println(tagTitle.get(i));
}
// 獲取所有的 title
ArrayList<ArrayList<String>> tagTitles = response.jsonPath().get("books.tags.title");
for (int i = 0; i < tagTitles.size(); i++) {
for (int j = 0; j < tagTitles.get(i).size(); j++) {
System.out.println(tagTitles.get(i).get(j));
}
System.out.println("---------------------");
}
// 獲取Response json里面所有title = "Selenium 2自動化測試實戰(zhàn)"的title
String title = response.jsonPath().get("books.title.findAll{title ->title==\"Selenium 2自動化測試實戰(zhàn)\"}").toString();
System.out.println(title);
// 獲取Response json中 1< numRaters <=20的所有 numRaters
String numRaters = response.jsonPath().get("books.rating.numRaters.findAll{numRaters -> numRaters>1 && numRaters<=20}").toString();
System.out.println(numRaters);
// 獲取Response json種title = "基于Selenium 2的自動化測試"對應(yīng)的 author
String title2 = response.jsonPath().get("books.findAll{it.title==\"基于Selenium 2的自動化測試\"}.author").toString();
System.out.println(title2);
}
校驗Response 是否達到預(yù)期
Rest Assured提供了類似hasITems ,is,equalTo等靜態(tài)方法供使用,使用時先導(dǎo)入方法:
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
@Test
public void testDemo1() {
Response response = given()
.config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
.params("q", "自動化測試", "start", 0, "count", 5)
.expect()
// 判斷 title是否包含了 自動化 和 自動化測試
.body("books.tags[2].title", hasItems("自動化", "自動化測試"))
// 判斷 count 值是否為 5
.body("count", is(5))
// 判斷 publisher 值是否為 "電子工業(yè)出版社"
.body("books.publisher[0]", is("電子工業(yè)出版社"))
// 判斷 title 是否等于 5
.body("count", equalTo(5))
.when()
.get("https://api.douban.com/v2/book/search");
// 打印出 response 的body
response.print();
}
盡管Rest Assured 提供了校驗方法,但是在實際API自動化測試過程中,因為往往需要校驗的字段非常多,建議還是直接先把要校驗的JSON字段解析出來,再通過TestNG提供的Assert類進行校驗。
補充個例子
Web API 里面非常多的API發(fā)起請求都需要在用戶登陸下才能發(fā)起,而判斷用戶是否登錄往往就是去判斷cookies,所以如果遇到這類問題,只需要在需要模擬的API中設(shè)置cookies為登錄API生成的cookies便可。
例如下demo,第二個請求通過的前提是拿到第一個請求的cookies,那么我們就把第一個請求的response中的所有cookies放到第二個請求作為request:
@Test
public void postHttpsTest3() {
Response response1 = given()
.contentType("application/x-www-form-urlencoded")
.body("UserName=XXXX&Password=XXXXX&CheckCode=&Remember=false&LoginCheckCode=7505")
.post("http://XXXX.XXXX.com/Home/Login");
response1.print();
// 獲取reponse中所有的cookies
Map cookies = response1.getCookies();
Response response2 = given()
// 寫入第一個請求的cookies
.cookies(cookies)
.contentType("application/x-www-form-urlencoded")
.param("ActionName", "")
.param("CurrentUserNo", "XXXXX")
.post("http://XXXX.XXXXX.com/Home/IsCurrentAccountValid");
response2.print();
}
更多幫助
官方網(wǎng)站:http://rest-assured.io/
gitHub:https://github.com/rest-assured/rest-assured
幫助文檔:https://github.com/rest-assured/rest-assured/wiki/Usage
javadoc:http://www.javadoc.io/doc/io.rest-assured/rest-assured/3.0.2