AsadYarKhan
AsadYarKhan

Reputation: 688

How to clear JSESSIONID of each Thread (user) in JMeter on demand

I want to clear the 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?

Upvotes: 6

Views: 4716

Answers (3)

Max Fesenko
Max Fesenko

Reputation: 143

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.

Upvotes: 1

Alaa Murad
Alaa Murad

Reputation: 166

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);
    }
}

Upvotes: 7

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

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:

Upvotes: 2

Related Questions