Ignore a global response assertion in Jmeter - jmeter

Assume the structure below:
Thread Group
-Simple Controller
-Global Response Assertion
-Http Sampler1
-Http Sampler2
-Http Sampler3
-Http Sampler4
etc
So the global response assertion will work on all the samplers below it. However, I need a way for a specific sampler to ignore the global assertion so for example i want HTTP sampler4 not to be asserted by the global assertion and to completely ignore it.
Thanks everyone.

Given you have beanshell in tags my expectation is that your Global Response Assertion is a Beanshell Assertion. If so - you can "ignore" the Http Sampler4 as follows:
if (SampleResult.getSampleLabel().equals("Http Sampler4")) {
// do nothing
}
else {
// your "global assertion" logic here
}
Also be aware that starting from JMeter version 3.1 it is recommended to use Groovy for any form of scripting in JMeter so consider migrating to JSR223 Assertion on next available opportunity.
See Apache Groovy - Why and How You Should Use It guide for more details.

You can rearrange your structure so that the response assertion isn't in the same level for the HTTP sampler that you don't want to have assertion on it, like below.
Thread Group
-Simple Controller
-Global Response Assertion
-Http Sampler1
-Http Sampler2
-Http Sampler3
-Simple Controller
-Http Sampler4
-Simple Controller
-Global Response Assertion
-Http Sampler5
-Http Sampler6
-Http Sampler7
etc

Related

pass http request response to use in others http requests in same thread group

I'm new using JMeter and I have this case:
Test Plan
Test Group1
Http Request 1
Json Extractor1
BeanShell Assertion1
Http Request 2
Http Request 3
And I want to use the response of HTTP req 1(extracted in JSON extractor) in both HTTP request 2 and 3. For request 2 is working fine I just use ${response} and works fine but when I try to user the same variable in request 3 is like is empty is not showing anything.
So I tried to put the BeanShell Assertion and do a var.set or even a set property, but is still not working. It is like the var or property is being set, and I can see them in HTTP req 2 but in HTTP req 3 they are empty.
Is there another way to set the variable or the response of the request 1 to be use in any other requests of the same thread?
Thanks
You need to amend your test design and make the JSON Extractor a child of the Http Request 1
Test Plan
Test Group1
HTTP Request 1
JSON Extractor
HTTP Request 2
HTTP Request 3
If you have JSON Extractor at the same level as HTTP Requests 1-3 it's being executed after every of them so it tries to extract the value from HTTP Request 2 response, doesn't find it and the variable gets empty or default value.
The same applies to the Beanshell Assertion.
More information: JMeter Scoping Rules
With regards to using Beanshell in general it's not very recommended, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language

How to run the request based on previous request response data

I have test plan with following request in sequence with 2 loops.
ApiHttp request
Mqtt request
I need to run 1st loop & verify API reaponse is true or false:
If it's true, run Mqtt request , else start the 2nd loop and same verify the API response, if it's true run Mqtt else stop.
In above scenarios, I put beanshell to read the API response:
vars.put("response", new String(data));
But don't know how to verify of its true or false & execute the Mqtt. Any help pls.
Add If Controller as the very first test element under the Thread Group and use the __groovy() function as the condition:
${__groovy(vars.getIteration() <= 2,)}
Put ApiHttp request as a child of the If Controller
Add another If Controller after the ApiHttp request and use the following __groovy() function as the condition:
${__groovy(ctx.getPreviousResult().getResponseDataAsString().contains('true'))}
Add Mqtt request as a child of the If Controller
Add Flow Control Action sampler after the Mqtt request and configure it like:
First switch from Beanshell to JSR223 Test elements and use Groovy.
Use If Controller and just put in it:
${response}
To stop the test based on iteration number, be aware that a Loop Controller exposes iteration as a variable based on name of variable, for example if your LoopController is named LC, then you can access iteration as:
vars["__jm__LC__idx"].toInteger()
And to stop a test from Groovy using JSR223 Assertion you can throw:
throw new org.apache.jorphan.util.JMeterStopTestException();
See:
https://jmeter.apache.org/api/org/apache/jorphan/util/JMeterStopTestException.html

