Conditionally perform assertion - jmeter

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.

Related

Code in JMeter JSR223 Sampler comments is executed

Please tell me why the code in comments (both /*something*/ and //something) is executed using JSR223 Sampler & BeanShell sampler?
For example, I have:
and in the next JSR223 Sampler I have:
and the result is:
and the question is: why this code: "/${__setProperty(checkProperty, 50)};/" is executed regardless of that it is in comment and it is in wrong condition?
JMeter Functions are being executed in the place where they're found, no matter where it is, in Sampler label, comments section, sampler body, etc.
Actually inlining JMeter Functions and/or Variables into JSR223 scripts is not the best idea as
it might conflict with Groovy's string interpolation syntax
the function or variable might resolve into something causing script compilation failure or logic error
and last but not the least Groovy will cache the first occurrence and use it for subsequent iterations
So if you need to set a property - use props.put() function like
props.put('foo', 'bar')
And finally I'm not sure that using props.clear() is a good idea because there are some pre-defined JMeter properties (you can check yourself using Debug Sampler and View Results Tree listener combination) and it might result into unexpected behaviour if a test element will be relying on that property existence and/or value

how do I check if a specific string exists in the response, and set a user defined variable to true or false

I want to determine if a specific string exists in a HTTP response. If it does, I want to set a user defined variable to TRUE, and if it does not, I want to set it to FALSE. I do not want to pass/fail the test based on this. I just want to know if the response has the string or not. Then, once I have that answer stored in my user defined variable, I will use that variable in the jmeter IF controller to perform other actions based on the answer.
I have tried using the beanshell assertion with the following code, but my pre-defined user variable, called stringExists, is not getting updated to correctly reflect if the response has the string or not:
vars.get("stringExists");
if (new String(ResponseData).contains("this is the string I expect")) {
vars.put("stringExists","TRUE");
}
else {
vars.put("stringExists","FALSE");
}
What am I doing wrong?
There is nothing wrong with your code, you can use Debug Sampler in order to "see" the generated variable:
Also be informed that starting with JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so you should consider switching to the JSR223 Assertion. As there is no ResponseData shorthand there you will need to amend a code a little bit:
if (prev.getResponseDataAsString().contains("this is the string I expect")) {
vars.put("stringExists","TRUE");
}
else {
vars.put("stringExists","FALSE");
}

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

Beanshell assertion script failing in Linux machine and working in windows

IN JMeter I want some of the 500 internal server errors to NOT be marked as failures if the response contains a specified text. All 500 server errors are marked as failures.written code to make it as pass for all fail cases.same code is working without error in windows, but getting all samplers are fails in linux machine.
if (ResponseCode.equals("412") == true)
{
SampleResult.setResponseOK();
}
else if (ResponseCode.equals("500") == true)
{
SampleResult.setResponseOK();
}
You need to add SampleResult.setSuccessful(true);, also remove == true which is redundant and remove duplicate code:
if (ResponseCode.equals("412") || ResponseCode.equals("500")) {
SampleResult.setResponseOK();
SampleResult.setSuccessful(true);
}
Also consider change ResponseCode to responseCode according to Java/Oracle parameter name conventions.
You can achieve this using Response Assertion, just check Ignore Status box. For example, below configuration mark affected sampler(s) as successful if response will contain foo
Check out Response Assertions in JMeter 3.2 - New and Improved article for more information on using Response Assertion to add pass/fail criteria logic to your JMeter tests.
If you need more complex assertion logic which cannot be achieved using single or multiple Response Assertion instance(s) consider switching to JSR223 Assertion as it is recommended to use Groovy for scripting since JMeter 3.1
With regards to your question itself, double check JMeter's "lib" folder of Linux machine contains bsh-2.0b5.jar file for Beanshell scripting and groovy-all-2.4.10.jar for Groovy scripting.

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

Resources