原文地址https://blog.csdn.net/lijing742180/article/details/81157947
原文地址https://blog.csdn.net/zailushangbuting/article/details/70808612
原文地址https://blog.csdn.net/maybe_frank/article/details/79094230
原文地址https://www.cnblogs.com/vv00cc/p/7902693.html
一、響應斷言
1、什么是響應斷言?
在jmeter中最常用的斷言方法是“響應斷言”,它是通過檢查sampler的請求內容和響應結果中是否匹配指定的字符串來判斷結果是否正確。
響應斷言簡單實用,能夠解決工作中的大部分問題。
尤其是在jmeter4.0版本中在響應斷言中加入了Request Headers、Request Data,使得能夠對請求頭和請求體進行斷言,滿足了多樣性的斷言需求,非常方便。
2、使用響應斷言
響應斷言的使用非常簡單,通常需要關注如下三點:

“要測試的響應字段”
指我們要進行斷言的內容所在的位置,分為request和response兩大塊,根據實際情況選擇即可(最常用的是響應文本)
“模式匹配規則” (此處提到的1和3對應上圖中的數字)
控制上圖中的“1”如何匹配“3”
"包括"-------------1包括3,支持正則;
“匹配”-------------1完全匹配3,支持正則;
“Equals”---------1完全匹配3中的文本內容,不支持正則,且大小寫敏感;
“Substring”-----1包括3中的文本內容,不支持正則,且大小寫敏感;
“否”、“或者”----跟前面四個選項結合使用,分別用於邏輯取反、取或。
“要測試的模式”
這里填寫我們要進行斷言的內容,可以添加多個模式,可以使用變量、文本、正則表達式(在“包括”和“匹配”模式下)。
二、BeanShell斷言
1、什么是BeanShell斷言?
BeanShell斷言可以使用beanshell腳本來執行斷言檢查,可以用於更復雜的個性化需求,使用更靈活,功能更強大,但是要能夠熟練使用beanshell腳本。
在這里除了可以使用beanshell的內置變量外,主要通過 Failure 和 FailureMessage來設置斷言結果。
下面看一個簡單的示例:

其中腳本內容如下:
if ("200".equals(""+ResponseCode) == false ) { // 響應碼不等於200時,設置斷言失敗,並輸出失敗信息 Failure=true ; FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ; print ( "the return code is " + ResponseCode); // this goes to stdout log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file } else { // 響應碼等於200時,設置斷言成功,並輸出成功信息 Failure=false; FailureMessage = "Return true, and the response code was " + ResponseCode; } }
Bean Shell常用內置變量
JMeter在它的BeanShell中內置了變量,用戶可以通過這些變量與JMeter進行交互,其中主要的變量及其使用方法如下:
log:寫入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);
ctx:該變量引用了當前線程的上下文,使用方法可參考:org.apache.jmeter.threads.JMeterContext。
vars - (JMeterVariables):操作jmeter變量,這個變量實際引用了JMeter線程中的局部變量容器(本質上是Map),它是測試用例與BeanShell交互的橋梁,常用方法:
a) vars.get(String key):從jmeter中獲得變量值
b) vars.put(String key,String value):數據存到jmeter變量中
更多方法可參考:org.apache.jmeter.threads.JMeterVariables
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
=============================================================================================================================================
=============================================================================================================================================

2、編寫斷言判斷代碼:
if("206".equals("${action_seq_1}") && "3".equals("${trans_status_1}")){ Failure = false; FailureMessage = "交易成功!"; }else if(!"206".equals("${action_seq_1}")){ Failure = true; FailureMessage = "交易類型不正確!"; }else if(!"3".equals("${trans_status_1}")){ Failure = true; FailureMessage = "交易未成功!"; }

=============================================================================================================================================
=============================================================================================================================================
BeanShell是jmeter的解釋型腳本語言,和java語法大同小異,並有自己的內置對象和方法可供使用。
vars:操作jmeter的變量:vars.get(String parmStr) 獲取jmeter的變量值;vars.put(String key,String value) 把數據存到Jmeter變量中;
prev:獲取sample返回的信息,prev.getResponseDataAsString() 獲取響應信息;prev.getResponseCode() 獲取響應狀態嗎;
從這里可以參考更多:https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
1、添加一個線程組,並依次加上需要的功能組件,右鍵http請求添加一個BeanShell Assertion。
2、我們把json.jar引入,可以拷貝到jmeter的lib\ext目錄下,也可以在計划任務引入jar包


import org.json.*; String responsData = prev.getResponseDataAsString(); JSONObject responseJson = new JSONObject(responsData); String status = responseJson.getString("code").toString(); if(!status.equals("0")){ Falure = True; }
=============================================================================================================================================
=============================================================================================================================================
解析response中的內容,並把獲取到的value傳遞到后續的request中,常用的方法就是在想要解析response的request上添加后置處理器
本章介紹兩種常用的組件
BeanShell PostProcessor
JSON Extractor
下面是具體的操作步驟:
添加后置處理器:BeanShell PostProcessor
獲取response中的字符串,並對內容進行判斷,當response中包含“The wait operation timed out”或者“Oops. Something went wrong ... sorry”時,都認為該request的response不正確
String response = prev.getResponseDataAsString(); String code = prev.getResponseCode(); log.info("Respnse is " + response); log.info(code); int result1 = response.indexOf("The wait operation timed out"); int result2 = response.indexOf("Oops. Something went wrong ... sorry"); if(code == "200" && result1>=0 || result2>=0){ FailureMessage = "OK,check current"; } else{ Failure = true; FailureMessage = "ERROR,check error"; }
解析返回的Jason數據,獲取name字段的值賦給變量result
{“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}}
import org.json.*; 腳本中的導入的json包需要自己去網絡下載后放到\lib\ext下 String response_data = prev.getResponseDataAsString(); JSONObject data_obj = new JSONObject(response_data); String apps_str = data_obj.get("body").get("apps").toString(); JSONArray apps_array = new JSONArray(apps_str); String[] result = new String[apps_array.length()]; for(int i=0;i<apps_array.length();i++){ JSONObject app_obj = new JSONObject(apps_array.get(i).toString()); String name = app_obj.get("name").toString(); result[i] = name; } vars.put("result", Arrays.toString(result));
添加后置處理器:JSON Extractor
解析返回的Jason數據,獲取name字段的值帶入參數到后邊的request中
{“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}}

變量t1的值是111,變量t2的值是222
