jmeter断言(3种)

beanshell断言
-------(新增)获取数据库的字段,与接口发送的字段做校验
log.info("=========");
/获取发送的手机号码/
mobile = vars.getObject(“result”).get(0).get(“mobile”).toString();
log.info(mobile);
/获取发送的短信模板/
TPL_CODE = vars.getObject(“result”).get(0).get(“TPL_CODE”).toString();
log.info(TPL_CODE);
/打印上面得到的变量的类型/
log.info(mobile.getClass().getName());
String compareresult=null;
/判定发送的短信是否要求/
if (!mobile.equals("${mobile}") || !TPL_CODE.equals(“SMS_4444_01”)){
Failure = true;
FailureMessage = “数据库查询出来的数据不正确”;
}

-------获取接口返回的结果,如果是true则获取其中的参数用于下一个接口,如失败则报错
import org.json.* ;
//获取最后一次请求返回的数据(JMETER内置方法)
String response = prev.getResponseDataAsString() ;
log.info(response) ;
// 返回值转换为JSON对象
JSONObject jsonResp = new JSONObject(response) ;
//获取success字段的值并转换为字符串
String successValue = jsonResp.get(“success”).toString() ;
log.info(successValue) ;
// 判断返回值中success字段值如果为 true,则取得data中token的值,当失败时,说明登录失败
if(successValue == “true”){
String dataValue = jsonResp.get(“data”).toString() ;
JSONObject jsonData = new JSONObject(dataValue) ;
String tokenValue = jsonData.get(“token”).toString() ;
log.info(tokenValue) ;
vars.put(“token”,tokenValue) ;
FailureMessage = “用户登录执行成功!”;
// log.info(“用户登录执行成功!”) ;
}
else{
//
Failure = true ;
String msg = jsonResp.get(“msg”).toString() ;
log.info(msg) ;
// FailureMessage=“用户登录失败的错误信息”+msg ;
}

------获取接口返程的结果,如成功则校验数据库中的字段是否正确,若失败则报错
import org.json.*;

//获取接口请求返回的结果
String response = prev.getResponseDataAsString();
log.info(“接口返回的值为:”+response);
//将response转换为JSON格式
JSONObject responsejson = new JSONObject(response);
//获取response中 success的值(即接口的请求结果)
String responsesuccess = responsejson.get(“success”).toString();
log.info(“接口返回值中的状态为:”+responsesuccess);
//判断response中 success的值是否是True,如果是False则返回接口调用失败
if(!responsesuccess.equals(“true”)){
Failure = true;
FailureMessage = “接口调用失败~”;
}else{
/获取发送的短信模板/
TPL_CODE = vars.getObject(“result”).get(0).get(“TPL_CODE”).toString();
log.info(TPL_CODE);
/打印上面得到的变量的类型/
log.info(TPL_CODE.getClass().getName());

/*判定发送的短信是否要求*/
if (!TPL_CODE.equals("接口测试模板")){
    Failure = true;
    FailureMessage = "数据库查询出来的数据不正确";

}
}

---------使用前置处理器之后,得到的值用在http请求中
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37786014/article/details/90173660