Java教程

jmeter-beanshell断言练习

本文主要是介绍jmeter-beanshell断言练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

记录一下: 

这个是通过抓包获取到的response信息

{
    "header": {
        "errcode": "0000000000",
        "errmsg": "SUCCESS"
    },
    "data": [{
        "idDynBizSd": "20",
        "sdBiz": "2",
        "cdDynBizSd": "201",
        "nmDynBizSd": "aaa",
        "cdDynBizSdPar": "0",
        "reserve1": "xxx",
        "reserve2": "yyy",
        "reserve3": "zzz:zzz",
        "reserve4": null,
        "reserve5": null
    }, {
        "idDynBizSd": "21",
        "sdBiz": "2",
        "cdDynBizSd": "202",
        "nmDynBizSd": "bbb",
        "cdDynBizSdPar": "0",
        "reserve1": "xxxx",
        "reserve2": "yyyy",
        "reserve3": "zzzz:zzzz",
        "reserve4": null,
        "reserve5": null
    }]
}

 

以下代码可在jmeter的beanshell断言 可正常运行

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
try{
    response = prev.getResponseDataAsString();
    //将字符串转换为了对象
    JSONObject jo = JSON.parseObject(response);
    //header对象
    JSONObject headerJsonObject = jo.getJSONObject("header");
    System.out.println(headerJsonObject);
    System.out.println("===========响应码和响应信息的断言===========");
    String errcode = headerJsonObject.getString("errcode");
    System.out.println("errcode为:"+errcode);
    if("0000000000".equals(errcode)==false){
        Failure=true ;
           FailureMessage ="Response code was not a 200 response code it was " + errcode + "." ;
            log.warn( "the return code is " + errcode);
        }
    else{
        Failure=false;
            FailureMessage = "Return true, and the response code was " + errcode;
            log.info(FailureMessage);
        }
        
    String errmsg = headerJsonObject.getString("errmsg");
    System.out.println("errmsg为:"+errmsg);
    if(!errmsg.equals("SUCCESS")){
        Failure = true;
           FailureMessage = "errmsg与实际值不一致";
           log.info(FailureMessage);
        }
    else{
        Failure=false;
            FailureMessage = "Return true, and the response errmsg was " + errmsg;
            log.info(FailureMessage);
        }
    
    //获取data对象 data的对象为[],所以要转化为JSONArray类型的对象
    JSONArray data = jo.getJSONArray("data");
    int size = data.size();
    for(int i = 0 ; i<size ; i++) {
            JSONObject dataIndex = data.getJSONObject(i);
            String reserve3 = dataIndex.getString("reserve3");
            String nmDynBizSd=dataIndex.getString("nmDynBizSd");
//            System.out.println(reserve3);
            log.info(reserve3);
            log.info(nmDynBizSd);}
     System.out.println("===========response返回值关键字段的断言,只断言了一个字段===========");
     JSONObject dataIndex = data.getJSONObject(0);
     String nmDynBizSd = dataIndex.getString("nmDynBizSd");
     System.out.println(nmDynBizSd);
        if(!nmDynBizSd.equals("计划111")){
            Failure = true;
           FailureMessage = "nmDynBizSd与实际值不一致"; 
           log.info(FailureMessage);          
            }
        else{
            Failure=false;
            FailureMessage = "Return true, and the nmDynBizSd was " + nmDynBizSd;
            log.info(FailureMessage);
            }
        
}
catch(Exception e){
    FailureMessage = "响应的参数解析错误";
    Failure = true;
}

 

这篇关于jmeter-beanshell断言练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!