-5

The code i have written for that is as folows:

try {
    byte []
    samplerdata = ctx.getPreviousResult().getResponseCode();
    String
    samplerdatastring = new String(samplerdata, "UFT-8");
    vars.put("samplerdata", samplerdatastring);
    vars.get(samplerdata);
    int
    matches = StringUtils.countMatches(samplerdata, "200");
    vars.put("matchescount", String.valueOf(matches));
    vars.get(matchescount);
} catch (Throwable
ex
)
{
    log.error("Error in Beanshell", ex);
    throw ex;
} 

It results in an rror as follows:

jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``try { byte [] samplerdata = ctx.getPreviousResult().getResponseCode(); Stri . . . '' : TargetError

Can any one please help me to resolve this error.

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Please take a minute to properly format the code in your question. As it is now, it's very hard to read. – JeffC Dec 12 '16 at 15:41

1 Answers1

0

try as follows:

import org.apache.commons.lang.StringUtils;

try {
    String
    samplerdata = ctx.getPreviousResult().getResponseCode();
    vars.put("samplerdata", samplerdata);
    vars.get("samplerdata");
    int
    matches = StringUtils.countMatches(samplerdata, "200");
    vars.put("matchescount", String.valueOf(matches));
    log.info(vars.get("matchescount"));
} catch (Throwable
ex
)
{
    log.error("Error in Beanshell", ex);
    throw ex;
} 

Solved issues in the code:

  1. getResponseCode returns String, so store it in String but not in byte[]
  2. Removed additional code used for conversion from byte[] to String
  3. Added import statement for StringUtils
  4. while retrieving matchescount, we should keep it in double quotes in vars.get. similarly for samplerdata

Instead of ctx, you can use prev, which returns SampleResult object.

existing:

String
    samplerdata = ctx.getPreviousResult().getResponseCode();

you can also try:

String
    samplerdata = prev.getResponseCode();
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65