Shai Alon
Shai Alon

Reputation: 990

Jmeter passed cookies to different thread group but still get requests with [no cookies]

I have a question where I saw lots of various questsions and answers about this but nothing worked for me so far.

Please read it to the end to see it's not a duplicated post.

I'm using 2 thread groups which are using cookies. Since cookies are not being shared across thread groups, I used the method to "export" them into properties and "import" them back in the 2ns thread group with the coookie class of the CookieManager. I placed the same http request on the 1st and 2nd thread groups. Obviously it works on the first, but on the 2nd, I see it uses [no cookies].

The test structure is as follows:

HTTP Cookie Manager
Thread A - 1 thread, 1 loop
 - Login page
    - Beanshell PostProcessor
        props.put("ASPXAUTH_Cookie","${COOKIE_.ASPXAUTH}");
        props.put("ASP.NET_SessionId_Cookie","${COOKIE_ASP.NET_SessionId}");
    - Page X to hit - success
Thread B - 6 thread, 1 loop
 - Login page
    - Beanshell Prerocessor
        import org.apache.jmeter.protocol.http.control.CookieManager;
        import org.apache.jmeter.protocol.http.control.Cookie;
        import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
        HTTPSamplerProxy sampler = ctx.getCurrentSampler();
        CookieManager manager = sampler.getCookieManager();
        Cookie cookie1 = new Cookie(".ASPXAUTH",props.get("ASPXAUTH_Cookie"),"","/",false,0);
        manager.add(cookie1);
        Cookie cookie2 = new Cookie("ASP.NET_SessionId",props.get("ASP.NET_SessionId_Cookie"),"","/",false,0);
        manager.add(cookie2);
    - Same Page X to hit - failure

First results for page X:

GET http://Mysite/pages/MyView.aspx?subRoute=views&pageid=7168&routeName=dashboards

Cookie Data:
.ASPXAUTH=9CB09DD7A906ED67027E9B1ED71C707A498B8234C737169AA3EF1164890A217D9CCD1F1B32736338A7DFBF3DE0A07AD45F170A0F11FE49C827581C584577372D2D3C7D4F52DE1B73D3DE9A1263150B85F302DD01B9CF93AF380F5C63F618634CEEB5C3CFD95F1081EC28F5A556B21EAA; ASP.NET_SessionId=rg4dxqm1xai4wovvfvyezggo

Request Headers:
Connection: keep-alive
X-DevTools-Emulate-Network-Conditions-Client-Id: 08a74297-b666-496a-a1cb-620054bc5db5
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://Mysite/pages/ui/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2
Host: load-rec-8

Second results for page X (which is on the 2nd thread group):

GET http://Mysite/pages/MyView.aspx?subRoute=views&pageid=7168&routeName=dashboards

[no cookies]

Request Headers:
Connection: keep-alive
X-DevTools-Emulate-Network-Conditions-Client-Id: 08a74297-b666-496a-a1cb-620054bc5db5
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://Mysite/pages/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4,fr;q=0.2
Host: load-rec-8

Please don't answer with "don't use different thread groups" since I have to use only 1 login (1st thread group) while the 2nd thread group should run the same request for 6 users in parallel at the same time.

Whatever tests I did with lots of other posts about this subject didn't work.

Any idea please?

Upvotes: 0

Views: 771

Answers (1)

Dmitri T
Dmitri T

Reputation: 168147

Most probably your problem is that you don't provide "domain" when creating a new cookie instance, you need to pass the domain which matches your application under test location (what you have in the "Server Name or IP" section of the HTTP Request sampler), i.e. if you are load testing http://example.com domain, provide .example.com when you are creating the cookie via Beanshell

In general given you copy cookie names and values, you can also copy all the remaining attributes as well. See How to Use BeanShell: JMeter's Favorite Built-in Component for example code.

Other enhancements:

  1. Replace ${COOKIE_.ASPXAUTH} with vars.get("COOKIE_.ASPXAUTH");
  2. Below lines can be removed, they are not required, Beanshell PreProcessor already has sampler pre-defined variable

    import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
    HTTPSamplerProxy sampler = ctx.getCurrentSampler();
    

Upvotes: 1

Related Questions