Why we need BeanShell scripting in JMeter?
JMeter is already equipped with various samplers, config elements, listeners, controllers, but there are instances where we want a more customized test plan. In such cases, the Beanshell sampler gives us the convenience of creating our own sampler by using BeanShell scripting.
BeanShell is one of the scripting languages that execute java syntax dynamically at run time.
We can write java code, or pass any java code fragment, loosely typed java code (variables without the type specified), and scripted methods, objects, and events into the BeanShell sampler. This piece of code gets interpreted at run time and gets executed line by line.
For those who are new to the programming world, the programming language and a scripting language differ in the following manner:
A programming language is usually compiled and converted into a machine language before execution while a scripting language is usually interpreted and executed line by line i.e. every line of code is first interpreted and then executed, unlike a programming language where entire code gets compiled and converted into object code before execution begins.
Just like Beanshell sampler, Jmeter also equipped with Beanshell pre and post processors. Beanshell preprocessor takes the arbitrary code written in it to be executed before taking any sampler. likewise, Beanshell postprocessor takes the arbitrary code written in it to be executed after taking any sampler. Beanshell samplers, Beanshell pre/postprocessors in JMeter give us various predefined variables that can be used directly to call several methods under the JMeter library.
1. Predefined variables in Beanshell postprocessor:
-ctx
-vars
-props
-prev
-data
-log
2. Predefined variables in Beanshell preprocessor:
-ctx
-vars
-props
-prev
-sampler
-log
3. Predefined variables in Beanshell sampler:
-SampleResult
-ResponseCode
-ResponseMessage
-IsSuccess
-Label
-FileName
-ctx
-vars
-props
-logs
ctx can be used to call any method of JmeterContext class.
Some common methods that can be called using ctx are:
vars can be used to access JMeterVariables class.
Some common methods that can be called using vars are:
prev can be used under BeanShell pre and post-processors to access methods of SampleResult class.
Some common methods that can be called using prev are:
props can be used to access the methods under java.utils.Properties class.