Is there a way in JMeter to get the response message from the network tab? - jmeter

I am trying to get the error message from the network tab in JMeter:
I have inserted the response assertion looking for a 200 or 201. However, when a test fails, it only tells me it is looking for one of the values, but not a message like in the response section of the network tab. For example, if it is a bad request, I see the 400, but no message like "uid cannot be empty, name cannot be empty". Is there a way to pull this information in JMeter?

With "normal" Response Assertion you cannot, if you want to apply some custom logic you need to perform some custom scripting.
For example if you want to add response body to the Assertion message you can play the following trick:
Add JSR223 Assertion after the Response Assertion
Put the following code into "Script" area:
prev.getAssertionResults().each { assertionResult ->
if (assertionResult.isFailure()) {
assertionResult.setFailureMessage(assertionResult.getFailureMessage() +
System.getProperty('line.separator') +
'Response data:' +
System.getProperty('line.separator') +
prev.getResponseDataAsString())
}
}
That's it, the above code will add the response data to any failing assertion, hopefully this is what you're looking for.
Going forward you might want to migrate all the assertion logic into the JSR223 Assertion and Groovy, see Scripting JMeter Assertions in Groovy - A Tutorial article for more details.

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 can I run multiple if controller in jmeter

I am currently working in a heavy load test, I have one login request which access with user and password and basic auth, I have to validate some info from the response and I am using assertions but I need to apply different kind of assert depending on the code response and to be able to do that I am using an if control putting the assertions inside as a child, the problem begins when I try to execute the assertions with an error code response, some how the if controller is not taking the value of the variable I created to store the code response. could some one help me? thanks!
You cannot put assertion as a direct child of the If Controller. In fact you can, however it will not make any sense as assertions obey JMeter Scoping Rules and since there is no any Sampler in the Assertion scope - it will simply not get executed.
I would recommend going for JSR223 Assertion where you have all power of Groovy SDK and JMeter API in order to set up your custom pass/fail criteria. The pseudo code would be something like:
if (SampleResult.getResponseCode().equals('200')) {
//do what you need when response code is 200
//for example let's check if response contains "foo" line
if (!SampleResult.getResponseDataAsString().contains('foo')) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Failed to find "foo" line in the response')
}
}
else if (SampleResult.getResponseCode().equals('300')) {
//do what you need when response code is 300
}
else if (SampleResult.getResponseCode().equals('400')){
//do what you need when response code is 400
}
else {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Unexpected response code: ' + SampleResult.getResponseCode())
}
References:
SampleResult documentation
AssertionResult documentation
Scripting JMeter Assertions in Groovy - A Tutorial

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

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