Handling Cookies in JMeter

Saving cookies value to Jmeter variable:

Change the property CookieManager.save.cookies=true in jmeter properties file.Jmeter.properties file is located in JMeter’s bin folder. you can use variable ${COOKIE_<CookieName>} in your script to use cookie value.

Adding specific cookies

In order to pass cookie value from one thread group to other we need to change CookieManager.save.cookies=true in jmeter properties file.Jmeter.properties file is located in JMeter’s binfolder.

1.Create Jmeter Test plan.

2.Create thread group 1 with one threads.

a.Create sample which you want to execute one.(for example log-in)

b.If you want to pass cookie information from this thread group to other thread group Add HTTP Cookie Manager and HTTP Header Manage to the request.

3.Create a bean shell post processor and create global variable by jmeter property and store the cookie value. Use below code in the bean shell processor.

props.put("MyCookie","${COOKIE_vwo}");

props.put("MyCookie1","${COOKIE_vwo_logged_in}");

props.put("MyCookie2","${COOKIE_vwola}");  

Create Thread Group 2.

a.Add cookie manager

b.Create Bean shell Preprocessor and use below code to add the stored Cookies.

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();

Cookie cookie = new Cookie("vwo",props.get("MyCookie"),"vwo.com","/",false,0)
manager.add(cookie);
Cookie cookie1 = new Cookie("vwo_logged_in",props.get("MyCookie1"),"vwo.com","/",false,0);
manager.add(cookie1);
Cookie cookie2 = new Cookie("vwola",props.get("MyCookie2"),"vwo.com","/",false,0);
manager.add(cookie2);

Adding all cookies

In thread group 1

1.  Add HTTP Cookie Manager and HTTP Header Mana

2. Create a bean shell post processor and create global variable and store the cookie value.    

3. Put below code to capture the cookies

import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();

props.put("cookiecount",String.valueOf(manager.getCookieCount()));
 for (int i=0;i<manager.getCookieCount();i++)
{
    //Code to convert Cookie information to JMeter Properties
   props.put("cookie_name" + i, manager.get(i).getName());
   props.put("cookie_value" + i, manager.get(i).getValue());  
}

1.   Add HTTP Cookie Manager and HTTP Header Manager.

2.   Create a bean shell preprocessor and add cookies to cookies manager by bean shell

3.    Put below code to add cookies in cookie manager.

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;

CookieManager manager = sampler.getCookieManager();
int count = Integer.parseInt(props.getProperty("cookiecount"));
log.info("Cookie Count="+count);
for (int i=0;i<count;i++)

{
Cookie cookie = new Cookie(props.getProperty("cookie_name"+i),props.getProperty("cookie_value"+i), "vwo.com","/",false,0);
manager.add(cookie);
}