在作接口測試時,對響應數據的校驗是很是重要的部分;在使用Jmeter進行接口測試時,有多種respone校驗方式,好比響應斷言、BeanShell斷言等等,BeanShell斷言能夠自定義斷言,自由靈活的用腳本實現斷言。java
小型嵌入式Java源代碼解釋器,具備對象腳本語言特性,可以動態地執行標準JAVA語法
運行其內部的腳本處理Java應用程序,還能夠在運行過程當中動態執行你java應用程序執行java代碼,由於BeanShell是用java寫的,運行在同一個虛擬機的應用程序,所以能夠自由地引用對象腳本並返回結果。shell
JMeter在它的BeanShell中內置了變量,用戶能夠經過這些變量與JMeter進行交互,其中主要的變量及其使用方法以下:apache
log:寫入信息到jmeber.log文件,使用方法:log.info(「This is log info!」);json
ctx:該變量引用了當前線程的上下文,使用方法可參考:org.apache.jmeter.threads.JMeterContext。數組
vars - (JMeterVariables):操做jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質上是Map),它是測試用例與BeanShell交互的橋樑,經常使用方法:測試
a) vars.get(String key):從jmeter中得到變量值spa
b) vars.put(String key,String value):數據存到jmeter變量中線程
更多方法可參考:org.apache.jmeter.threads.JMeterVariablescode
props - (JMeterProperties - class java.util.Properties):操做jmeter屬性,該變量引用了JMeter的配置信息,能夠獲取Jmeter的屬性,它的使用方法與vars相似,可是隻能put進去String類型的值,而不能是一個對象。對應於java.util.Properties。對象
a) props.get("START.HMS"); 注:START.HMS爲屬性名,在文件jmeter.properties中定義
b) props.put("PROP1","1234");
prev - (SampleResult):獲取前面的sample返回的信息,經常使用方法:
a) getResponseDataAsString():獲取響應信息
b) getResponseCode() :獲取響應code
更多方法可參考:org.apache.jmeter.samplers.SampleResult
sampler - (Sampler):gives access to the current sampler
假如咱們有以下的response數據:
{ "message": "不能發送小於當前時間點的定時任務", "statusCode": 200 }
(1)咱們使用JSONObject對象來獲取json數據,首先須要下載org.json的jar包,而後在測試計劃中導入該jar包,並在jmeter的lib目錄下放入該jar包,下面驗證statusCode的值是否等於200:
import org.json.*; //獲取上一個請求的返回 String jsonString = prev.getResponseDataAsString(); JSONObject responseJson = new JSONObject(jsonString); //判斷返回值是否和預期一致 if (responseJson.getInt("statusCode") != 200) { //把斷言失敗置爲真,即用例失敗,並在結果樹中顯示FailureMessage Failure = true; FailureMessage = "statusCode的返回值有誤"; }
(2)若是要驗證 respone 中 message 的值是否與預期一致,須要怎麼作呢?
import org.json.*; //獲取上一個請求的返回 String jsonString = prev.getResponseDataAsString(); JSONObject responseJson = new JSONObject(jsonString); String fbpcontent = responseJson.getString("message"); if (!fbpcontent.equals("不能發送小於當前時間點的定時任務")) { //把斷言失敗置爲真,即用例失敗,並在結果樹中顯示FailureMessage Failure = true; FailureMessage = "message與實際值不一致"; }
假如咱們有以下的response響應數據:
{ "statusCode": 200, "data": [ { "i": "50356", "n": "項目一", "v": "2.0", "iconUrl": "", }, { "i": "45280", "n": "項目二", "v": "3.0", "iconUrl": "", }, { "i": "14656", "n": "項目三", "v": "2.6", "iconUrl": "", }, { "i": "66213", "n": "項目四", "v": "5.0", "iconUrl": "", } ] }
(3)咱們須要解析數組data的值,如何去解析呢?
import org.json.*; import java.util.Arrays; //獲取上一個請求的返回 String jsonContent = prev.getResponseDataAsString(); JSONObject response = new JSONObject(jsonContent); JSONArray groups = response.getJSONArray("data"); String strData= groups.toString(); log.info(strData)
如今有更加複雜格式的respone數據:
{ "priorityGroups": { "proId": 1234, "name": "項目一", "groups": [ { "id": "50356", "items": [ { "proId": 1360, "n": "PC端", "index": 1 }, { "proId": 1361, "n": "iOS端", "index": 2 }, { "proId": 1362, "n": "安卓端", "index": 4 } ] } ] }, "promotion": { "proId": 1364, "cusId": 84, "name": "項目二", "from": 1470821215, "to": 1470907615, "status": 1, "objectId": 1069, "createBy": 394, "eff": 1470821215000, "createTime": 1470821155000 } }
(4)咱們須要解析groups中的數據,須要怎麼實現呢?
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; String jsonContent = prev.getResponseDataAsString(); JSONObject response = new JSONObject(jsonContent); JSONArray groups = response.getJSONObject("priorityGroups").getJSONArray("groups"); String strGroups = groups.toString();