jmeter引用json的jar包处理beanshell的json响应
程序员文章站
2024-01-12 19:53:52
...
1、测试web项目,在web项目的WEB-INF/lib目录中找到对应的json包,如json-20140107.jar
如:可以在http://download.csdn.net/download/xiaoxiao_renhe/10246640中下载该json的jar包。
2、将该jar包放入到jmeter的lib\ext\目录下,以便jmeter调用引用
3、jmeter脚本引入该json包,如下截图所示:
4、响应的json形式如下:
5、BeanShell断言中该json包的使用:
6、该beanShell断言代码如下:
import org.json.JSONObject;
boolean flag = false;
if(200!=ResponseCode){
flag = true;
}
if(!ResponseMessage.equals("OK")){
flag = true;
}
String response = prev.getResponseDataAsString();
JSONObject responseJson = new JSONObject(response);
String msg = responseJson.getString("msg");
if(!msg.equals("用户登录成功")){
flag = true;
}
String data = responseJson.get("result").toString();
JSONObject strdata = new JSONObject(data);
log.info("----------"+strdata);
String fbpcontent = strdata.getString("userName");
if(flag){
Failure = true;
FailureMessage = "断言失败原因!"
}else{
Failure = false;
FailureMessage = "测试通过!"
}
7、响应的json如下(包含json数组)
8、该beanShell断言代码如下:
9、源码如下:
import org.json.JSONObject;
import org.json.JSONArray;
String beforeId = vars.get("registerId");
boolean flag = false;
try{
String response = prev.getResponseDataAsString();
JSONObject responseJson = new JSONObject(response);
JSONArray infoArrays = responseJson.getJSONArray("datas");
int len = infoArrays.length();
int i = 0;
String tempId = "";
String tempIsMessage = "";
for(i=0; i<len; i++){
JSONObject tempInfo = (JSONObject)infoArrays.getJSONObject(i);
tempId = tempInfo.getString("id");
tempIsMessage = tempInfo.getString("isMessage");
if(beforeId.equals(tempId) && "0".equals(tempIsMessage)){
flag = true;
break; //确认当前记录没有发消息
}
}//end for
}catch(Exception e){
e.printStackTrace();
}
if(flag){
Failure = false;
FailureMessage = "断言成功,断言是:没发过消息,图标灰色";
}else{
Failure = true;
FailureMessage = "断言失败,没找到该记录或已发过消息";
}
10、json数组中嵌套对象取值方法,json响应如下截图
11、BeanShell断言如下:
12、源码如下:
import org.json.JSONObject;
import org.json.JSONArray;
boolean flag = false;
String tempCode = "";
try{
String response = prev.getResponseDataAsString();
JSONObject responseJson = new JSONObject(response);
JSONArray infoArrays = responseJson.getJSONArray("datas");
int len = infoArrays.length();
int i = 0;
String itemId = "";
String name = "";
for(i=0; i<len; i++){
JSONObject tempInfo = (JSONObject)infoArrays.getJSONObject(i);
JSONObject tempRunVo = (JSONObject)tempInfo.getJSONObject("runSampVo");
tempCode = tempRunVo.getString("sampCode");
itemId = tempInfo.getString("id");
name = tempInfo.getString("name");
break; //只取第一个项目的id和name
}//end for
vars.put("itemId",itemId);
vars.put("name",name);
vars.put("backReson","任务分配已接收单独"+name+"退回");
}catch(Exception e){
e.printStackTrace();
}
String beforeCode = vars.get("code");
if(beforeCode.equals(tempCode)){
flag = true;
}
if(flag){
Failure = false;
FailureMessage = "断言成功,断言是:任务分配已接收查询成功";
}else{
Failure = true;
FailureMessage = "断言失败,任务分配已接收没有查询到";
}