一、vars:当前线程内使用
1、新增用户定义变量
用户变量.png
2、新增BeanShell PreProcessor
import org.apache.commons.codec.digest.DigestUtils;
/*vars.get(String key) //获取变量值*/
String phone = vars.get("phone");
String timestamp = vars.get("timestamp");
String countryCode = vars.get("countryCode");
log.info("************************************************************************************************************");
/*md5加密*/
/*加密字符串是按字母顺序进行排序的*/
String str = "countryCode"+countryCode+"phone"+phone+"timestamp"+timestamp;
String secret = "3c21561aea0925f1a1667660b2ac3fd7";
String sign = DigestUtils.md5Hex(secret+str+secret);
/*vars.put(String key, String value) //存储value到变量key*/
vars.put("sign",sign);
/*log.info(""),括号为变量*/
log.info(phone);
log.info("************************************************************************************************************");
/*加密后,转换成小写*/
/*vars.put("sign",sign.toLowerCase());*/
二、props:跨线程使用
线程A内:
响应数据示例:
{
"code": 200,
"data": [
{
"cover": "http:\/\/package.xiaohutu.com\/xiaohutu\/xueling\/cidian.jpg",
"bookSize": "904.0MB",
"bookIntro": "",
"releaseDate": "2017-01-20",
"packageUrl": "http:\/\/package.xiaohutu.com\/xiaohutu\/source\/Growing_upV7.bnf",
"progress": "",
"bookName": "Growing Up 英汉彩图辞典八万字",
"bookId": 9674,
"status": 0
}
],
"message": "获取成功"
}
1、使用JSON path extractor 获取book_id;
image.png
2、新增BeanShell PostProcessor;
将变量改成jmeter的内置属性
import org.apache.commons.codec.digest.DigestUtils;
String b = vars.get("book_id");
props.put("newbook_id", b);
log.info("********************")
线程B内:
3、新增BeanShell Sampler;
直接调用jmeter的内置属性变量
${__P(newbook_id)}
网友评论