Jmeter will help you to create Performance script which are similar to real user but how to validate the response what we received from is valid, To validate the response from server Jmeter has a feature of Assertions. we have different types of assertions like Bean Shell Assertion, JSR223 Assertion, Response Assertion, Size Assertion, XPath Assertion, etc..
In this tutorial we will see the how to use Bean shell Assertions to manage assertions for a Jmeter sampler
Bean shell assertion is one of the assertion provide by the Jmeter. Bean shell assertion is used to control of the Sampler to make it pass/failed. We have multiple objects available in bean shell assertions like
Read/Write Access for objects :
- Failure
- FailureMessage
- SampleResult
- vars
- props
- log
ReadOnly :
- ResponseData
- ResponseCode
- ResponseMessage
- ResponseHeaders
- RequestHeaders
- SampleLabel
- SamplerData
- ctx
We can change the sampler status from pass to fail and fail to pass using bean shell assertion. by using the bean shell assertion will be getting control on the response Object
we can fail the Sampler using bean shell Assertion using below code in bean shell assertion editor
- Below Code to fail the sampler with assertion
Failure = true; FailureMessage="Beanshell assertion failing";
- For Changing the responseCode , ResponseHeader or ResponseMessage use below code
SampleResult.setResponseCode("500"); SampleResult.setResponseHeaders("header1:headervalue"); SampleResult.setResponseMessage("response message as string");
above line will override the actual response object. For reference use sampleResult API of Jmeter : Jmeter API
- Vars Object will help you to get variables of Jmeter or change the already existing variable value by using vars.get(“variableName”) or vars.set(“varaibleName”,”ValueValue”);
- vars.get will return only string object
- vars.set will take only 2 parameters both are string only
- log is used to log the info , error , warn in console
Readonly objects will used to just return the content which we cannot modify
- ResponseData : will return the response data of the sampler as Byte Array
- ResponseCode: it will return the response code like 200,500,404,403 etc
- ResponseMessage: it will return the response message like “OK”,”Internal Server Error”, “Access Forbidden” etc
- ResponseHeaders: it will return the response headers of current sampler inform of String
- RequestHeaders: it will return request header of current sampler
- SampleLabel : Sampler name
- SamplerData: it will also return the same response body as String
- Ctx: it will return the context of the jmeter thread
Thanks