-->

how to clear JSESSIONID of each Thread(user) in Jm

2020-06-17 07:06发布

问题:

I want to clear Jmeter JsessionID variable at any time (on my demand).

I know there is a check box option in Jmeter CookieManager named "Clear Cookie on each Iteration".
But it clears the session on each iteration while I want to clear it at any time in the iteration.

How can i do that in Jmeter?

回答1:

Currently you cannot simply , particularly if you want to clear one particular cookie.

You should raise an enhancement request at JMeter Bugzilla giving precision on what you want to do.

I think a custom function would be a nice feature, see:

  • https://issues.apache.org/bugzilla/show_bug.cgi?id=53976


回答2:

You can, just add post/pre process beanShell and with this code

import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
for (int i=0;i<manager.getCookieCount();i++){
    Cookie cookie = manager.get(i);
    //remove a cookie 
    if (cookie.getName().equals("BAD_COOKIE")){
        sampler.getCookieManager().remove(i);
    }
}


回答3:

My way is not far from the above one (which didn't work for me, sorry), but it is shorter, contains important update on index inside the loop, and some additional demo for clearing script usage (I hope ;) )

JSESSIONID is one of tokens (first or some of subsequent), thus in order to delete all the tokens including JSESSIONID I would propose to use the following Java script in JSR223 Pre- and/or PostProcessor where you need:

import org.apache.jmeter.protocol.http.control.CookieManager;

CookieManager cManager = sampler.getCookieManager();
    int count = cManager.getCookieCount();
    for (int index = 0; index < count; index++) {
        cManager.remove(0);
        }

Example of adding the script to PostProcessor in jMeter

Pay attention: inside for loop here is (0), not (index), that helps to avoid OutOfBoundary exception, because size of the CookieManager instance comes smaller after each iteration.