Jmeter While loop - 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

Related

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,

Looping in JMeter based on condition

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.

Jmeter user Defined Variable

I'm running a test in Jmeter. that test sends in the end the status of the test - "success" or "fail".
I've created a 'user defined variable' that is named 'subject' and assigned it with value 'success'.
within the http requests I've put 'BeanShell Assertion' that assigns the 'subject' variable with 'failure' if the test failed:
if( (ResponseCode != null) && (ResponseCode.equals ("200") == false))
{
//Failure=true;
vars.put("subject","failure");
}
now, in the SMTP sampler I'm sending ${subject} as the subject of the mail.
the sampler doesn't recognise the variable (it is empty).
any ideas?
Can you show the screenshot of your Test Plan? I'm particularly interested in where Beanshell Assertion lives.
JMeter Assertions have their scope, i.e. given the following test structure:
Sampler 1
Assertion
Sampler 2
Assertion will be applied to Sampler 1
In case of the following test plan:
Sampler 1
Sampler 2
Assertion
Assertion will be applied to both Sampler 1 and Sampler 2
Nothing is wrong with your code, it should be setting "subject" variable in case of non "200" response code.
By the way, there is a pre-defined JMeter variable - ${JMeterThread.last_sample_ok} - which returns "true" if previous sampler was successful and "false" in the other case. It is better to use it in combination with the If Controller as Beanshell has known performance issues and can become a bottleneck in case of heavy loads.
The problem was that when using the 'Beanshell Assertion' I didn't pass it the variable - ${subject}, so when the test succeeded it was like the variable was never assigned.

Apache JMeter : How to compare responses of same HTML request during loop?

Please reply me with the solution for following problem:
With Apache JMeter I have ran the 2000 requests per 2 minutes with 10 loops.
The request is for checking hotel availability for mentioned start and end dates.
Following 3 important parameters are there:
Hotel id, Start date and End date
With the help of 'CSV data set Config' I have stored input from 2 text files (1 text file has hotel ids and another having start,End dates). and used variables into http requests.
By taking listeners View Result Tree and summary report, I checked the responses of each request. For some requests I am getting blank responses, as hotel is not available. Now I want to find out the exact count of the blank responses. Please let me know if anybody has solution for it.
Thanks in advance
You can use i.e. Size Assertion or Response Assertion to mark blank requests as failed ones.
If you just need just number and don't want to record failures on blank responses I would suggest using a Beanshell Post processor as follows.
Define a User Defined Variable called i.e. blank with the value of 0
Add a Beanshell Post Processor to each request which may produce blank response
At the end add a Beanshell Sampler which will report the final variable
Example Post Processor code:
int blank = Integer.parseInt(vars.get("blank")); //get current "blank" variable value
String response = new String(data); //get response data as string
if (response.length() == 0) // if response length equals zero (feel free to update this as requred)
{
blank++; // increment "blank" variable
}
vars.put("blank", String.valueOf(blank)); // update "blank" variable value
Example final Beanshell sampler code:
log.info("Pages with blank response count: " + vars.get("blank")); // print current "blank" variable value to "jmeter.log" file
You should see something like below in jmeter.log file:
2014/05/15 18:36:34 INFO - jmeter.util.BeanShellTestElement: Pages
with blank response count: 15
2014/05/15 18:36:34 INFO -
jmeter.threads.JMeterThread: Thread finished: Thread Group 1-1
2014/05/15 18:36:34 INFO - jmeter.engine.StandardJMeterEngine:
Notifying test listeners of end of test
2014/05/15 18:36:34 INFO -
jmeter.gui.util.JMeterMenuBar: setRunning(false,local)
See How to use BeanShell: JMeter's favorite built-in component guide for JMeter extension with scripting walkthrough and a kind of Beanshell cookbook.

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.