ApiPost如何驗證返回數據中是否包含某關鍵字/字符串?使用ApiPost的測試校驗(斷言)能夠很方便的實現這個目標。javascript
看本文前,建議先讀如下apipost官方的文檔:https://doc.apipost.cn/839e6acdc7341451 (ApiPost如何使用測試校驗(斷言))java
實現
假如咱們的返回結果是以下結構:json
{ "errcode": 0, "errstr": "success", "post": { "url": "https://myywt.ar/kzzc" }, "get": [], "request": { "url": "https://myywt.ar/kzzc" }, "put": "url=https%3A%2F%2Fmyywt.ar%2Fkzzc", "header": { "Host": "echo.apipost.cn", "Connection": "keep-alive", "Content-Length": "33", "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN", "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", "Cookie": "PHPSESSID=6bvaf84un0c7j24bksm0htsiob", "Origin": "https://echo.apipost.cn", "User-Agent": "ApiPOST Runtime +https://www.apipost.cn" } }
我想實現校驗返回json的post.url
字段是否含有https://
字符串。怎麼寫斷言呢?api
其實apipost斷言的語法徹底100兼容js語法,js怎麼寫,咱們就怎麼寫就好了:app
apt.assert('response.json.post.url.indexOf("https://") > -1');
其中,response.json.post.url
指的是返回json的post.url
字段(字符串類型),indexOf("https://")
表明該字符串是否含有https://
。indexOf()
是javascript的一個內置方法,即:post
JavaScript indexOf() 的定義和用法
indexOf() 方法可返回某個指定的字符串值在字符串中首次出現的位置。測試
更多預(後)執行腳本經常使用方法請參考:https://mp.apipost.cn/a/c04c8213f3e78865url