静态包导入:
RestAssured.baseURI = "xxx" 服务器地址;
RequestSpecification httpRequest = RestAssured.given(); 获取httpRequest对象
int statuscode = response.getStatusCode(); 获取响应码
String xx = response.asString() 转化为字符串
Get请求:#使用then()方法后 返回Response 类型为 ValidatableResponse
Response response = httpRequest.get("/books"); 获取响应报文;
ResponseBody rbody = response.getBody(); 获取响应体
或者:
ResponseBody response = get().body();
参数:
ResponseBody response = given().params(// map).when.get().body(); 通过map 参数较多使用 // param 系方法 中的参数不可以在get(url)中使用
ResponseBody response = given().pathParameters(// map).when.get().body(); //pathParam系方法 中的参数可以在url中使用# url = “ xxx/{param1}/{param2} ”
POST请求:
ResponseBody response = given().qureyparams(// map).when.post().body();
响应体检验:
1.转成jsonPath:
JsonPath jsonPath = response.jsonPath();
jsonPath.get("xxKey") 获取对于的value值
jsonPath.get("key1.key2.key3") 获取节点下面的节点
jsonPath.getList("key1.key2.key3") 获取列表
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"));
// 校验某个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转成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);
// 获取倒数第二个的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自动化测试实战"的title
String title = response.jsonPath().get("books.title.findAll{title ->title=="Selenium 2自动化测试实战"}").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的自动化测试"对应的 author
String title2 = response.jsonPath().get("books.findAll{it.title=="基于Selenium 2的自动化测试"}.author").toString();
System.out.println(title2);
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 值是否为 "电子工业出版社"
.body("books.publisher[0]", is("电子工业出版社"))
// 判断 title 是否等于 5
.body("count", equalTo(5))
.when()
.get("https://api.douban.com/v2/book/search");
// 打印出 response 的body
response.print();