Thursday 30 July 2009

How to make BeanShell work in JMeter

There was a section in JMeter tutorial on how to make BeanShell work in it. You can see section 2.2.7 in this page. But the section is not present at the moment I write this post. Maybe it should be like it is, because there is virtually one step to get it working.
To make BeanShell working you basically have to download one JAR file from BeanShell.org (I have downloaded bsh-2.0b4.jar) and place it in JMETER_HOME/lib directory.
After resterting JMeter and allowing it to read all the libraries it should work because lib directory is in JMeter's classpath.
There are some properties in jmeter.properties file e.g. beanshell.init.file if you need for example to define functions used in you BeanShell test elements.

Sometime it is difficult to troubleshoot problems with BeanShell test elements because JMeter omits Samplers that have bad BeanShell code. If you think your code should work, but it does not look into jmeter.log file. All errors are put there with sufficient description to get to know what is wrong with your code.

If you are using BeanShellSamplter it is good to use return statement with the data you want to observe. The value you returen will become the response of BeanShellSampler and you will be able to see it in View Results Tree and furthermore if it is an XML it can be shown as a tree.
Very goot explanation of what BeanShellSampler can do is in JMeter documentation. Look there.

Tuesday 30 June 2009

How to read e-mails through JMeter

To read e-mails JMeter uses Mail Reader Sampler. You have to place JavaMail and JAF jars in JMeter's classpath to use this sampler. If you plan to use downloaded e-mails in your tests you have to process them. Main problem is, that e-mails are treated as subsamples and many post processors cannot use them. Following code put in BeanShell PostProcessor processes all the messages got by Mail Reader Sampler:

subs = prev.getSubResults()
;

counter = 1;
for(SampleResult sub: subs) {
mail = sub.getResponseDataAsString();

// do something here with your the e-mail if you wish

// or put it into variable for further processing
vars.put("prefix_"+Integer.toString(counter),mail);
counter++;
}



If you put e-mail to variables prefix_# you can use ForEach Controller to process e-mail. For example you test plan may look as follows:
ForEach Controller should be configured as follows:
Remember to check Add "_" before number, because in the code there was underscore after ther prefix. And you can use ${mail} to access mail body or mail = vars.get("mail"); in BeanShell.

What I had to do was to extract URL from confirmation e-mail and pick up the process from where it has been interrupted. So, I have extracted urls in BeanShell Postprocessor and put them into variables rather than whole e-mail.

Happy testing.
Janek

Tuesday 23 June 2009

Manifesto

I am beginning this blog to show anybody who works with JMeter how certain tasks can be achieved. I believe JMeter is great tools and constitutes good framework to build automated tests. JMeter is flexible and, what I value most, extendable. If you need something and there is no function doing it in JMeter you can extend it either by writing several lines of Java code in BeanShel sampler or building your own custom class and adding your jar to lib/ext directory.

I think some people may need solutions I have worked out for my testing projects, so I want to share them. If you need anything with JMeter, write it and I may be able to help you and we will share the solution with all JMeter users. If you have developed an interesting piece of code regarding JMeter, please share with us.

Janek