Sharing cookie values between thread groups in JMeter

CookieManager does not share cookies between threads. For some reason @ClarenceKlopfstein's method didn't work for me (jmeter 3.0). For some reason "${COOKIE_<INSERT ACTUAL COOKIE NAME>}" didn't seemed to evaluate the string passed.

So, here's another solution to do a Login, and then pass the .ASPXAUTH cookie. It should work without any configuration changes: In a setUp Thread Group (important):

BeanShell PostProcessor:
 import org.apache.jmeter.protocol.http.control.CookieManager;
 import org.apache.jmeter.protocol.http.control.Cookie;

 CookieManager manager = ctx.getCurrentSampler().getCookieManager();
 Cookie cookie = manager.get(3); //Find the '.ASPXAUTH' cookie
 log.info("Cookie:" + cookie);
 props.put("MyCookie",cookie.getValue());

Then in a normal Thread Group:

BeanShell PreProcessor:
 import org.apache.jmeter.protocol.http.control.CookieManager;
 import org.apache.jmeter.protocol.http.control.Cookie;
 CookieManager manager = sampler.getCookieManager();
 Cookie cookie = new Cookie(".ASPXAUTH",props.get("MyCookie"),"<DOMAIN>","/",false,0);
 manager.add(cookie);

Rather than having a separate thread group to log users in, you can use a single thread group with a Once Only Controller. Add Samplers under the Once Only Controller to login and get the session cookie (which will of course only run once). From then on the thread group will just run the other samplers passing the session cookie to every request. I had the HTTP Cookie Manager at the Test Plan scope for this.


I should have been a little more clear in my question, but we got this fixed. Here is our solution:

Http Cookie Manager
Thread A - 1 Thread - 1 Loop Count
  - Login Page
      - BeanShell PostProcessor
            - props.put("MyCookie","${COOKIE_<INSERT ACTUAL COOKIE NAME>}");
Thread B - 50 Threads - Infinite Loop Count
  - BeanShell PreProcessor 
      - import org.apache.jmeter.protocol.http.control.CookieManager;
        import org.apache.jmeter.protocol.http.control.Cookie;
        CookieManager manager = sampler.getCookieManager();
        Cookie cookie = new Cookie("<INSERT ACTUAL COOKIE NAME>",props.get("MyCookie"),"<INSERT DOMAIN NAME>","<INSERT COOKIE PATH>",false,0);
        manager.add(cookie);
  - Page to hit
  - Another page to hit, repeat as needed

Then there is a configuration change for JMeter needed:

Open up the jmeter.properties file and go to the line "CookieManager.save.cookies=false" and make it = true.

This will allow the login cookie in the first thread to be used in the second thread.


None of the other solutions here worked for me, but they all had some part of the solution. Here's what finally worked to pass the cookie (JSESSIONID, in my case) from one thread group to the other. Note I did not need to set CookieManager.save.cookies to anything.

Http Cookie Manager
Thread A - 1 Thread - 1 Loop Count
  - Login Page
      - BeanShell PostProcessor
            - import org.apache.jmeter.protocol.http.control.CookieManager;
              import org.apache.jmeter.protocol.http.control.Cookie;
              import org.apache.jmeter.testelement.property.PropertyIterator;
              import org.apache.jmeter.testelement.property.JMeterProperty;
              CookieManager manager = ctx.getCurrentSampler().getCookieManager();
              PropertyIterator iter = manager.getCookies().iterator();
              while (iter.hasNext()) {
                  JMeterProperty prop = iter.next();
                  Cookie cookie = prop.getObjectValue();
                  if (cookie.getName().equals("JSESSIONID")) {
                      props.put("MySessionCookie", cookie);
                      break;
                  }
              }
Thread B - 50 Threads - Infinite Loop Count
  - BeanShell PreProcessor 
      - import org.apache.jmeter.protocol.http.control.CookieManager;
        import org.apache.jmeter.protocol.http.control.Cookie;
        CookieManager manager = sampler.getCookieManager();
        manager.add(props.get("MySessionCookie"));
  - Page to hit
  - Another page to hit, repeat as needed

Tags:

Jmeter