0
votes

I am using JMeter to automate performance test cases. After HTTP request, I have used RegEx Post Extractor to read the response header. This response header return location. Location is project-specific URL. I want to read the value of the state variable from this URL. For this, I have used BeanShell PostProcessor. For example,

location: "http://www.google.com/state=asdas123123123123&query=asdasdas!@#"

I am able to read value of response header as http://www.google.com/state=asdas123123123123&query=asdasdas!@#" using regular expression extractor

Then, I have added BeanShell PostProcesssor to read state and query.

import java.util.regex.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

try{
//location = "http://www.google.com/state=asdas123123123123&query=asdasdas!@#"
String location = vars.get("location");
String pStr = "/state=(\\S+?)&/"";
Pattern patternN = Pattern.compile(pStr);

Matcher matcher = patternN.matcher(pStr);

    if (matcher.find()) {
        vars.put("variablename_1",matcher.group(0));
    }

    
vars.put("variablename_2",location);
}catch (Exception ex) {
        vars.put("variablename_ex",ex);
    throw ex;
} 

This code is not working. It seems double slash is creating problems.

1
Replace String pStr = "/state=(\\S+?)&/""; with String pStr = "state=([^\\s&]+)"; and then use vars.put("variablename_1",matcher.group(1));Wiktor Stribiżew

1 Answers

1
votes
  1. Since JMeter 3.1 you should be using JDR223 Test Elements and Groovy language for scripting

  2. Groovy provides Find operator which looks like =~

  3. Assuming above 2 points you can refactor your "code" to this one-liner:

    vars.put('variablename_1', (vars.get('location') =~ /state=(\w+)&/)[0][1])
    

Demo:

enter image description here

More information: