zoukankan      html  css  js  c++  java
  • beanShell断言

    用户可以在jmeter- “beanShell断言”中自定义断言。自由灵活的用脚本实现自己的断言 
    beanShell断言接口介绍 
    在beanShell中直接可以调用的变量,无需加前缀。 
    1.log 打印日志 log.info(“在控制台打印日志”); 
    2.SampleResult 获取SampleResult对象,可以通过这个对象获取想要的信息 
    3.Response 获取Response对象,可以通过这个对象获取对应的信息 
    4.Failure 查看接口调用是否成功,如果返回false是成功的,true是失败的 
    5.FailureMessage 失败信息,没有设置的时候失败信息是空的。可以set这个信息 
    6.ResponseData 获取 response body 类型是byte[] 
    7.ResponseCode 返回接口code 成功是200 
    8.ResponseMessage 获取msg 成功是OK 
    9.ResponseHeaders 获取接口服务端返回的头部信息 
    10.RequestHeaders 获取客户端请求的头部信息 
    11.SampleLabel 获取接口请求的名称 
    12.SamplerData 获取请求的url和body 
    13.ctx 代表上下文信息,可以直接使用 
    14.vars 可以直接调用,将获取的数据变成jmeter变量(put),也可以获取用户自定义的变量(get) 
    eg: 
    1.在http sample下添加一个beanShell断言 
    2.编写script 

     1 import org.apache.jmeter.assertions; 
     2 import org.apache.jmeter.samplers.SampleResult; 
     3 import org.apache.jmeter.assertions.AssertionResult; 
     4 import org.json.*;
     5 
     6 String response_data = prev.getResponseDataAsString();//获取接口返回的response数据 
     7 JSONObject data_obj = new JSONObject(response_data);//转换成json 
     8 //判断code是否等于0 如果等于0 再取里面的字段 
     9 String code = data_obj.get(“code”); 
    10 if(code.equals(“0”)) 
    11 { 
    12 String user_name = data_obj.get(“data”).get(“user”).get(“userName”).toString(); 
    13 //这里的my_name 是用户自定义的变量或者之前的接口返回获取的 
    14 if(user_name.equals(${my_name})){ 
    15 Failure = false; //设置成false 表示接口跑成功,在结果树中sample是绿色的 
    16 //做后续动作 
    17 ……. 
    18 } 
    19 else{ 
    20 //做其他动作 
    21 } 
    22 } 
    23 else 
    24 { 
    25 Failure = true;//直接判断失败 表示接口跑失败,在结果树中sample是红色的 
    26 prev.setStopThread(true);//如果断言失败,后面的接口不需要再跑,直接暂停 
    27 }
  • 相关阅读:
    CodeForces gym Nasta Rabbara lct
    bzoj 4025 二分图 lct
    CodeForces 785E Anton and Permutation
    bzoj 3669 魔法森林
    模板汇总——快读 fread
    bzoj2049 Cave 洞穴勘测 lct
    bzoj 2002 弹飞绵羊 lct裸题
    HDU 6394 Tree 分块 || lct
    HDU 6364 Ringland
    nyoj221_Tree_subsequent_traversal
  • 原文地址:https://www.cnblogs.com/linbo3168/p/9436039.html
Copyright © 2011-2022 走看看