Jmeter - Common Response assertion for multiple request - jmeter

I want a common Response assertion for 200 response code which should be applicable for each sampler request.
However it is also required for some sampler requests to overwrite that from sampler level as (response code 204,500).
Is there any possible way to achieve this?

JMeter Assertions obey Scoping Rules so if you put an Assertion on the same level as Samplers - it will be applied to all samplers
If you need to override expected response code for a specific sampler I would recommend changing the response code to 200 via JSR223 PostProcessor like:
if (prev.getResponseCode().equals('500')) {
prev.setResponseCodeOK()
}
this way the above Groovy code will change the individual sampler status code from 500 to 200 so "global" assertion will still be successful.

In Response Assertion you can add more Pattern To Test, so add 3: 200,204,500
And check the Or checkbox that you will allow to assert either one.
Note: Field to check is Response Code
Note: Pattern Matching rule can be Equals or Contains

Related

Extract JSON response status code from Sampler Result by using JSON extractor

I'm writing a script in Beanshell and validating it by checking first the response status that JSON response has sent i.e "200" but unable to find out the way to extract it.
Since JMeter 3.1 it is recommended to use Groovy for scripting as Groovy performance is much better than other scripting options so consider migrating to JSR223 Test Elements on next available opportunity.
You won't be able to use JSON Extractor there as this 200 status code is related to HTTP protocol. Normally JMeter automatically considers HTTP status codes below 400 as successful, however if you need to perform an explicit check I would recommend going for Response Assertion. The relevant configuration would be
If you still want to continue with checking response status code via scripting you can do it using JSR223 Assertion and the following code:
if (!SampleResult.getResponseCode().equals("200")) {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("Response code was not 200, received: " + SampleResult.getResponseCode())
}
You need to add a Response Assertion for checking HTTP status
Field to Test Instructs JMeter which field of the Request or Response to test.
Response Code - e.g. 200

How to add assertions to the request inside while controller of Jmeter

I'm using a jmeter script with while controller condition as
${__javaScript("${status}"=="Progress")}
Followed by one http request and from that request again I will extract the status.
If the status is progress it will contine one http request inside loop else if the status success it will exit the loop. But i want to add assertion to ensure whether I get the status as success in the http request or not.
Because if i use assertion as Success in that http sampler, it checks all the reqest and obviously it will return false, and i need to add assertion only to the final http sampler of the while controller.
And so I can get error percentage with failing request in aggregate report and view results tree . Can anyone share your ideas please.
I would recommend using Response Assertion to verify if ${status} variable value is Progress OR Success
Add Response Assertion as a child of the HTTP Request sampler
Configure it as follows:
Apply to: -> JMeter Variable -> status
Pattern Matching Rules: Matches
Patterns to Test: Progress|Success
Given you use "Matches" rule JMeter will treat the pattern as a Perl5-style regular expression so if ${status} variable will be equal to Success or Progress the sampler will pass and otherwise it will be marked as failed.
See How to Use JMeter Assertions in Three Easy Steps article to learn more about using Assertions in Jmeter tests.

How to add 'Or' condition in assertion

I want the request to pass in both the cases if response contains "Completed" or "Progress, Will take time to process".
But if I include both this assertions in response assertion, it will take it as 'and'. It will pass only if both are satisfied.
Here any one of this is sufficient. Please suggest.
You will need to go for an assertion which supports scripting, i.e. Beanshell Assertion
Add Beanshell Assertion as a child of the request which returns either "Completed" or "Progress" messages
Put the following code into "Script" area:
String response = new String(ResponseData);
Failure = !(response.contains("Completed") || response.contains("Progress, Will take time to process"));
Where:
ResponseData - byte array which holds parent sampler response
Failure - boolean which indicates whether parent sampler should be failed or not.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on how to use JMeter and Java API from Beanshell test elements and extend your JMeter tests with scripting.
Jmeter 3.2 allow to check using Or condition
Response Assertion now allows to work on Request Header, provides a "OR" combination

Manipulating the request body of HTTP thread based on the data extracted from the previous HTTP response

I want to manipulate the request body of HTTP thread based on the data extracted (using 'Regular Expression Extractor') from the previous HTTP response.
Here is the scenario:-
I have extracted the statusFlag and statusId from 'HTTP request 1' as:
Ref name: status
Reg. Exp: "statusFlag":"(\w+)","statusId":"(\w+)"
So, first I want to check that the value of statusFlag is 'New' or not.
If it is New then I have to proceed and feed statusId in next HTTP request or else display statusFlag mismatch.
Need help. Got stuck badly.
I believe Response Assertion is what you're looking for. Add it after the Regular Expression Extractor and configure it as follows:
Apply to: JMeter Variable -> statusFlag (or your reference name)
Pattern Matching Rules: Equals
Add New as a "Pattern to Test"
The assertion will check whether "statusFlag" is "New" and if not - it will fail the sampler and report the expected and the actual value.
Optionally you can add If Controller after the Response Assertion, use ${JMeterThread.last_sample_ok} as a condition and place 2nd request as a child of the If Controller - it will be executed only if "statusFlag" is new.
See How to Use JMeter Assertions in Three Easy Steps guide for more information on conditionally setting pass or fail criteria to requests in your JMeter test.
That's how your Jmeter project should look like.
Regular Expression Extractor stores extracted value in ct variable that can be accessed in If Controller as "${ct}" == "yourvalue" and, if true, can be also sent as a part of Request 2 body using the same ${ct} reference.
Jmeter project structure

Want to check for assertion failure in Jmeter

I am a beginner in JMeter. I want to check for Assertion Failures in my script. I want to continue my transaction for a number of iterations after a single log in attempt, and I want to log out only if an error occurs. For that, I want to check if an error occurred in the script.
Is it possible by comparing assertions in JMeter?
If not, is there are any other way to find that?
Define first indicator(s) that will mark response as erratic (Response Code, keyword, etc.).
Try to use Response Assertion to handle state of your request (success/failure) depending on indicators above and then use IfController along with pre-defined JMeterThread.last_sample_ok jmeter's variable - whether or not the last sample was OK - true/false.
Schema will look like below e.g.:
ThreadGroup
LOGIN REQUEST
...
YOUR HTTP REQUEST HERE
Response Assertion
Response Field to Test: Response Code
Pattern Matching Rules: NOT Equals
Patterns to Test: 200
Regex Extractor
IfController
Condition: ${JMeterThread.last_sample_ok} // will be TRUE if Response Assertion above is TRUE (i.e. response code != 200)
LOGOUT REQUEST
...
In addition to the answer above I would recommend you:
Add error checking to your script - assertions that the responses are
valid for a given reques
Use Firebug to view network traffic when you need to debug your test
script
Use a regular expression extractor to retrieve a dynamic value from a
response and re-use it in a later request
To get the idea you can follow JMeter error checking video tutorial.
FYI: Assertion details provided.
Hope this helps.

Resources