JMeter – HTTP Cookie Manager

JMeter - HTTP Cookie Manager

What is a Cookie?

As per definition, Cookies are text files stored on the client computer and they are kept for various information tracking purpose. Generally, a cookie is added to the response from the servlet and it is stored in the cache of the browser. After that, if a request is sent by the user, a cookie is added to the request by default. Thus, the server recognizes the user as the old user. That is why the secured application recorded script is failed during replay if you do not handle the cookie using JMeter – HTTP Cookie Manager.

How to handle cookie in Apache JMeter?

Cookies are handled in JMeter using ‘HTTP Cookie Manager’. Using this config element you can simulate browser activities. HTTP Cookies Manager does work in two ways:

  1. Stores and sends: If an HTTP request and the response contains a cookie, then Cookie Manager automatically stores that cookie and will use it for all future requests to that particular web site. In this way, it simulates the real browser. Each JMeter thread has its own “cookie storage area”. Also, one more good thing in JMeter is that you can store received Cookies in a JMeter thread variable. To save cookies as variables, define the property “CookieManager.save.cookies=true”. Just for a good practice you can provide cookies names are prefixed with “COOKIE_” before they are stored (this avoids accidental corruption of local variables). To revert to the original behaviour, define the property “CookieManager.name.prefix= ” (one or more spaces). If enabled, the value of a cookie with the name TEST can be referred to as ${COOKIE_TEST}.
  2. Add Manually: You can manually add a cookie to the Cookie Manager. However, if you do this, the cookie will be shared by all JMeter threads.  

Cookie Manager Properties: JMeter allows altering the default behaviour/configurations of cookies manager by changing its properties value for example:

  • save.cookies=true/false (set it to true if you want to keep cookies as variables)
  • check.cookies=true/false (set it to false if you want to store cross-domain cookies and might use them later)
  • name.prefix=true/false
  • delete_null_cookies=true/false

How to add ‘HTTP Cookie Manager’?

You can follow the below steps:

  1. Select the ‘Test Plan’ node
  2. Right-click on the ‘Test Plan’ node
  3. Hover the mouse on ‘Add’
  4. Hover the mouse on ‘Config Element
  5. Click on ‘HTTP Cookie Manager’
JMeter - HTTP Cookie Manager
Figure 01: Adding ‘HTTP Cookie Manager’

What are the input fields of ‘HTTP Cookie Manager’?

JMeter - HTTP Cookie Manager
Figure 02: JMeter – HTTP Cookie Manager
  • Name: To provide element name
  • Comments: To provide arbitrary comments (if any)
  • Clear cookies each iteration?: If this option is selected, then the cookie is cleared at the start of the thread. Any cookie defined in the GUI is not cleared.
  • Use Thread Group configuration to control cookie clearing: This option provides the ability to configure whether a new iteration is with a new user or with the same user. It is mapped with the option ‘Same user on each iteration’ in the Thread group.
  • Cookie Policy: The cookie policy that will be used to manage the cookies and options are:
    • standard
    • standard-strict
    • ignoreCookies
    • netscape
    • default
    • rfc2109
    • rfc2965
    • best-match
    • compatibility
  • User-Defined Cookies: This option is used for adding cookie manually. This gives you the opportunity to use hardcoded cookies that will be used by all threads during the test execution. You can add the value of ‘Name’, ‘Value’, ‘Domain’, ‘Path’ and ‘Secure’.

Leave a Reply

Your email address will not be published. Required fields are marked *