Looping in JMeter based on condition - while-loop

I have a scenario where a HTTP request needs to be executed multiple times in two loops as said below:
-Thread Group
-While Controller
-While Controller
-HTTP Request
-BeanShell Post Processor(1)
-BeanShell Post processor(2)
HTTP Request:
https://xx.xxx.xx.xx/api/v1/xx/utilization?starttime=${starttime}&endtime=${endtime}&start=${mycounter}&limit=100
In first BeanShell Post Processor, I am incrementing only "mycounter" and I am expecting the while loop above that to execute till it goes false.
In the second BeanShell Processor, I am incrementing "startime" & "endtime" and I am epecting the while loop above it to execute till it goes false.
But, Observing that thinsg are not working as expected. Does my undertanding is correct. I see that the beanshell processors are not executing as per the WHile loops they fall under.
Any comments/Suggestions??

Add For Loop controller to the plan.
Please refer this link.

Related

I am automating a login script for perdormance teating in j meter

I want to make script using jmeter for performance testing of login page . The authorization type is code and code challenge method is sh256. How could I fetch code challenge code verifier and state or noance values daynamically.
The script is successfull for 1 single user but failing for multiple can any one help? Also I am using blazemeter to record script..
The process of "fetching" dynamic values is known as correlation and there is a lot of information on the topic in the Internet, i.e. How to Handle Correlation in JMeter
The main steps are:
Use a suitable JMeter Post-Processor to extract a dynamic value from the response into a JMeter Variable
Replace recorded hard-coded value with the JMeter Variable from the previous step

get request for search in JMeter

I am performing a search request in jmeter. So my test plan flow is home then login then product catalogue and then search. I tried to make a post request for search but it failing all the time. I used a CSV file so each time the query is changed. But then I used a get request and used the query variable in the search path like this search?query=${search_input}and then it passed but when i checked the html it is not the correct page. In the html response I also see this
{{noSearchResults.query}}'. But if i put the url on the browser it works fine. Can you please help me with this?
Double check that your ${search_input} variable has the anticipated value using Debug Sampler and View Results Tree listener combination
It might be the case that your ${search_input} variable contains special characters which need to be URL-encoded so you might need to wrap the variable into __urlencode() function like:
search?query=${__urlencode(${search_input})}
JMeter automatically treats responses with status code below 400 as successful, if you need to add an extra layer of check for presence of the results or absence of {{noSearchResults.query}} - use Response Assertion

Jmeter While loop

I'd like to perform a while loop using jmeter. Within the loop I'm using xpath extract to pull information from the server response, and storing it in a variable. I'd like to quit the loop if that variable has any data in it (if the request has been successful) - otherwise I'd like to fail if it doesn't respond correctly in x number of attempts. Is this something that JMeter can do?
I found a solution for this
If you know the response that you are trying to extract from Xpath extractor,
With the help of response assertions and while loop its possible..
here is my answer
First of all add a beanshell sampler to the test plan before while loop.
In the beanshell sampler add the following 2 lines
vars.put("counter","1");
vars.put("txtFound","FALSE")
Next Add a While controller with the following condition
${__javaScript("${txtFound}" == "FALSE" && parseInt(${counter})<=3,)}
Above expression evaluates to true if both conditions are true.here 3 represents the number of attempts.
Now in the while loop add your request .
To the same request add a response assertion and add the pattern(the text you are trying to extract using Xpath)
to the same request add a beanshell post processor and copy the following code to it
int counter = Integer.parseInt(vars.get("counter"));
if(counter==3)
vars.put("txtFound","TRUE");
counter++;
vars.put("counter",Integer.toString(counter));
in above code 3 represents number of attempts.
The code will increment number of attempts by one for each iteration and if it reaches max attempts it sets txtFound to TRUE to stop the test.
Add an if condition below the request as shown below
In if loop add a bean shell sampler and set the txtFound value to TRUE as shown below
When response assertion fails if condition will not be executed and if the response assertion passes if condition is set to true and the elements in If will be executed
The test stops if it finds the correct response ant time or it will stop if it reaches max number of attempts
In my case i kept 3 as response assertion so if it finds 3 it will stop or if it reaches max number of 3 attempts
Please follow this link for more information on
while controller

Counting the number of response codes in JMeter 4.0

I run some load tests (all endpoints) and we do have a known issue in our code: if multiple POST requests are sent in the same time we do get a duplicate error based on a timestamp field in our database.
All I want to do is to count timeouts (based on the message received "Service is not available. Request timeout") in a variable and accept this as a normal behavior (don't fail the tests).
For now I've added a Response Assertion for this (in order to keep the tests running) but I cannot tell if or how many timeout actually happen.
How can I count this?
Thank you
I would recommend doing this as follows:
Add JSR223 Listener to your Test Plan
Put the following code into "Script" area:
if (prev.getResponseDataAsString().contains('Service is not available. Request timeout')) {
prev.setSampleLabel('False negative')
}
That's it, if sampler will contain Service is not available. Request timeout in the response body - JMeter will change its title to False negative.
You can even mark it as passed by adding prev.setSuccessful(false) line to your script. See Apache Groovy - Why and How You Should Use It article fore more information on what else you can do with Groovy in JMeter tests
If you just need to find out the count based on the response message then you can save the performance results in a csv file using simple data writer (configure for csv only) and then filter csv based on the response message to get the required count. Or you can use Display only "errors" option to get all the errors and then filter out based on the expected error message.
If you need to find out at the runtime then you can use aggregate report listener and use "Errors" checkbox to get the count of failure but this will include other failures also.
But, if you need to get the count at the run time to use it later then it is a different case. I am assuming that it is not the case.
Thanks,

jmeter help - test around polling /w meta refresh

I am new to jmeter and am working on putting together a test plan. The hurdle I've encountered is as follows:
First, a POST is made to processForm.aspx
Then, the user is redirected to pleaseWait.aspx
This page either redirects immediately to results.aspx OR loads, with a META REFRESH tag set to refresh in 5 seconds (and this step is repeated).
Now -- I can get this to execute by doing the following:
HTTP Sampler POST to processForm.aspx
Assert Response contains "<something on pleaseWait.aspx>"
While LAST
HTTP Sampler GET to pleaseWait.aspx
Assert Response contains "<something on results.aspx>"
However -- I don't care for this method, because it results in failed assertions (even though things are working as expected). I am sure there must be some other way to do this? Anyone more familiar with JMeter than I?
UPDATE:
Got it going by using Regular Expression Extractor instead of Assertions.
1) Add a User Defined Variables section at Test Plan Root
2) Add a variable to it "LoginWait" and "false"
HTTP Sampler POST to processForm.aspx
RegEx Extract Response Body contains "<something on pleaseWait.aspx>" into LoginWait
While ${LoginWait}
HTTP Sampler GET to pleaseWait.aspx
RegEx Extract Response Body contains "<something on pleaseWait.aspx>" into LoginWait
...
You could try using "follow redirects" on your HTTP Request. It would eliminate the logic you need, and still get you to the page you're going.