码迷,mamicode.com
首页 > 系统相关 > 详细

BeanShell断言

时间:2020-11-21 12:28:24      阅读:13      评论:0      收藏:0      [点我收藏+]

标签:port   rev   成功   状态码   message   string   ems   info   图片   

方法一:

import org.json.*;

//获取响应状态码
String code = prev.getResponseCode();
System.out.println("code的值:" + code);
if (code.equals("${my_code}"))
{
    //获取上一个请求的返回值
    String response = prev.getResponseDataAsString();
    //将返回值转换为json
    JSONObject responseJson = new JSONObject(response);

    //获取responseMsg
    String msg = responseJson.getString("msg");
    System.out.println("msg的值:" + msg);

    if(!msg.equals("${my_msg}")) {
            Failure = true;
            FailureMessage = "规则解析失败";
        }
    else{
            FailureMessage = "规则解析成功";
            }

    //获取responseCode
    String code1 = responseJson.get("code").toString();
    System.out.println("code的值:" + code1);

    if(!code1.equals("${my_code1}")) {
           Failure = true;
           FailureMessage = "code非0";
    }
    else{
           FailureMessage = "code为0";
            }
        
    //获取responseSuccess
    String success = responseJson.get("success").toString();
    System.out.println("success的值:" + success);

    if(!success.equals("${my_success}")) {
            Failure = true;
            FailureMessage = "success为false";
    }
    else{
            FailureMessage = "success为true";
            }

}

else
{
    Failure = true;
    FailureMessage = "响应code与预期不符";
    prev.setStopThread(true);
}

方法二: 先下载一个fastjson-1.2.49.jar 包,并将该包放在 D:\jmeter5.2\apache-jmeter-5.2.1\lib\ext 文件夹下

技术图片

具体脚本如下:

  `import com.alibaba.fastjson.JSONObject;
  import com.alibaba.fastjson.JSONArray;
  import com.alibaba.fastjson.JSON;
  import com.alibaba.fastjson.JSONPath;


  //获取响应状态码
  String code = prev.getResponseCode();
  System.out.println("code的值:" + code);
  if (code.equals("${my_code}"))
  {
      //获取上一个请求的返回值
      String response = prev.getResponseDataAsString();
      //将返回值转换为json
      JSONObject responseJson= JSON.parseObject(response); 
      //获取responseMsg
      String msg = responseJson.getString("msg");
      System.out.println("msg的值:" + msg);

      if(!msg.equals("${my_msg}")) {
              Failure = true;
              FailureMessage = "规则解析失败";
          }
      else{
              FailureMessage = "规则解析成功";
              }

      //获取responseCode
      String code1 = responseJson.get("code").toString();
      System.out.println("code的值:" + code1);

      if(!code1.equals("${my_code1}")) {
              Failure = true;
              FailureMessage = "code非0";
      }
      else{
              FailureMessage = "code为0";
              }
        
      //获取responseSuccess
      String success = responseJson.get("success").toString();
      System.out.println("success的值:" + success);

      if(!success.equals("${my_success}")) {
              Failure = true;
              FailureMessage = "success为false";
      }
      else{
              FailureMessage = "success为true";
              }

  }

  else
  {
      Failure = true;
      FailureMessage = "响应code与预期不符";
      prev.setStopThread(true);
  }`

BeanShell断言

标签:port   rev   成功   状态码   message   string   ems   info   图片   

原文地址:https://www.cnblogs.com/ymlpk/p/13995768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!