beanshell preprocessor
发送请求前执行
beanshell postprocessor
发送请求前执行
==================================
获取请求相关信息
String body = sampler.getArguments().getArgument(0).getValue();
String url = sampler.getPath();
获取响应报文
String response=prev.getResponseDataAsString();
获取变量
String deviceId = vars.get("deviceId");
设置变量
vars.put("deviceHttpsToken", xxxx);
举例:获取返回数据中的字段
import org.apache.jmeter.config.Arguments;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
//获取响应报文
String response=prev.getResponseDataAsString();
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode=mapper.readTree(response);
retCode=rootNode.get("retCode").asText();
log.info("==========取到的retCode值:"+retCode);
vars.put("code",code);
有列表
import org.apache.jmeter.config.Arguments;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
//获取响应报文
String response=prev.getResponseDataAsString();
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode=mapper.readTree(response);
retCode=rootNode.get("payload").get("list").get(0).get("creator").asText();
log.info("==========取到的retCode值:"+retCode);
//vars.put("code",code);
截取字符串
No=vars.get("No");
log.info("获取到的No:"+No);
No=No.substring(1);
log.info("去掉第一位后的No:"+No);
No1=No.substring(0,3);
log.info("截取出来的001:"+No1);
计算
import java.math.BigDecimal;
BigDecimal a=new BigDecimal("6");
BigDecimal b=new BigDecimal("2");
BigDecimal c=a.add(b);
String c1=c.toString();
log.info("a+b="+c1);
BigDecimal c=a.subtract(b);
String c1=c.toString();
log.info("a-b="+c1);
BigDecimal c=a.multiply(b);
String c1=c.toString();
log.info("a*b="+c1);
BigDecimal c=a.divide(b);
String c1=c.toString();
log.info("a/b="+c1);
查看日志
打开日志选项,beanshell的log可以在日志中查看log.info("deviceId")