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)