Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

jmeter cookie deletion issue programmatically

I am trying to clear cookies programmatically using the following code. But it is completely removing cookies and acting as "no cookies" from the second iteration. I want to store the cookies for the current iteration and from the next iteration, it should start as fresh again. Can you suggest any modifications here?

My requirement is just to delete cookies after every iteration. HTTP cookie manager is not working(i tried at both test plan and thread group level). So trying programatically.

import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.commons.lang3;
CookieManager manager = ctx.getCurrentSampler().getCookieManager();
for (int i = 0; i <= manager.getCookieCount(); i++) 
{
     manager.clear();
     ctx.getCurrentSampler().setCookieManager(manager);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Your code won't compile due to extra }
  2. Depending on the context ctx.getCurrentSampler() may refer to any sampler and getCookieManager() function will return something only for HTTP Request sampler and only if it is in scope of a HTTP Cookie Manager

In general the official and the best way to clear cookies on each iteration is:

  1. Either unticking Same user on each iteration box on Thread Group level

    enter image description here

  2. Or ticking Clear cookies each iteration on HTTP Cookie Manager level

    enter image description here

Both options assume iterations of the Thread Group, anything else (iterations coming from a Loop Controller or While Controller) will be ignored. If this is your use case - add JSR223 PreProcessor as a child of the very first HTTP Request sampler and use just this one-liner:

sampler.getCookieManager().clear()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...