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++){

扫描二维码关注公众号,回复: 4569691 查看本文章

    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
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/insane-Mr-Li/p/10146302.html