Sharing cookie values between thread groups in JMeter

前端 未结 8 2127
暗喜
暗喜 2020-12-08 11:32

I have the following setup:

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


        
相关标签:
8条回答
  • 2020-12-08 11:52

    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);
    
    0 讨论(0)
  • 2020-12-08 11:59

    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
    
    0 讨论(0)
  • 2020-12-08 11:59

    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.

    0 讨论(0)
  • 2020-12-08 12:01

    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
    ....
    
    0 讨论(0)
  • 2020-12-08 12:03

    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.

    0 讨论(0)
  • 2020-12-08 12:04

    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.

    0 讨论(0)
提交回复
热议问题