How to change / add to Cookie in JMeter?
I am very new to JMeter and need your help on how to change the cookie.
Here's the scenario: I am testing an evaluation / testing website that offers multiple answers to questions. When the user makes their choice and removes the submit button, the JavaScript on the page adds its responses (for example, "Responses = BBAACDA ...") to the cookie and makes the next GET request (instead of a POST request!).
Since JMeter does not execute JavaScript (as mentioned in his tutorial, it is not a browser), it cannot add responses to the cookie. As a result, my test plan is unable to recognize user interaction.
How do I add / add / change a dynamic cookie? Thanks in advance!
- Ishti
source to share
Use a Beanshell or better Jsr223 Pre-Processor with groovy and use the code 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);
source to share
I had to make some changes to 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 at http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie.html
source to share
You can change or add a cookie file manually in pre-processor script groovy as well as fooobar.com/questions/2155469 / ... .
Here's how to find and edit a cookie by looking at 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 a list of cookie manager methods like add or remove ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/CookieManager.html .
Here is a list of cookie methods to change additional properties like domain, expiration date ...: http://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/Cookie .html
It should be known that according to the standard chosen in the cookie manager, manually changed values ββcan be changed by the manager before being requested, so you must be careful.
source to share