Welcome to OGeek Q&A 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 Reply

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...