How to set a cookie in JMeter, which is usually set through Javascript?

I am testing a web application.
In the navigation menu of the main page, when you click node, the javascript function is called. I am viewing this function, the page will set document.cookie="current_moduleId=xxxx;path=/" before being redirected to the destination page.

So how can I set a cookie in JMeter for each request?

+4
source share
2 answers

Create the following panorama:

enter image description here

In the BeanShell pre-processor, put:

  import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache.jmeter.protocol.http.control.Cookie; CookieManager manager = sampler.getCookieManager(); Cookie cookie = new Cookie("toto","titi","localhost","/",false,0); manager.add(cookie); 

Please note that using JSR223 PreProcessor + Groovy + Caching would be better to execute

+6
source

For people who encounter this in the future, I had to use 0 or -1 to expire the cookie:

Cookie cookie = new Cookie("toto","titi","localhost","/",false,-1);

Any positive integers didn't seem to set a cookie

0
source

All Articles