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

Related

Conditionally perform assertion

I would like to perform an assertion on a sampler only if certain conditions are met (i.e. variables and parameters have a specific value). The assertion should be ignored if the conditions are not met, not fail.
What are my options?
An if controller does not seem to work as it apparently requires the sampler (which always should be invoked) to be in its scope too.
I can only think of using JSR223 Assertion which allows you executing arbitrary Groovy code providing maximum flexibility.
Here is an example simple code:
if (vars.get('foo') == 'bar') { // execute only if JMeter Variable ${foo} is equal to "bar"
if (!prev.getResponseDataAsString().contains('baz')) { // if there is no "buz" word in the response
assertionResult.setFailure(true) //fail the sampler
assertionResult.setFailureMessage('Failed to find word "baz" in the response')
}
}
Check out Scripting JMeter Assertions in Groovy - A Tutorial article for more information.

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

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.

Jmeter assert json response element to be NOT NULL

I am getting Json response, I have parsed it using jp#gc - JSON Path Extractor and got an element say 'Access_Token'. This Access_Token is dynamic. So I just want to make sure that this element is not null.
Any leads would be much appreciated.
In the JSON Path Extractor provide Default Value, for example NOT_FOUND
Add Response Assertion after the JSON Path Extractor and configure it as follows:
Apply To: JMeter Variable -> Access_Token
Pattern Matching Rules:
Tick NOT
Tick Equals
Patterns to Test: NOT_FOUND (or whatever you entered into the "Default Value" input of the JSON Path Extractor)
See How to Use JMeter Assertions in Three Easy Steps article for comprehensive information on using Assertions in JMeter scripts.
Add a BeanShell PostProcessor component after you get your Access_Token and in it check what you want...
if (vars.get("Access_Token") != null) {
// do something
} else {
// do something else
}
Depending on your needs, you can do basically what ever you want from here. For example stop the thread, stop the test...
Since JMeter 3.0 there is a new JSON Path Processor that you should use instead of the JMeter Plugins one.
See its features in action here:
http://www.ubik-ingenierie.com/blog/easy-scripting-of-json-applications-with-apache-jmeter/
You can then apply Dmitri T. answer.
In jmeter 5 you could try doing something like this:

JMeter enable/disable HTTP Request Sampler upon certain condition

I have a few HTTP Request Samplers that I would only like to execute when a certain condition has been met. What I have done is added a BeanShell PreProcessor to the HTTP Request Sampler with the following code
if (${getTeamName}.equals("Test Team") == true)
{
HTTPSampler.setEnabled(false);
}
If the value of getTeamName is Test Team then I want to disable that HTTP Request Sampler, as it doesn't have to be executed then. However it looks like that currently doesn't work.
Is there any one who knows what I'm doing wrong here, or a suggestion to what I should do?
As per JMeter Performance and Tuning Tips guide:
But of course ensure your script is necessary and efficiently written, DON'T OVERSCRIPT
Why not just to use If Controller like:
If Controller, condition: "${getTeamName}" != "Test Team"
HTTP Request Sampler
If ${getTeamName} will be Test Team child sampler(s) won't be executed.
While using beanshell, access variables using vars.get("VARNAME")
if (vars.get("getTeamName").equals("Test Team") == true)
{
sampler.setEnabled(false);
}

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.

Resources