BeanShell PostProcessor
【BeanShellPostProcessor簡介】
BeanShellPostProcessor 是一個輕量級的面向Java的腳本語言,借用了JMeter對於BeanShell支持的特性,允許使用標准的Java語法來處理Json數據。
【什么時候需要BeanShellPostProcessor后置處理器】
jmeter后置處理器主要完善測試腳本部分,進行數據關聯,常用的后置處理器有正則表達式提取器(主要用於正則匹配)、JSON Extractor(適用於json提取)、BeanShellPostProcessor,當遇到負責的邏輯提取時BeanShellPostProcessor就比較方便了。比如:數據比較大且返回有多個list,正則表達式提取器需人工去判斷需要取的值是否存在並且在什么位置,效率會有所降低並且容易出錯,針對這種情況可以使用jmeter自帶的功能后置處理器BeanShell PostProcessor,分別提取每個list的值。
【正則表達式與BeanShell的對比】
在jmeter中都可以使用正則表達式和BeanShellPostProcessor用來提取數據,下面是對他們的優缺點進行對比,如圖:
【如何使用BeanShellPostProcessor】
1、 下載fastjson-1.1.6.jar
下載fastjosn.jar包用以解析json數據,下載鏈接如下,然后把jar包放在jmeter文件的lib下就可以了。如圖
2:在測試計划中把jar加載進來
3:添加后置處理器—BeanShell PostProcessor
在需要獲取接口返回的值的接口處添加-后置處理器-BeanShellPostProcessor。
4:在BeanShell PostProcessor里輸入代碼,獲取接口響應數據,進行處理
5:在接口需要傳參的字段引入獲取BeanShell PostProcessor
【一個簡單的例子使用BeanShellPostProcessor獲取數據】
1:例子1-直接獲取接口返回數據,用作參數給其他需要該值的字段使用
1.1:接口響應數據為(解釋一下該接口響應是生成一個簽名串字段):WHxflHqtlf46AduAr509F98hj4vMGrVpC7pIFq9oC1ZXFk2HFWjFwlqKKagrTK3+mnqRs/Vw+Kv0kyfEGDF8iFsgX0e2ksLpQL5m5irGMCe5bIUB+AYY/jjZc943wDM2IY1WjesrGqD83kSNqWywdx6IrFZ/bNadiEq4QJMoeBS8/+PWmPcWNtTA9o3dUPOBpXVIae3bwmoyb8ycCifk2AVX9UnVleQ8zNx1hjHc+o+FhTH9t+Vo6Z8zNkIKK8Zhl23hBNm/jTDqp6u4BFUjNxYKX++6IDfq3Ttglv5heOudSh8+2nzKEeihL61ORG6YmwNjvKrGi27z/WhGTM/EPQ==
1.2:獲取該字段,並把獲取的值存放在cert_sign字段中--BeanShell PostProcessor代碼:
//獲取獲取請求的返回值 String response = prev.getResponseDataAsString(); //日志打印獲取請求的返回值 //log.info(response); vars.put("cert_sign", response)
1.3:在需要引用的cert_sign地方引用即可----引用方式:${cert_sign}
2:例子需要處理的響應數據不復雜,JSON字符串轉JSON對象
2.1:接口響應報文:{"error_code":"0TE1130003000","error_info":"內部服務rpc調用異常"}
2.2:beanshell代碼:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; //獲取獲取請求的返回值 String json = prev.getResponseDataAsString(); //日志打印獲取請求的返回值 log.info("~~~~~~~~~~~~~~~~~~~打印響應報文"+json); JSONObject jso = JSON.parseObject(json); String error_info = jso.getString("error_info"); log.info("~~~~~~~~~~~~~~~~~~~打印獲取的數據error_info"+error_info); vars.put("error_info1", error_info);
3:引用該值
3:例子需要處理的響應數據復雜,響應數據有多個數據list,JSON格式字符串與JSONObject之間的轉換
響應報文:獲取current_beans第一條數據的pay_status{
: "cert_sign":"",
: "current_beans":
: [
: : {
: : : "branch_code":"",
: : : "buyer_pay_amount":"null",
: : : "channel_serial_no":"101159704929076954905",
: : : "child_serial_no":null,
: : : "currency_code":"CNY",
: : : "customer_date_time":"20200810164810",
: : : "customer_serial_no":"",
: : : "goods_desc":"",
: : : "merchant_code":"",
: : : "merchant_fee":"0.010000",
: : : "operator_id":"15900000000",
: : : "order_status":"5",
: : : "pay_bankacct_type":"0",
: : : "pay_source":"6",
: : : "pay_status":"1",
: : : "profit_sharing":"0",
: : : "receipt_amount":"null",
: : : "req_serial_no":"",
: : : "status_msg":"交易成功",
: : : "terminal_code":"",
: : : "third_plat_act":"",
: : : "third_plat_code":"7",
: : : "trad_serial_no":"2020081000000003",
: : : "trade_amount":"0.10",
: : : "trade_type":"7",
: : : "undiscountable_amount":"0.00"
: : },
: : {
: : : "branch_code":"1909291058080031",
: : : "buyer_pay_amount":"null",
: : : "channel_serial_no":"101159704339999354849",
: : : "child_serial_no":null,
: : : "currency_code":"CNY",
: : : "customer_date_time":"20200810150958",
: : : "customer_serial_no":"wql7533019",
: : : "goods_desc":"",
: : : "merchant_code":"1909300907480221",
: : : "merchant_fee":"0.000000",
: : : "operator_id":"15900000000",
: : : "order_status":"6",
: : : "pay_bankacct_type":"0",
: : : "pay_source":"6",
: : : "pay_status":"1",
: : : "profit_sharing":"0",
: : : "receipt_amount":"null",
: : : "req_serial_no":"PJ2020081015095959777329",
: : : "status_msg":"交易成功",
: : : "terminal_code":"1912231916360691",
: : : "third_plat_act":"200001726689",
: : : "third_plat_code":"7",
: : : "trad_serial_no":"2020081000000001",
: : : "trade_amount":"0.01",
: : : "trade_type":"7",
: : : "undiscountable_amount":"0.00"
: : },
: ],
: "current_page":"1",
: "error_code":"0TE110001000",
: "error_info":"交易成功",
: "limit":"10",
: "start":"1",
: "total_count":"14",
: "total_pages":"2",
: "trad_date_time":"20201030145729"
}
2:BeanShell PostProcessor代碼
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; //獲取請求的返回值 String response_data = prev.getResponseDataAsString(); JSONObject responseObj = JSON.parseObject(response_data); // 響應對象 JSONArray currentBeans = responseObj.getJSONArray("current_beans"); vars.put("pay_status",currentBeans.getJSONObject(1).getString("pay_status"));
3:引用該值
總結
以上3個例子都是通過BeanShell PostProcessor處理接口數據,用作關聯,實際對於一般數據量小的數據可以直接使用正則表達式提取器或者JSON Extractor都比較方便快捷,如果數據量大,或者對取值有特殊要求可以考慮使用BeanShell PostProcessor。
如果有了解正則表達式提取器或者JSON Extractor需要可以參考:
1:https://www.cnblogs.com/qiaoli0726/p/13854368.html
2:https://www.cnblogs.com/qiaoli0726/p/13854373.html
備注:FastJson對JSON字符串、JSON對象及JavaBean之間的相互轉換,參考:https://blog.csdn.net/xuforeverlove/article/details/80842148