JMeter: Using a regex extractor value in beanshell post processor

為{幸葍}努か 提交于 2019-12-11 07:25:02

问题


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:

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):

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!


回答1:


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

  • http://jmeter.apache.org/usermanual/component_reference.html#postprocessors



回答2:


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.


来源:https://stackoverflow.com/questions/47024784/jmeter-using-a-regex-extractor-value-in-beanshell-post-processor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!