JMeter: Using a regex extractor value in beanshell

2019-07-31 04:33发布

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!

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-31 04:58

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.
查看更多
Melony?
3楼-- · 2019-07-31 05:10

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

查看更多
登录 后发表回答