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

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");
}

Related

Jmeter check using if controller, for a variable has a value or not

in one of my steps in the Jmeter script, I'm using json extractor to read a value from a key(address) in the JSON response and store it in a variable called "TypeOfRequest." In the next step, I need to check if the "TypeOfRequest" value is null or not(There can be situations I don't find that key in the JSON response). Then I need to take a different route.
Snippet how I'm getting the TypeOfRequest from Json extractor $.communicationMethods[:1].hTTPS.address
So my question is, how do I check if TypeOfRequest has a value or not in the if controller?
tried using '${__javaScript(vars.get("TypeOfRequest") == null)}(ref https://www.blazemeter.com/blog/jmeter-if-controller and https://sqa.stackexchange.com/questions/32969/how-do-i-check-if-a-variable-is-null-using-a-if-controller) but unable to go through the if condition, can someone help me with this. Thanks in advance
Just use Debug Sampler to see what JMeter Variables are defined and inspect their values, my expectation is that you're "unable to go through the if condition" because your TypeOfRequest variable is not null, i.e. it's present but it's an empty string.
Also the referenced article suggests using __groovy() or __jexl3() function so I believe if you change your condition to something like:
${__groovy(org.apache.commons.lang.StringUtils.isEmpty(vars.get('TypeOfRequest')),)}
you will be able to "go through the if condition"

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.

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

why does print vars.put ,the result is null?

String pass = "123456789";
vars.put("token01",pass);
System.out.println(pass);
System.out.println(token01);
the result of System.out.println(pass) is right ,it is 123456789,
but the result of token01 is null, I can not understand .
If you need to access a JMeter Variable value programatically you need to consider one of the following options:
If your variable is a String: String value = vars.get("token01")
If your variable is an Object: Object value = vars.getObject("token01")
Demo:
References:
vars.get()
vars.getObject()
You may also find Groovy Is the New Black article useful.
You have to use vars.get to access jmeter variables.
So you should use System.out.println(vars.get("token01")). Also you can use debug sampler and view results tree combination to make sure your script is working fine see How To Debug Your Apache Jmeter Scripts
for more information on jmeter troubleshooting techniques.

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:

Resources