• jmeter-beanshell断言练习


    记录一下: 

    在使用jemeter做接口测试的时候,需要对返回的结果进行断言,如果要进行多个字段的断言,得添加多个jp@gc - JSON Path Assertion,这样比较费劲。所以想使用BeanShell断言来处理,将需要断言的字段存起来,需要对比时,再拿出来用。BeanShell断言可以使用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;
    }
  • 相关阅读:
    imagemagick-图片
    selenium-嘿
    centos命令行连接无线网络
    centos7安装桌面合盖不休眠
    mysql执行命令:ERROR 1820 (HY000): You must reset your password
    编码规范 C++
    Docker使用总结
    JAVA使用总结
    VS IDE 相关
    编程网站总结
  • 原文地址:https://www.cnblogs.com/eosclover/p/15405726.html
Copyright © 2020-2023  润新知