0
votes

I have this code in my Beanshell Post Processor

String line;
String[] words;

line = SampleResult.getResponseMessage(); log.info(msg);

words  = line.split("*");

log.info("Here We are"); 

for (int i = 0; i < words.length; i++) {
  log.info(words[i]);
 }

This is the error I'm getting

2017/02/21 14:23:31 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: String line; String[] words; line = SampleResult.getResponseMessage(); log.inf . . . '' : Attempt to resolve method: getResponseMessage() on undefined variable or class name: SampleResult 2017/02/21 14:23:31 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: String line; String[] words; line = SampleResult.getResponseMessage(); log.inf . . . '' : Attempt to resolve method: getResponseMessage() on undefined variable or class name: SampleResult

2
Where are you getting SampleResult from? because if you read the error its mor or less saying " I dont know what SampleResult is." – TehTris

2 Answers

3
votes

Strictly speaking, to get rid of the error, you need to replace SampleResult with prev.

prev - (SampleResult) - gives access to the previous SampleResult

(source)

So to resolve exception:

line = prev.getResponseMessage(); 
log.info(line);

You can always see the list of variables objects gives you in the object itself:

enter image description here

However if I understand correctly you want to get data and not response message from the sampler. So you may want to use getResponseDataAsString() instead of getResponseMessage()

So all together:

line = prev.getResponseDataAsString(); 
log.info(line);
2
votes
  1. SampleResult is something you can use this way
  2. ResponseMessage != Response Body

So use one of the following approaches

  • line = new String(data);
  • line = prev.getResponseDataAsString();
  • line = ctx.getPreviousResult().getResponseDataAsString());

Where:

  • data is a byte array containing binary representation of parent Sampler response data
  • prev is a shorthand to current SampleResult class instance
  • ctx - stands for JMeterContext

Also consider using JSR223 PostProcessor and Groovy language instead of Beanshell as Groovy is more Java-compatible, performs better and has nice language features which will make your code look and work better. See Groovy Is the New Black article for details.