一藏研、準備階段
按照開源文檔,做一個樣例
https://github.com/rest-assured/rest-assured/wiki
vi daybreak.json
內(nèi)容是文檔中的json樣例
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
python -m CGIHTTPServer
用python快速生成一個本地8000端口的小網(wǎng)站
訪問127.0.0.1:8000咐汞,daybreak.json文件內(nèi)容如下:
與新建json文件同樣的方法盖呼,新建一個daybreak.xml
<shopping>
<category type="groceries">
<item>
<name>Chocolate</name>
<price>10</price>
</item>
<item>
<name>Coffee</name>
<price>20</price>
</item>
</category>
<category type="supplies">
<item>
<name>Paper</name>
<price>5</price>
</item>
<item quantity="4">
<name>Pens</name>
<price>15</price>
</item>
</category>
<category type="present">
<item when="Aug 10">
<name>Kathryn's Birthday</name>
<price>200</price>
</item>
</category>
</shopping>
訪問127.0.0.1:8000/daybreak.xml,結構如下:
shopping下面有多個不同type的category化撕,每個category下有一個或多個item几晤,item下面有name和price
Tips:(可能遇到的坑)
當使用python -m GCIHTTPServer
時,常常會報錯:
socket.error: [Errno 48] Address already in use
解決方法:
ps -fA | grep python
結果:
...
501 23129 20675 0 7:51下午 ttys001 0:00.99 python -m CGIHTTPServer
501 23815 20675 0 9:54下午 ttys001 0:00.00 grep python
找到對應的進程號植阴,然后kill 進程號
蟹瘾,kill 23129
再重新python -m GCIHTTPServer
二、演練
new一個class掠手,Demo.java
1. 對json的斷言:
方式一:
import org.junit.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
public class Demo {
@Test
public void testJson(){
given().when().get("http://127.0.0.1:8000/daybreak.json")
.then()
.body("store.book.category",hasItems("reference"));
}
}
"store.book.category"
,是昨天提到過的憾朴,根節(jié)點.子節(jié)點.子節(jié)點……
hasItems("reference")
,前面的key對應的value是在這個括號里
方式二:
[ ]
是json中的數(shù)組
book節(jié)點下喷鸽,是四個對立的數(shù)組元素众雷,每個元素中都有category、author、title砾省、price,我要斷言book中的第一個author的值是Nigel Rees鸡岗。
代碼
.body("store.book[0].author",equalTo("Nigel Rees"))
"store.book[0].author"
:book數(shù)組的第1個元素下的author
第2個元素可以用book[1]
……,最后一個元素可以使用book[-1]
方式三:
官方文檔中還有一種方式编兄,叫深度搜索轩性,比如某個子集有n個數(shù)組,找出符合特定條件的翻诉。
store.book.find{ book -> book.price >= 5 && book.price <= 15 }
獲得price在5-15之間的book數(shù)組
用法:
.body("store.book.find{ book -> book.price >= 5 && book.price <= 15}.price",equalTo(8.95f))
2. 對xml斷言
方式一:同json相似的
@Test
public void testXml(){
when().get("http://127.0.0.1:8000/daybreak.xml")
.then()
.body("shopping.category.item[0].name",equalTo("Chocolate"));
}
.body("shopping.category[0].item[0].name",equalTo("Chocolate"));
雖然結果是一樣的炮姨,但是category的含義不一樣
方式二:
shopping.category.item.size()
,返回itms的個數(shù)/長度
用法:.body("shopping.category.item.size()",equalTo("5"));
使用.body("shopping.category.item.size()",equalTo(""));
可以明白category
和category[0]
的區(qū)別
category
有5個item,category[0]
有2個item
方式三:深度檢索
"shopping.category.findAll { it.@type == 'groceries' }.size()"
碰煌,返回type = 'groceries' 的category的size舒岸,是1,使用@
表示屬性芦圾。
示例1:
.body("shopping.category.findAll { it.@type == 'groceries' }.size()",equalTo(1));
示例2:
"shopping.category.findAll { it.@type == 'groceries' }.item.size()",equalTo(2))
:返回上面所述的category.item的size蛾派,是2。
示例3:
.body("shopping.category.item.findAll { it.price == 200 }.name",equalTo("Kathryn's Birthday"));
返回price=200的item的name个少,是"Kathryn's Birthday"
示例4:
.body("**.findAll { it.price == 200 }.name",equalTo("Kathryn's Birthday"));
:shopping.category.item可以換成**
(json path不支持**
)
find
是找第一個洪乍,findAll
是找所有,我理解是it
指帶find前面的內(nèi)容夜焦。
3. 對html的斷言:
昨天的文章中壳澳,在百度搜索mp3的響應,是一個html結構的內(nèi)容茫经,把html也看作是一層一層的節(jié)點巷波。
斷言示例:
.body("html.head.title",equalTo("mp3_百度搜索"));
三、實戰(zhàn)
繼續(xù)xueqiu.com卸伞,對搜索接口進行應用
接口返回結果如下:
{
"q": "sogo",
"page": 1,
"size": 10,
"stocks": [
{
"code": "SOGO",
"name": "搜狗",
"enName": "",
"hasexist": "false",
"flag": null,
"type": 0,
"stock_id": 1029472,
"ind_id": 0,
"ind_name": "通訊業(yè)務",
"ind_color": null,
"_source": "sc_1:1:sogo"
}
]
}
.body("stocks.find{it.code == 'SOGO'}.name",equalTo("搜狗"));
四抹镊、今日份源碼
Demo.java:
import org.junit.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class Demo {
@Test
public void testJson(){
when().get("http://127.0.0.1:8000/daybreak.json")
.then()
.body("store.book.category",hasItems("reference"))
.body("store.book[0].author",equalTo("Nigel Rees"))
.body("store.book.find{ book -> book.price >= 5 && book.price <= 15 }.price",equalTo(8.95f));
}
@Test
public void testXml(){
when().get("http://127.0.0.1:8000/daybreak.xml")
.then()
.body("shopping.category.item[0].name",equalTo("Chocolate"))
.body("shopping.category.item.size()",equalTo(5))
.body("shopping.category.findAll { it.@type == 'groceries' }.item.size()",equalTo(2))
.body("shopping.category.item.findAll { it.price == 200 }.name",equalTo("Kathryn's Birthday"))
.body("**.findAll { it.price == 200 }.name",equalTo("Kathryn's Birthday"));
}
}