jMeter issue when using Cookie manager and Regular expression extractor

810 views Asked by At

So basically I need to extract an auth token from header response of 1st http request and then use the extracted data in 2nd (and all the following) http requests cookies.

The issue here is, that I have cookie manager set for the whole controller and instead of getting actual data I get the name of variable in my cookie ".authToken=${auth}".

I am guessing the reason is that the variable is not declared when the test reaches Cookie manager, but I would expect jmeter to be smart enough to declare the variable when it gets to the regular expression extractor.

Structure

  • Thread
    • Cache Manager
    • Cookie Manager (Cookie Policy:compatibility; Implementation:HC3)
    • Controller
      • Http Request
        • Regular expression extractor
      • Http request (I need to use value extracted above in Request Cookie here)
      • Http request (I need to use the same value in Request Cookie here)
      • Http request (I need to use the same value in Request Cookie here)
      • .....

Details:

All the http requests are recorded with implementation HttpClient3.1

Pretty sure I have everything configured correctly as in variable names, regular expression since it works in a very specific case:

The only time it seemed to work correctly was when I had Cookie manager inside the http request and disabled the 'main' Cookie manager (the one for the whole controller). Then it got extracted correctly, but that would be really silly workaround for such a basic requirement and also I have many http requests (over 100) where I need to use the extracted value.

Jmeter doesn't need to use the variable before it's declared by the regular expression extractor, I made sure that the domain is correct and it gets used for the first time after it should have been extracted.

Another workaround I thought of would be having separate threads, have them linked and send the variable in between them, launching the next one once the data gets extracted, but that seems a little bit too drastic.

What I tried:

  • Splitting http requests into 2 different controllers and using 2 different Cookie managers - got "${auth}" instead of some value
  • Defining user variable above controller and then using "Apply to: Jmeter Variable" option - again got just string "${auth}" instead of some value.
  • Moving the Cookie manager to a position after the http request which is used for the extraction - again "${auth}" instead of some value
  • Setting different cookie's policy (not all of them, but few)
  • Setting "CookieManager.save.cookies=true" in jmeter.properties (and still have on true)

Any help/ideas are appreciated. I have been trying to figure this out for about an hour and I think I must be missing something very simple.

1

There are 1 answers

0
David Porizek On BEST ANSWER

Alright, finally got this resolved after roughly 2 hours.

Thanks to this article, I was able to do what I needed https://capacitas.wordpress.com/2013/06/11/thats-the-way-the-cookie-crumbles-jmeter-style-part-2/

In nutshell: You need to use beanshell pre-processor and add the cookie manually Here is the beanshell script in case the site dies:

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("CookieName", vars.get("YourExtractedVariable"), "Domain", "Path", false, 0);
manager.add(cookie);