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

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. hi,
    I also need to extract an URL from a confirmation e-mail. But I'm having some trouble figuring it out. It looks like Jmeter is not executing my Beanshell PostProcessor. Are there any configuration options that I need to keep in mind?

    Kind Regards
    Valérie

    ReplyDelete