1 Star2 Stars3 Stars4 Stars5 Stars

By Bob Meliev 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:


2. After you’ve run the test, nothing will happen in JMeter.

However, if you check the console, i.e STDOUT, where JMeter was launched, you’ll see the text: “Hello”. Now clear the Beanshell sampler field and write the following“Hello”);

3. Before running your test open “Log Viewer” from the “Options” menu.

Once you’ve done this, you can run the test. If you check your log, you’ll see the following line:

2014/08/25 13:07:19 INFO – jmeter.util.BeanShellTestElement: Hello

This is the result of a previous command.

4. Work with JMeter API.

Now we know how to print out the text – but this isn’t enough to start writing a Beanshell script. We need to try to learn the JMeter API and how to work with it. You can find documentation about the JMeter API here.

Let’s say we want to send a HTTP request and print out request properties. You should add the “HTTP Request” sampler, “View Results Tree” listener and the “BeanShell PostProcessor” as a sub element of the “HTTP Request”.

Before starting to write the Beanshell script, I recommend reading the article: “How to use BeanShell: JMeter’s favorite built-in component”. This article describes and explains frequently used pre-defined Beanshell variables. For example: we use the ctx variable to get access to samplers and their results. The ctx variable stands for JMeterContext class, and to use it we need to know its list of methods. Search for “JMeterContext” in the JMeter API and open it. In the table “Method Summary”, you’ll see all available methods in JMeterContext (ctx) displayed. We need getCurrentSampler() – which gives access to our “HTTP Request” sampler. If we write a script, it will look like this:


5. Save your test and run. In the console, you’ll see the sampler’s full URL.

If you want to print something to STDOUT or log, I recommend adding .toString() at the end of line. So previous line will look like this:


In the script, the ctx is JMeterContext class and getCurrentSampler() is it’s method. You can use other methods and see what results you’ll get.

6. We’ve now printed the thread number and name – but what if we want to get cookies from Beanshell?

In such as case, add “HTTP Cookie Manager” as a sub element of the “HTTP Request” sampler. In “HTTP Cookie Manager” add some cookies.

Now we need to find the interface of getCurrentSampler(). For this, click “getCurrentSampler()” which redirects to Sampler interface. In the Sampler interface, you’ll need to select the necessary sampler class from “All Known Implementing Classes”. In our case it’s the “HTTPSamplerProxy” class. In “HTTPSamplerProxy”, you’ll need to find the method with the name which includes cookies – which is: getCookieManager(). Open “CookieManager” to see the list of available methods. We need the get() method to print out the sending cookie. Now we can edit the Beanshell script and run the test. After getCurrentSampler(), add method getCookieManager(), get() and define the index 0 for the get() method.


7. Save the test and run it. In the console, you’ll see the sending cookie.

This command prints only one cookie – but how can we print all the cookies? To solve this problem, we first need to get a count of the cookies. To do this, we can use the following command:


This will print count of cookies. Now try to add more than two cookies in “HTTP Cookie Manager”.

8. In the “BeanShell PostProcessor”, add these lines:

cookieCount = ctx.getCurrentSampler().getCookieManager().getCookieCount();

for(int i=0; i


Você também pode querer ler

Comments are off for this post.