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

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

Related

Jmeter - Common Response assertion for multiple request

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

Conditionally Save responses to a file based on HTTP code or body string

I'm a newbie in using Jmeter and searching for a solution solving the following problem: I have a suite that contains a set of atomic HTTP requests and the goal is to save response bodies into an xml file. Currently, I'm handling that with using "Save Responses to a file listener", but need to be able to save separately error and OK responses in different XML, based on response HTTP code and body string.
to illustrate the case with dummy code
if (HTTP response code == 200 OK && body does not contain "ERROR") then
save response to file (%path%\responsename_OK.xml
else
save response to file (%path%\responsename_ERROR.xml
My bad, Nikolay, I forgot you wanted it in xml format...
Well, the easiest way is to add 2 Simple Data Writers (SDW) to each HTTP Request where 1 SDW logs only errors and the other logs only successes.
The Pro's are
it's easy and once you set it up for 1 HTTP Request, you can simply copy it to every other HTTP Request
Each success or failure with be appended to the corresponding .xml file
You can define as much or as little detail as you want
The Con is you add 2 elements to each Request, thus increasing your test size twice as much as you would with a BeanShell Assertion. (if that's a concern for you.)
If you want a more detailed way, you could try a BeanShell Assertion scripted with XML Parsers and XPath Assertions like outlined here.
Use Response Assertion (in fact you will need 2: one for Response Code and another one for Response Body checking) in order to fail the request basing on these criteria
Use If Controller for setting a condition:
${JMeterThread.last_sample_ok} will be triggered if previous sampler is successful
!${JMeterThread.last_sample_ok} - will fire if previous sampler fails
N.B. you should have any Sampler under the If Controller so it could work.

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

Complex response assertion in jmeter

I'm struggling a bit to build a proper test for this scenario: basically, I'm making a POST call to a web-service and trying to assert the following:
A 201 response is ok
A 409 response is also OK (but I extract this code to a variable and attempt a retry later)
A 400 response might be OK, but only if there is a certain string in the response body
Items 1 and 2 above I got to work fine: I handle 2 with a Response Code Extractor and an If Controller later on.
My problem is: how do I test for a given substring on the response body, but only in the event of a 400 response code?
The final assertion I want to build goes something like this:
"(if the response code is 201 or 409) or (if the response code is 400 and 'substring' found in the body) then OK"
I believe that you need to use Beanshell Assertion as it is the most flexible of all assertions provided.
Relevant Beanshell code will look as follows:
if (ResponseCode.equals("201") || ResponseCode.equals("409") || (ResponseCode.equals("400") && SampleResult.getResponseDataAsString().contains("something"))) {
Failure = false;
}
See How to Use JMeter Assertions in 3 Easy Steps guide for more details on getting confidence by using JMeter Assertions.

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