Search code examples
regexvariablesscopejmeterextractor

jmeter variable can not be referenced as expected


I have a problem with my Test Plan. If I add User Defined Variable, let's say csrftoken and assign NOT_INITIALIZED to it in User Defined Variables config element, it is passed to http request where it is referenced, but not set in prior http request sampler that has a regular expression extractor child element, that is supposed to set it, which means it is passed as "NOT_INITIALIZED". If I don't use User Defined Variables config element to declare it, I cannot reference it in later http request, that is, it stays like ${csrftoken}, although it "should be" set/created by previous http request and its post processing regexp Extractor, since it properly references the name. I think, there might be some problem with the scope, but I can't figure what. Everything is within the same Transaction Controller, nothing changes if I place everything directly at the Thread Group level. What am I missing? Thanks in advance.


Solution

  • It is not necessary to initialize a variable, Regular Expression Extractor can create a variable and assign a value to it on its own.

    If variable value isn't getting substituted - something is wrong with regex or template. If you could update your question with the relevant response bit and your Regular Expression Extractor configuration community and myself would be able to assist.

    Few references:

    • You can test your regular expressions against response data right in View Results Tree listener by choosing RegExp Tester option from the dropdown

      Regex Tester

    • Sometimes it isn't handy to use Regular Expressions for parsing HTML as they may be complex, markup-dependent and fragile so it's better to consider XPath Extractor or CSS/JQuery Extractor instead.

    • See How to Load Test CSRF-Protected Web Sites guide for comprehensive information on how to bypass CSRF protection in your JMeter test.