JMeter has rich built-in plugins for most things you need. But you could still get stuck while writing complex tests for something specific.
In such cases, it’s worth using the Beanshell sampler. It’s really powerful…if you know how to use it. If you’re a Java developer you can easily invoke Java code in Beanshell. However, if you’re not familiar with Java, you might find it difficult to write a Beanshell script. So, in this article I’m going to show you how to write Beanshell scripts without any Java knowledge.
For starters: add “Thread group” and Beanshell sampler. The Beanshell sampler has pre-defined variables which you can use in the script, these are: SampleResult, ResponseCode, ResponseMessage, isSuccess, Label, FileName, ctx, vars, props, and log.
JMeter by default supports Beanshell so you can write and run Beanshell scripts in the Beanshell sampler. However, if you want to run them separately you can install the Beanshell console.
Type the following in the console:
sudo apt-get install bsh
Now type “bsh” in the console and you’ll see a prompt message from the Beanshell console. To learn the syntax of Beanshell, try checking out the online tutorial: “Beanshell Quick Start”.
Note that I’m not using the Beanshell console for coding. All scripts will be written in the JMeter Beanshell samplers.
Now we can start the step-by-step coding of the Beanshell script. Here are the steps you should take:
1. Open the Beanshell sampler and type the following: