20

I have the following setup:

Thread A
  - Http Cookie Manager
  - Login Page
Thread B
  - Http Cookie Manager
  - Page to hit
  - Another page to hit

I've been trying to get the cookie that is set in the Login Page to a global property and then set it to the Http Cookie Manager in Thread B. However I've been unable to get it right. I've tried BeanShell PostProcessors and such, but haven't been able to get it set up correctly.

=== Add for clarification ===

The reason I can't have the log in page and cookie manager in Thread B is due to the behavior desired. The goal is to log in, get the cookie, and then hit a bunch of pages over and over again. This gives us the ability to log in once and then simulate a lot of hits from that user. By putting the cookie manager and log in page into the same thread the user would log in, hit a bunch of pages once and then log in and do it again.

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47

9 Answers9

32

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.

Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47
  • @Clarence Klopfstein, I used the method you did, and I'm able to see the same cookies' names and values, but the requests I use says now "[no cookies]". Any idea? – Shai Alon Jan 17 '17 at 14:08
  • @Clarence Klopfstein, Hi Thank you for the answer, this works if i have only one field in my cookie, how can i add more than one field in the cookie. – Vishal Puliani Jan 19 '17 at 14:05
  • @VishalPuliani See more details [here](https://www.blazemeter.com/blog/how-use-beanshell-jmeters-favorite-built-component), under Passing Cookies between Thread Groups – Adrian Oct 31 '18 at 12:08
  • Solution still works, but make sure when you set the `CookieManager.save.cookies` to remove the `#` comment at the beginning of the line. I think some people might not be doing that and thinking the solution doesn't work. – Breakpoint25 Mar 24 '22 at 18:35
16

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
aaron
  • 713
  • 9
  • 9
4

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.

Daniel Metcalfe
  • 165
  • 4
  • 11
1

I have the same use-case as you but I think there's a simpler solution: inside the thread group you can use a loop controller.

So...

Thread Group
   Login
   Loop Controller
     - Page to hit
     - Another page to hit

That said I'm still going to use the trick you describe, because I think we will want to log in once, then hit several different pages in parallel from different thread groups. So we will log in first, then several different thread groups will hit the server simultaneously. So your trick is definitely useful. But for simple cases, a loop controller can do it I think.

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Emmanuel Touzery
  • 9,008
  • 3
  • 65
  • 81
1

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);
d.popov
  • 4,175
  • 1
  • 36
  • 47
0

The answer above really helped me but the HTTP cookie Manager was missing the detail that I required to get it to work. By setting the cookie manager as below, it worked for me

Http Cookie Manager
NAME   $<ACTUAL_COOKIE_NAME>
VALUE  ${COOKIE_<INSERT_ACTUAL_COOKIE_NAME>}    
DOMAIN ${SITE}   
PATH    /
Thread A - 1 Thread - 1 Loop Count
....
Thread B - 50 Threads - Infinite Loop Count
....
ConorFi
  • 1
  • 1
0

This has worked for me.

Http Cookie Manager
Thread A - 1 Thread - 1 Loop Count
  - Login Page 
      here you get the cookie from the browser and save it to a variable MY_COOKIE)
      - JSR223 PostProcessor
         props.put("MY_COOKIE",vars.get("MY_COOKIE"))
        
Thread B - 50 Threads - Infinite Loop Count
  - JSR223 PreProcessor 
      vars.put("MY_COOKIE",props.get("MY_COOKIE"))
  - Page to hit
 
Fede Ogrizovic
  • 159
  • 2
  • 2
0

Why not to add Http Cookie Manager to the Test Plan level instead of Thread Group one and group all the samplers in one thread group?

Test Plan
    Http Cookie Manager
    Thread Group
        - Login Page
        - Page to hit
        - Another page to hit

This should solve your problem without any other extra samplers.
Or maybe there are some objective reasons to implement the way you've done?

...Please look onto this too.

Community
  • 1
  • 1
Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
  • 1
    I'll post our solution in a bit, but we got this working. To answer your suggestion though, the idea was to login once and do a lot of stuff and see how it performs. Your method would require a login each and every iteration. Not a realistic load test. – Clarence Klopfstein Mar 22 '12 at 17:52
  • Putting the HTTP Cookies Manager on the Test Plan level didn't work for me... :( – Shai Alon Jan 16 '17 at 14:03
-2

for more than one field in cookie i tried this n its works

   BeanShell Post Processor 
   props.put("MyCookie","${COOKIE_<FIRST    FIELD>}");
   props.put("MyCookie1","${COOKIE_<SECOND FIELD>}");

  BeanShell Pre Processor

  import org.apache.jmeter.protocol.http.control.CookieManager;
  import org.apache.jmeter.protocol.http.control.Cookie;
  CookieManager manager = sampler.getCookieManager();
  Cookie cookie = new Cookie("<FIRST FIELD>",props.get("MyCookie"),"DOMAIN","/",false,0);
  manager.add(cookie);
  Cookie cookie1 = new Cookie("<SECOND FIELD>",props.get("MyCookie1"),"DOMAIN NAME","/",false,0);
  manager.add(cookie1);
MJS
  • 1
  • 3