June 28, 2014

Using the HTTP Cookie Manager in JMeter

Open Source Automation
Back to top

Depending on your web-application complexity, you can create JMeter test-plans of two types: hits-oriented and scenario-oriented.


For creating a hits-oriented test-plan you need to have median statistics. These statistics should contain information about how many separate requests per second your application should handle. It is very simple case and it works for many web-sites. But, there are still a large number of much more complex applications. You cannot, for example, get a general report from Internet-banking using a direct link; you need to first login to the application and then navigate to certain page.


For this purpose browsers store these sweetly named “cookies”, here (unfortunately) not a delicious baked good, but rather pieces of text that contain information about login sessions. JMeter also has a tool for handling these many cookies collected along the way aptly named the HTTP Cookie Manager.

Back to top


Back to top


If you have an HTTP Request and the response contains a cookie, the Cookie Manager automatically stores that cookie and will use it for all future requests to that particular web site. Each JMeter thread has its own "cookie storage area". So, if you are testing a web site that uses a cookie for storing information for particular sessions then each JMeter thread will have its own session. **Note that such cookies do not appear on the Cookie Manager display, but they can be seen using the View Results Tree Listener.


Back to top

2. Received Cookies can be stored as JMeter thread variables (versions of JMeter 2.3.2+ no longer do this by default). 


To save cookies as variables, define the property "CookieManager.save.cookies=true".  The names of the cookies contain the prefix "COOKIE_" before they are stored (this avoids accidental corruption of local variables). To revert to the original behavior, define the property "CookieManager.name.prefix= " (with one or more spaces). If enabled, the value of a cookie with the name TEST can be referred to as ${COOKIE_TEST}.


Back to top


Note that if you do this, the cookie will be shared by all JMeter threads. Such cookies are created with an expiration date far in the future.


A bit of practicality: the HTTP Cookie Manager in action.


HTTP Cookie Manager in action


Let's say you have to test a bug-tracking system. The testing scenario will consist of a “Login Once Only” operation that should be performed, obviously, only once for each new user. “Creating project”, creates a new project in BTS. So we have placed the request for login into the "Only Once Controller". It means that login request will be executed only once. After that, cookies will be saved by the HTTP Cookie Manager and used for future requests.


Now we’ll execute scenario one time with one user. Then we’ll switch to View Results Tree and see, whether JMeter processes cookies or not.           


switch to View Results Tree and see, whether JMeter processes cookies or not.

You can see that during POST request JMeter caught cookie. Now we have to check whether this cookie is applied to following requests or not.


 POST request JMeter caught cookie


Note that in the additional request, PHPSESSID has the same value as in the request where it was received directly from the server.

HTTP Cookie Manager works as it should. No crumbs either.

Learn more about JMeter through BlazeMeter University



Related Resources:

Back to top