JMeter can not destroy session - jmeter

I want to destroy my session with jmeter. I am using following code from my logout sampler as post beanshell processor:
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager cManager = sampler.getCookieManager();
for (int i=0;i<cManager.getCookieCount();i++){
sampler.getCookieManager().remove(i);
}
But the cookies are not destroyed.
What am I doing wrong?
Best regards,
Peter

Any reason for not using "Clear cookies each iteration" box?
I'm also not too sure that it's the right way to delete cookies, I would go for clear() method, something like:
CookieManager cManager = sampler.getCookieManager();
cManager.clear();
sampler.setCookieManager(cManager);
Also cookie manager scope matters, depending on what you're trying to achieve it may worth moving the HTTP Cookie Manager to the root test plan level or vice versa, let each HTTP Request sampler have its own cookie manager as its child. See Using the HTTP Cookie Manager guide for more details.

Related

How to clear cookies and cache in jmeter after each iteration?

The scenario is- After logging out from application either i have to close the browser or have to clear the cache in order to login with same user again, else browser will not give me login page and it will directly redirect me to post login.
When i am running my script from jmeter, its failing for 2nd iteration as its not able to find out the login page.
what needs to be done in jmeter for handling this scenario? i have added http cache and cookie manager in test plan.
I already tried
1.JSR223 Post Processor- sampler.getCacheManager().clear()
2.BeanShell PostProcessor- import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
manager.clear(); for all 3 requests of Logout transaction.
There is Same user on each iteration setting on Thread Group level.
If you untick it - both HTTP Cookie Manager and HTTP Cache Manager will be cleared when a thread (virtual user) starts new iteration. (only applies to Thread Group's iterations, controllers like Loop, Foreach, While, etc. won't be impacted)
If you want to proceed with scripting and copy-pasting the code without understanding what it's doing for JSR223 Post Processor the relevant code would be:
prev.getCacheManager().clear()
prev.getCookieManager().clear()
but make sure to place it as a child of the last HTTP Request sampler, see JMeter Scoping Rules - The Ultimate Guide article for more information on the impact of placement of JMeter test elements

Keep session with Cookies in Jmeter

I am trying to run a simple test in jmeter but i am stuck. The steps are:
Log in
Set the Cookie Manager
Access the landing page
The first 2 steps are successful, but i get status 403 "errorId":"AUTHENTICATION_REQUIRED" on the 3rd step. My guess is it is not getting the session from log in, but everything i 've tried hasn't worked. Here is my test plan:
Any suggestions?
JMeter's HTTP Cookie Manager obeys JMeter Scoping Rules, so if you put it as a child of the Setting Cookies request it will be applied to that request only, therefore cookies will not be available to the Landing Page
Try moving the HTTP Cookie Manager one level higher so your test plan would look like:
You should be able to see which cookies are being received and sent out using View Results Tree listener

JSR223 sampler jmeter / Passing cookie data

I am trying to simulate a parallel ajax requests using a JSR223 sampler, as mentioned here https://www.blazemeter.com/blog/how-load-test-ajaxxhr-enabled-sites-jmeter
But for my set of requests ,I am getting an error,Invalid API/Auth Key
I assume it is is becuase the authentication cookie is not being passed ,I tried to grab the cookie from the previous sampler using
HTTPSamplerProxy previousSampler = ctx.getPreviousSampler();
CookieManager cookieManager = previousSampler.getCookieManager();
HTTPSampleResult previousResult = (HTTPSampleResult)ctx.getPreviousResult();
log.info("Cookie Count is : "+ cookieManager.getCookieCount());
But I get the error
Cannot invoke method getCookieCount() on null object
,I do have the cookie manager enabled in my test plan.
Any help on what I am doing wrong would be great .
The error you're getting means that there is no HTTP Cookie Manager associated with the sampler. You need to add it to your Test Plan and your code should start working as expected.
Be aware that as of now there is a much easier way to implement AJAX requests without having to do any coding, there is Parallel Controller which can be used to mimic AJAX calls by running its children in parallel. Just add it to your Test Plan and move HTTP Request samplers which represent AJAX calls under it. See How to Use the Parallel Controller in JMeter for more details if needed.

Add Session ID to each JMeter HTTP request

For my JMeter test, I would like all threads in the test to use one shared session id. The session id is known before I start the test job. How do I specify a specific JSessionId for my JMeter test?
I've tried appending a JSessionId parameter in the HTTP Cookie Manager (under user-defined cookies), in the HTTP Request Defaults (under send parameters with the request), and I tried adding a 'HTTP URL Re-writing Modifier' from the Pre-Processor tab under the Recording Controller. None of these have worked. Which step is the best way to set a fixed session id for my JMeter test?
You were correct when you tried to use the HTTP Cookie Manager, this should do what you need.
Probably, it didn't work for one of two reasons:
You either have the Cookie Manager in the wrong place. To affect all requests it should be at the root of the Test Plan or Thread Group.
Or, more likely, you didn't specify all the required values properly when you added the User Defined Cookie. This can be tricky to get working; have you made sure you're not using 'http://' in the domain field?
You need to add jp#gc-JSON Path Extractor in HTTP request, the particular HTTP request which is generating session id in its response data .
If response is like:
{"sessionId":"f5b06970-f00f-4b44-89c8-305738e19cba","loginDate":1483599209337}
In JSON path extractor add:
Variable name - session (variable in which session id will be stored)
JSON Expression - $.sessionId (this will vary according to the JSON response)
Default Value - NOT_FOUND
Now the next step is to use this variable named "session". You can use it in your HTTP request for which you need to pass session id under HTTP header manager as x-auth-token = ${session}

jsessionID not kept between jmeter http requests

I'm trying to do a simple load-test in a website which requires you to log in first.
I read the jmeter documentation about the cookie manager and looks like as long as I make all my requests within the same thread group where the cookie manager is the sessionID is shared among the http requests but is not my case.
The simple structure I have is:
Thread Group
HTTP Cookie Manager
HTTP Requests Defaults
Simple Controller
http request // POST method to authenticate in the site (this works fine, I get a session id)
http request // GET method (this one should use the same session id from the previous http requests but I get a new one instead)
I did this same web page flow in firefox using firebug to see the requests/responses and the session id is the same, the server doesn't send a new one so looks like the cookie manager is not keeping the same session id and using it for all the requests.
Can someone point me in the right direction? What I am doing wrong?
Check the get request sends the same jsessionid cookie in the request as the one returned in previous response.
If it's the case then check your login was fine, as it is probably root cause of issue

Resources