安裝就不多介紹
本文主要講解beanshell 如何將接口返回數據 數據處理后用于其他接口提供參數 完成自動化測試閉環。希望能幫助到大家!后端語言環境為PHP
獲取JSON 對象中的鍵

?
?
?獲取上圖中1的步驟如下
1.添加?BeanShell 后置處理程序
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;//此包沒有的話 百度一下
//獲取返回數據 字符串類型
String response_data = prev.getResponseDataAsString();
log.info(response_data);
// 字符串轉對象
JSONObject data_obj = JSON.parseObject(response_data);
//獲取到目標對象 {"1":"在線支付","0":"對公支付"}}
JSONObject jsonObject = data_obj.get("result").get("data");
//聲明數組變量
JSONArray jsonArray = new JSONArray();
//遍歷對象
for (Map.Entry entry : jsonObject.entrySet()) {
//添加進數組
jsonArray.add(entry.getKey());
}
log.info(jsonArray.toString());
//添加屬性供全線程組使用
props.put("_expectPayment", jsonArray);
?上步驟存入了數組變量 ["0","1"] 供其他接口使用
2.在需要使用此數據的接口 添加?BeanShell 預處理程序
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
//獲取數組
JSONArray jsonArray = props.get("_expectPayment");
//獲取數組中隨機值 即獲取到了目標值 0或1
vars.put("expectPayment",jsonArray.get((int)(Math.random()*jsonArray.size())));
log.info(vars.get("expectPayment"));
即可通過 ${expectPayment}獲取到0或1 完成自動化測試閉環。
本文摘自 :https://www.cnblogs.com/

