Shrushti
Shrushti

Reputation: 3

Get variable value from the response of an HTTP Sampler to Beanshell PostProcessor

In HTTP Sampler,response I have a javascript variable

int a=1;

I want to get the value of this 'a' in BeanShell Post Processor, How do I get it in JMeter?

Upvotes: 0

Views: 9941

Answers (2)

Dmitri T
Dmitri T

Reputation: 167992

Be aware that since JMeter 3.1 it is recommended to use Groovy for any form of scripting in JMeter so I would suggest going for JSR223 PostProcessor instead. The relevant Groovy code would be something like:

def text = prev.getResponseDataAsString()
log.info('Response text is' + text)
def match = text =~ /int a=(\d+)/
if (match.find()) {
    def value = match.group(1)
    log.info('------------------')
    log.info('a value=' + value) 
    vars.put('a', value) 
}

Demo:

Groovy Extract Data From Response

References:

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58774

The easiest way you can add Regular Expression Extractor as a Post Processor

Regular Expression: int a=(\w+);
Template: $1$
Match No.: 1

For Beanshell (or better JSR 223) Post processor you will have to "work" to get the regex:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
String stringToSearch=prev.getResponseDataAsString();
Pattern p = Pattern.compile("int a=(\\w+)");   
Matcher m = p.matcher(stringToSearch);
if (m.find()){
  vars.put("a", m.group(1));
}

In this script I'm adding the value to variable a, which later can be called ${a}

Upvotes: 0

Related Questions