How to check the response assertion is passed or failed in Jmeter using beanshell

I want to save the request and response of the sample into a text file, if response assertion is failed.
Here I can save the request and response it to a text file. But I am struck where,
how to check the whether response assertion is passed or failed for the respective sample.
How to validate the is current "Http Request" sample is passed or failed?
Thanks In advance.
Add Beanshell Listener as a child of your HTTP Request sampler which has Assertion(s)
Put the following code into "Script" area:
import org.apache.jmeter.assertions.AssertionResult;
for (AssertionResult assertionResult : sampleResult.getAssertionResults()) {
if (assertionResult.isFailure()) {
//do what you need here
}
}
As Listeners are executed after assertions the listener will have information regarding all assertion results therefore you will be able to conditionally store request/response details into a file.
Be aware that starting from JMeter version 3.0 the Groovy scripting engine is being shipped with JMeter so consider switching to JSR223 Listener and replacing all your Beanshell Test Elements with the JSR223 equivalents, performancewise it would be much better.

JMeter While Controller doesn't loop SOAP/XML-RPC Request

I have a JMeter script with
-Thread Group
- While Controller
-SOAP/XML-RPC Request
-Regular Expression Extractor
Extractor gets response code, While controller checks if it's = "200"
(${__javaScript("$errCode)== "500";)})
And for some reason, it doesn't loop.
But if replace SOAP request with HTTP request it's looping.
How can I loop with while SOAP request?
According to Apache's manual: "Otherwise - exit (or don't enter) the loop when the condition is equal to the string "false""
Apparently, SOAP/XML-RPC Request is failed and that's the reason why loop didn't start

How do I resend a sampler upon a failed assertion on JMeter?

I am designing a load test in JMeter.
With the current application that we have whenever a HTTP request is sent, the web server will very occasionally send back a page with a message. To get around this we just have to reload the page. This page could come up for literally any HTTP request.
Is there any way to design a test in JMeter where when a sampler fails, the sampler simply retries?
I'm not sure how I can get a Beanshell sampler to resend a HTTP request.
It is possible via additional Beanshell Assertion
You can re-run arbitrary sampler from the Beanshell Assertion as simple as
ctx.getCurrentSampler().sample(null);
Add a Beanshell Assertion after all other assertions. It matters as assertions are being executed upside-down.
Put the following code into Beanshell Assertion's "Script" area (just change "message" to what your server returns on error.
import org.apache.jmeter.samplers.SampleResult;
if (new String(ResponseData).equals("message")) {
SampleResult result = ctx.getCurrentSampler().sample(null);
if (result.getResponseDataAsString().equals("message")) {
Failure = true;
} else {
SampleResult.setSuccessful(true);
}
}
You'll have only one result recorded.
If assertion passes 1st time - it'll be successful
If assertion fails 1st time and passes 2nd time - it'll be successful
If assertion fails 2 times - it'll be recorded as failed.
For extended information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide.
Create such hierarchy:
Thread Group (1 user, 1 second ramp-up, forever)
-While Controller (empty condition = forever)
--Counter (start – 1, increment – 1, reference name – counter)
--HTTP request
---Timer (I prefer constant Timer, responseble for pause betwee retrying)
---BeanShell Post Processor
BeanShell Post Processor should contains(pseudo code):
if(Integer.parseInt(vars.get("counter")>5)
{
prev.setSuccessful(false);
prev.setStopTestNow(true);
}
if(successCondition)
{
prev.setStopTest(true);
}
There is no direct way to achieve it, but I think you can use While controller in conjuction with Regex extractor to resend the failed requests.
Logical flow could be,
1. HTTP request
2. Regex extractor post processor - check response contains failure extract value in msg variable, default is success
3. While controller - run till msg=failure, default value of msg is success
Example screenshot,
Let me know if this works.

Resources