JMeter工具的BeanShell断言20210725

BeanShell Assertion

https://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Assertion

if (ResponseCode != null && ResponseCode.equals ("200") == false )
{
    // this is standard stuff
    Failure=true ;
    FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ;
    print ( "the return code is " + ResponseCode);   // this goes to stdout
    log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file
} else {
    try
    {
         // non standard stuff where BeanShell assertion will be really powerful .
         // in my example I just test the size , but you could extend it further
         // to actually test the content against another file.
         byte [] arr = (byte[]) ResponseData ;
        // print  ( arr.length ) ; // use this to determine the size
         if (arr != null && arr.length != 25218)
         {
             Failure= true ;
             FailureMessage = "The response data size was not as expected" ;
          }
          else if ( arr == null )
          {
              Failure= true ;
              FailureMessage = "The response data size was null"  ;
           }
     }
     catch ( Throwable t )
     {
          print ( t ) ;
          log.warn("Error: ",t);
     }
}

Before invoking the script, some variables are set up in the BeanShell interpreter. These are strings unless otherwise noted:

log - the Logger Object. (e.g.) log.warn("Message"[,Throwable])
SampleResult, prev - the SampleResult Object; read-write
Response - the response Object; read-write
Failure - boolean; read-write; used to set the Assertion status
FailureMessage - String; read-write; used to set the Assertion message
ResponseData - the response body (byte [])
ResponseCode - e.g. 200
ResponseMessage - e.g. OK
ResponseHeaders - contains the HTTP headers
RequestHeaders - contains the HTTP headers sent to the server
SampleLabel
SamplerData - data that was sent to the server
ctx - JMeterContext
vars - JMeterVariables - e.g.
vars.get("VAR1");
vars.put("VAR2","value");
vars.putObject("OBJ1",new Object());

props - JMeterProperties (class java.util.Properties) - e.g.

props.get("START.HMS");
props.put("PROP1","1234");

The following methods of the Response object may be useful:

setStopThread(boolean)
setStopTest(boolean)
String getSampleLabel()
setSampleLabel(String)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值