简体   繁体   中英

Jmeter BeanShell - Access Response Data - Beanshell error?

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 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 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

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:

在此处输入图片说明

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);
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM