CallmeJack 发表于 2018-2-8 16:05:50

Jmeter 解析 response 并传递 value

解析response中的内容,并把获取到的value传递到后续的request中,常用的方法就是在想要解析response
的request上添加后置处理器
本章介绍两种常用的组件

BeanShell PostProcessor
JSON Extractor
添加后置处理器:BeanShell PostProcessor

获取response中的字符串,并对内容进行判断,当response中包含“The wait operation timed out”或者“Oops.
Something went wrong ... sorry”时,都认为该request的response不正确
String response = prev.getResponseDataAsString();
String code = prev.getResponseCode();
log.info("Respnse is " + response);
log.info(code);
int result1 = response.indexOf("The wait operation timed out");
int result2 = response.indexOf("Oops. Something went wrong ... sorry");
if(code == "200" && result1>=0 || result2>=0){
FailureMessage = "OK,check current";
}
else{
Failure = true;
FailureMessage = "ERROR,check error";
}解析返回的Jason数据,获取name字段的值赋给变量result
{“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}}

import org.json.*;脚本中的导入的json包需要自己去网络下载后放到\lib\ext下
String response_data = prev.getResponseDataAsString();
JSONObject data_obj = new JSONObject(response_data);
String apps_str = data_obj.get("body").get("apps").toString();
JSONArray apps_array = new JSONArray(apps_str);
String[] result = new String;
for(int i=0;i<apps_array.length();i++){
JSONObject app_obj = new JSONObject(apps_array.get(i).toString());
String name = app_obj.get("name").toString();
result = name;
}
vars.put("result", Arrays.toString(result));

添加后置处理器:JSON Extractor

解析返回的Jason数据,获取name字段的值带入参数到后边的request中

{“body”:{“apps”:[{“name”:”111”},{“name”:”222”}]}}
变量t1的值是111,变量t2的值是222


梦想家 发表于 2018-2-28 17:07:05

:handshake

Miss_love 发表于 2020-12-30 08:42:34

支持分享
页: [1]
查看完整版本: Jmeter 解析 response 并传递 value