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

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

Related

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

Running Jmeter for Https app with single user for multiple times through Loop controller

Attached is the snapI am trying to run jmeter for single user with value of 10 in loop controller , I first login into the app and then subsequently uses the CSRF token accordingly for future request .The login is always successful and generates CSRF token, But when i try to use CSRF token for future request , I get unauthorized HTTP 401 reponse code , when running with value 10 in loop controller for single user , the same works when i try to run single user with value 1 in loop controller , Any idea how to fix this? (Note :- All this is present in same thread Group)
Well-behaved JMeter test should be able to loop single user without any problem, most likely you have a problem with your test design or mismatch with the application under test behaviour. General recommendation would look like:
First of all switch to iterations on Thread Group level, JMeter configuration elements don't respect iterations originated from elsewhere as "iterations"
Add Debug Sampler and View Results Tree listener to your Test Plan
Run your test with 1 user and 2 loops
Inspect request and response details, identify missing/extra/different parameters and fix them. See How to Debug your Apache JMeter Script for more details on JMeter test troubleshooting techniques.
Blind shot: your test includes login request and the virtual user is already logged in therefore it doesn't require login on 2nd loop. In this case you have 2 options:
Put the logic which needs to be executed only 1 time under Once Only Controller
If you use HTTP Cookie Manager be aware that Clear cookies each iteration works only on Thread Group-level iterations

Logout not working in Jmeter script

I have been using JMeter for a while and I am facing a problem in particular related to logout. I am using the below script -
TestPlan
HTTP Cookie Manager
HTTP Request Defaults
ThreadGroup - (scheduled for half and hour)
Once Only Controller
HTTP Request - login
Loop Controller - (forever)
HTTP Request 1
HTTP Request 2
....
HTTP Request 13
tearDown Thread Group
HTTP Request - logout
But the problem here is that even the logout response looks good , the user used for JMeter script login is still actually not logged out. (that i can monitor as as Admin)
Most likely your "Logout" link just clears user's cookies via JavaScript. As per Apache JMeter project main page:
JMeter is not a browser. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever viewed at a time).
So Logout event can be simulated in JMeter by clearing cookies - see "Clear cookies each iteration" box in the HTTP Cookie Manager
NB: Iteration in the HTTP Cookie Manager (as well as in HTTP Cache Manager, etc) is something which is being defined and incremented at Thread Group level, loops triggered by Loop Controller, While Controller, etc. are not considered as "iterations"
It it also possible to clear cookies programatically i.e. from Beanshell Sampler

What is the use of HTTP Cache Manager and HTTP Cookie Manager for the Jmeter?

What is the use of HTTP Cache Manager and HTTP Cookie Manager
and how it performs if we enable/disable the options to clear them in each iteration?
Both are to make load test more realistic.
HTTP Cookie Manager - holds cookies which usually are being used for identifying user session including login information.
HTTP Cache Manager - represents browser cache. Real browsers download different embedded stuff like images, styles, scripts, etc. during rendering web page but do it only once or based on what specific cache control headers tell.
Clear above each iteration - if enabled new test cycle starts "clean" i.e. each virtual user will have to log in, fetch all the data, etc. so it'll look like a new user to the system under test. If disabled - it'll just be another test iteration for the same virtual user
So if your idea is to make your test more close to real browser testing you need to use above test elements in combination with HTTP Header Manager and configuring HTTP Request Defaults to tell all the HTTP Samples to download embedded resources and use concurrent pool of 2-5 threads for it.
You should read those 2 docs:
http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Cache_Manager
http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Cookie_Manager
Question?
To stimulate browser behavior more realistic do we have to enable "Use Cache-Control/Expires header when processing get request"? during Load Test.
I am asserting the response for get request. If is use this then is there any chance for me to get null response?
I was confused with the detail gathered?

Why is only my first HTTP request running?

I'm still pretty new to jmeter. I'm using jmeter 2.9.
I have created a thread group with two threads in it. I have set up an HTTP Request Defaults for the thread group. I have created User Parameters to define person IDs that I'm going to be working with in each thread.
I then set up two HTTP requests. One for login, one for retrieve person. For the login one, I created a regular expression extractor to extract the session key that comes back. I then supply the session key as the parameter for the retrieve person request.
I then sniffed the net traffic and started jmeter test. I wanted to make sure the session key was getting passed into the second parameter OK, but it appears to be running on the login twice and then ending. I am getting a 200 return on the HTTP packet and my jmeter test was set to continue regardless.
I believe I followed chapter 5 of the jmeter user manual to set up this test with the addition of parameters, user parameters, and the regex extractor.
Can anyone give me an idea what i might have done so that the second request never runs?
Add "View Results Tree" listener to see what jMeter is actually doing? You should also check jmeter.log to see if there are any exceptions while generating the 2nd request.

Resources