Definition of JMeter BeanShell
Beanshell is an advanced feature in JMeter and supports Java syntax with scripting features such as method closure, commands, and loose types. If the test case is rare and implemented in JMeter components it becomes complex. But BeanShell is a suitable option to obtain the goals. The entities of Beanshell in Jmeter has the access to both external class and internal API Jmeter which are fed to the classpath and have all the components associated with it which are available for the instant purpose. The article explains the working and features of BeanShell in JMeter in brief.
What is JMeter BeanShell?
In simple terms, the Beanshell is a strong assertion that provides the user complete access to the API of the JMeter. The function of the JMeter enables the execution of customized BeanShell code at the time of sampler run. There are other entities enabled in BeanShell.
A standalone sampler is called a BeanShell sampler
A pre-processor to another sampler is executed before the sampler and is implied for a prerequisite setup that is to fetch some input. The postprocessor of BeanShell is executed only after the execution of the sampler and it is used for clean-up or recovery.
The advanced assertion with entire access to API of JMeter is possible by Beanshell assertion and it works on Java conditional logic which is used to configure the assertion result.
The function of Beanshell enables the execution of code customized in Beanshell at the time of the sampler run.
How to use JMeter BeanShell?
JMeter has many advanced built-in plugins which can be used instantly. But the user feels perplexed when compiling complex test execution. In this case, it is advised to use the Beanshell sampler which is extremely powerful and instant. If the user is a Java developer, he can easily fetch the Java code to Beanshell and starts working on it.
If the user is not aware of Java, he finds it a bit difficult to compose the script the BeanShell. But there are a few steps, to script the code in Beanshell. Add Beanshell sampler and thread group from the console. The pre-defined variables are available in the Beanshell sampler which can be used for the code. They are isSuccess, Label, Responsemessage, responsecode, samplerresult, ctx, vars, log, filename, and props.
The script can be executed in the Beanshell sampler and if it is required separately, he can install it in the console of Beanshell.
The command sudo – apt-get install bsh
Once the user types bsh in the console, the prompt message is displayed in the console of the BeanShell. The most frequent and important pre-defined classes are discussed below,
SampleResult is used to map the JMeter class and all the methods and fields can be invoked and accessed. The sample case String current URL = sample result.get()
Response code is used as java.lang.string which represents the response code of sampler
The response message is similar to the response code and denoted in java. lang.string
Issuccess is a Boolean function that is used to denote the sampler is passed or failed. It is represented as true or false. It can be used in if or else statement.
A label is used to represent the sampler and it is a java.lang.string that can be set or get as a standard string which is listed in the result of the test as a sampler label.
FileName is comprised of Beanshell script which is used in the sampler.
Ctx is the advanced variable used in Beanshell and is represented in the Jmeter context class it provides write and read access to the engine and sampler of the Jmeter where the result is defined as properties or variables.
Jmeter variables are called vars which are used frequently. It is an instance that provides write or read access to the current variables which can change the existing variables or nested variables or create a new one. All the variables in Jmeter are declared as Java strings. If the user has to add some functions to the Jmeter variable, he has to cast the string.
Props are similar to vars but it has more exposure to features of Jmeter and supports global scope where the var has limited properties on the global scope to the present thread group.
The log is used to append the message in the log file of Jmeter.
The steps to use Beanshell in Jmeter are as follows:
Open the sampler of the BeanShell and execute the test which doesn’t make any function in Jmeter.
Choose the option from log viewer and open it to view the previous commands.
Then start to work with API in Jmeter by using pre-defined variables.
Then save the test and the page is reflected with the entire URL
Configure the properties in a sampler of the Beanshell
Analyze the script in BeanShell
Import the classes from the script in Beanshell and then start to debug the BeanShell script.
Use the BeanShell postprocessor and pre-processor to work on the sampler of the BeanShell
Jmeter BeanShell Groups
Before the test plan is executed, click on the run thread check box consecutively which is mandatory to execute thread groups one after one.
Then choose the regular expression extractor.
The output of the regular expression is saved in the second URL of the variable. The shuffle is made on the assertion of Beanshell.
Set property() is a function that has the in-built option and the second URL is used to save a regular expression extractor. The second URL is a line that is responsible to transfer the value in between the threads. The value of the second URL can be available from other thread groups.
The values of the second URL can be increased and analyzed with JMeter function properties. The property of the second URL can be defined as a server name.
Hence this is the overview of Jmeter BeanShell which has the option to compile the code and execute it to make achieved results.
This is a guide to JMeter BeanShell. Here we discuss the definition, What is JMeter BeanShell, How to use JMeter BeanShell?Examples with code implementation. You may also have a look at the following articles to learn more –