Search code examples
jmeterjmeter-pluginsjmeter-5.0jmeter-4.0jmeter-3.2

Delete cookie in jmeter


Is there a way to reset(Delete the current cookies and allow JMeter to generate new ones) the cookies? I am using the below code to clear the cookies for my requirement. Now, this code is clearing the entire cookies. But when my script moves into the next loop(i am controlling iteration in a loop controller) it is not generating the new cookies. Probably this clear() methos will delete the entire cookie manager itself. Could you suggest?

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

To regenerate the cookie in the next loop, i want something like this?

import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = new CookieManager();
sampler.setCookieManager(manager);

Solution

  • We cannot suggest anything without seeing where do your cookies come from. The easiest option is to switch to the Thread Group from the Loop Controller, it has:

    1. Possibility to set the desired number of loops

    2. Possibility to choose whether to keep cookies/cache on the new iteration

      enter image description here

    Cookies come from Set-Cookie response header, if everything is fine with the cookie (domain/path matches current ones, not expired, etc.) it's being added to the Cookie Manager.

    You can enable debug logging and see where does the cookie come from and where it goes, it can be done by adding the next line to log4j2.xml file (lives in "bin" folder of your JMeter installation)

    <Logger name="org.apache.jmeter.protocol.http.control" level="debug" />
    

    More information: HTTP Cookie Manager Advanced Usage - A Guide

    In case of complete malfunction (which I doubt, HTTP Cookie Manager is reliably working for years already) you can always extract the cookie from the aforementioned Set-Cookie header and add them manually using HTTP Cookie Manager or HTTP Header Manager