jmeter使用BeanShell断言

1. 首先存储一个接口的响应结果,如在http请求的BeanShell PostProcessor:

import java.io.UnsupportedEncodingException;

System.out.println(prev.getResponseCode());
String str = prev.getResponseDataAsString();
/*
String result = ${__urldecode(str)};
System.out.println("------------"+result);
*/
String result = "";
try {
result = java.net.URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("--------"+result); vars.put("result",result);
log.info(result);

 或者是jdbc request中BeanShell PostProcessor:

columnValue = vars.getObject("testresultvariable").get(0).get("app_id");
vars.put("value",columnValue);

 

变量存储好后,再需要断言的接口后面添加BeanShell断言,使用Failrue来标识断言失败,FailureMessage标示断言失败的原因,如:

String result = vars.get("result");

if(!result.contains("\"result\":2")) {
Failure = true;
FailureMessage = "分润查询失败";
}else{
FailureMessage = "分润查询成功";
}

  

断言结果是:

jmeter使用BeanShell断言

使用第三方jar包,结合fastjar的JSON解析如下:

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.*;
import com.jmeter.*; listexc = new ArrayList();
listexc.add(351);
listexc.add(301);
listexc.add(302);
listexc.add(304);
listexc.add(305);
listexc.add(306);
list = new ArrayList();
flag = "false"; if ((prev.getResponseCode() != null) && (prev.getResponseCode().equals("200") == true)) {
String json = prev.getResponseDataAsString();
JSONObject jo = JSON.parseObject(json);
if(jo.containsKey("events")) {
JSONArray ja = jo.getJSONArray("events");
if(ja.size()>0) {
flag = "true";
for(int i = 0;i<ja.size();i++) {
System.out.println(ja.get(i));
JSONObject joe = ja.getJSONObject(i);
System.out.println(joe.get("eventId"));
list.add(joe.get("eventId"));
}
System.out.println(list.size());
System.out.println(new EventCompare().compare(listexc,list));
if(!new EventCompare().compare(listexc,list)){
Failure = true;
FailureMessage = "事件校验失败,预期结果和实际结果不一致,预期事件:"+listexc+" ,实际事件:"+list;
}
}else{
flag = "false";
Failure = true;
FailureMessage = "事件校验失败,轮询接口返回没有eventId";
}
vars.put("result",flag);
System.out.println(list.size());
/*
System.out.println(listexc.containsAll(list));
if(listexc.containsAll(list)) {
Failure = true;
FailureMessage = "事件校验失败";
}
*/
}else {
Failure = true;
FailureMessage = "事件校验失败,轮询接口返回没有events";
}
}else {
Failure = true;
FailureMessage = "事件校验失败,轮询接口返回空";
}

  

上一篇:jmeter之beanshell断言---数据处理


下一篇:Jmeter之BeanShell断言使用