I\'m very new to JMeter and need your help on how to modify a cookie.
Here is the scenario: I\'m testing an assessment/test taking website that offers multiple answe
It is possible to modify or add a cookie manually in a groovy pre-processor script in the same way as https://stackoverflow.com/a/38505077/5747304.
Here's how to find and edit a cookie by browsing all cookies in the cookie manager:
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
log.info("#########################################################################");
// cookie manager
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
def NbOfCookies = manager.getCookieCount();
for (def i = 0; i < NbOfCookies; i++) {
log.info("Cookie n° " + (i+1) + ": " + manager.get(i).getName() + ": " + manager.get(i).getValue());
if (manager.get(i).getName() == "Cookie_name_to_find") {
log.info("MAJ of Cookie_name_to_find");
manager.get(i).setValue("New_cookie_value");
log.info("-> " + manager.get(i).getName() + ": " + manager.get(i).getValue());
}
}
log.info("#########################################################################");
Here is the list of cookie manager methods like add or delete ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/CookieManager.html.
Here is the list of cookie methods to modify more properties like the domain, its expiration date ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie .html
It should be known that according to the standart chosen in the cookie manager, the manually modified values can still be modified by the manager before the request so you have to be careful.
Use a Beanshell pre-processor or better a Jsr223 Pre-Processor with groovy and use code mentionned here:
Code:
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("<NAME>","<VALUE>","<HOST>","/",false,0);
manager.add(cookie);
I had to implement some changes in the code that worked for me:
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
Cookie cookie = new Cookie("<NAME>","<VALUE>","<DOMAIN>","<PATH>",false,0, true, true, 0);
manager.add(cookie);
Following the definition in http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie.html