JMeter組件之BeanShell PostProcessor的使用


1. 場景一:獲取請求響應中的數據,並保存

import com.alibaba.fastjson.*;  // 引入包。這個包需要先放在:<安裝目錄>\apache-jmeter-3.2\lib\ext中

// 獲取數據

String response = prev.getResponseDataAsString();  // 獲取Response

JSONObject responseObj = JSON.parseObject(response);  // 整個Response作為JSON對象

JSONObject resObj = responseObj.getJSONObject("res");  // 獲取某一部分對象。即,json串中{}的內容

JSONArray listArray = resObj.getJSONArray("list");  // 獲取列表。即,json串中[]的內容

// 保存數據

// 1) 列表

int len = listArray.size();

for(int i=0; i<len; i++){

    temp[i] = listArray.get(i).getString("name");

}

vars.put("names", Arrays.toString(temp));  // 保存到JMeter變量中,vars.put只能保存字符串,因此這里使用了toString()進行轉換

// 2) String類型

String id = resObj.get("id");  // 獲取對象中某一字符串的內容。

vars.put("id", id);  // 保存到JMeter變量中,后面的組件可以使用

// 3) Integer類型

Integer no = resObj.get("id"); // 獲取對象中某一整型的內容。

vars.put("no", no.toString());   // 保存到JMeter變量中,vars.put只能保存字符串,因此這里使用了toString()進行轉換

2. 場景二:獲取數據庫中查詢到的數據,並保存

前提:在JDBC Request中已經獲取到數據,詳見《JDBCRequest的用法》。

select total_cash from t_cash where user_name="tom";

在Result variable name中的值為:result

String cash_new = vars.getObject("result").get(0).get("total_cash").toString();

vars.put("cash_new", cash_new);  // 操作前與此步驟相同,不再贅述。保存的變量名為cash_old
---------------------
作者:aduocd
來源:CSDN
原文:https://blog.csdn.net/aduocd/article/details/80351719
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM