1

I found a couple of similar threads on this topic but haven't had any luck applying solutions I've found so far. I'm in the process of learning JMeter, and an exercise I've been given is to (1) create a simple HTTP request, (2) extract the CSRF token with a Regular Expression Extractor, then (3) write out the extracted token via a beanshell script to the jmeter.log.

So extracting the token is straightforward: enter image description here

Since I need to next take that value and run it through a beanshell script, I assumed using a BeanShell PostProcessor would be the way to go here. I've tried a few variations of the following:

log.info("The token is: " + vars.get("token"));

The test runs fine, but then this appears in the resulting log:

INFO o.a.j.u.BeanShellTestElement: The token is: null

Lastly, since I understand the post processor runs upside-down in order, I placed it above my regex extractor (though I've tried it reverse, with no difference in effect):

enter image description here

I'm sure I'm making some noob mistake, but I'm starting to run in circles with my googling. Any advice would be hugely appreciated!

James Panetti
  • 238
  • 1
  • 2
  • 13

3 Answers3

1

The Beanshell post processor must be after the regex extractor otherwise the variable is not yet available

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
  • That was it -- Oddly, as I mentioned in the original post, I would have sworn I had tested it both ways, but apparently I just dreamed that in my head or something, because swapping the order worked: "INFO o.a.j.u.BeanShellTestElement: The token is: all aboard the FAIL boat!" It's getting the default value, so something's wrong with how I setup my token variable, but I can figure that out -- at least I know the script works now! Thank you! – James Panetti Oct 30 '17 at 22:28
1

With regards to your query itself: in order to be able to access the value from the Regular Expression Extractor in the other PostProcessor you need to put the Post Processor below the regular expression extractor.


With regards to approach in general:

  1. According to JMeter Best Practices starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language
  2. In general parsing HTML with regular expressions is not the best idea, I would suggest considering switching to CSS/JQuery Extractor instead.
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thank you for the advice. Unfortunately, because this is actually for a training exercise I've been tasked with, I don't have the option to switch over to Groovy -- but I have read that that is one of the preferred approaches. I am having some trouble with that pesky regex extractor though, so I posted a new thread today here: https://stackoverflow.com/questions/47043972/jmeter-regex-extractor-not-pulling-token – James Panetti Oct 31 '17 at 20:26
0

After Putting detailed effort, I got the solution of using the variables defined in Regular Expression Extractor in BEANSHELL POST PROCESSOR.

Simple you need to follow the standard format of accessing variables in JMeter:

${variablename}

i.e

log.info("${variablename}");

If you try to access those variables with vars.get() method, it will not work. So simply call the variables as we did normally with ${variable name}.

Sample Screenshots are attached:

Screenshot

Jaimil Patel
  • 1,301
  • 6
  • 13