2

I have a problems while picking up variables set by the Regular Expression Extractor in a Beanshell.

enter image description here

enter image description here

The result of Regular Expression Extractor:

JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage@120cc173
START.HMS=090316
START.MS=1507255396987
START.YMD=20171006
TESTSTART.MS=1507266934916
returnLink=not found
returnLink_1=/tripper/payment/info?booking=KD6YGS4L8I
returnLink_1_g=1
returnLink_1_g0="returnLink":"/tripper/payment/info?booking=KD6YGS4L8I"
returnLink_1_g1=/tripper/payment/info?booking=KD6YGS4L8I
returnLink_matchNr=1

Now I want to using returnLink_1 in Beanshell, but I cannot get And print it out. Actually, I want to get "KD6YGS4L8I" from the string but I can't.

int size = Integer.parseInt(vars.get("returnLink_matchNr")); // to get size of matched values
log.info("size is:" + size ); // you can remove log stmts once it is working

String url = vars.get("returnLink_1");
log.info("link:", url);
vars.put("BookingCode",url); // store in Booking code and use for next thread

I'm a newbie in JMeter. Please help me.

2
  • 1
    Keep match number 1 instead of -1. See if that works, your regex looks good otherwise. Oct 6, 2017 at 6:57
  • 1 will only grab one value whereas -1 is supposed to grab all matches
    – ScottJShea
    Dec 4, 2018 at 22:05

2 Answers 2

2

As Rohan already states: you should use "1" and not "-1" enter image description here

It would be better that your expression would look like

booking=(.+?)"

enter image description here

as that would extract only the code you are looking for and it is returned in the defined variable ready for further use (assuming that the string is always the same of course apart from your code, otherwise you have to adapt your regex accordingly).

3
  • Nice: a downvote without comment... that really helps!!
    – Ray Oei
    Oct 9, 2017 at 7:16
  • Maybe because -1 is supposed to work and return all values that match?
    – ScottJShea
    Dec 4, 2018 at 22:04
  • As there is no reference to using the ForEach controller, a negative number is not expected. See manual Regular Expression Extractor: "Negative numbers are used in conjunction with the ForEach Controller - see below."
    – Ray Oei
    Dec 5, 2018 at 9:54
0
  1. Make sure your Beanshell PostProcessor is located below the Regular Expression Extractor as JMeter runs Post-Processors upside down
  2. Your test has mistake, you need to replace this line:

    log.info("link:", url);
    

    with this line:

    log.info("link:" + url);
    

    in order to see the output in the jmeter.log file

  3. Since JMeter 3.1 it is recommended to use JSR223 test elements and Groovy language for scripting instead of Beanshell. See Apache Groovy - Why and How You Should Use It guide for more detailed explanation on why Groovy is better. You won't even have to change a single line of code (apart from the one, mentioned in point 2)
  4. In general you don't need to copy returnLink_1 variable into BookingCode variable, you can access its value as ${returnLink_1} directly where required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.