Beanshell assertion script failing in Linux machine and working in windows - jmeter

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.

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.

I want to fail Beanshell sampler when my script is not conditions not satisfied

I am using Beanshell sampler with java code to compare the two files line by line. I want to fail the Beanshell sampler if comparison failed at any line. I see always my beanshell sampler is sucess in view results treee even the comparison failed or passed. So any one please give me a idea how to fail the sampler.
Note: I used Beanshell Assertion as well but its not worked.
View Results Tree Image
Beanshell Sampler with Beanshell Assertion
boolean differenceInFile = false;
SampleResult.setSuccessful(differenceInFile);
IsSuccess=differenceInFile;
line #2 and line #3 is what you need in your bean shell sampler to pass / fail depending on your file comparasion
Sample JMX File
It will show you as an xml, try to download it
There is SampleResult pre-defined variable which you can use for setting the sampler passed or failed, define response code, response message, response body, etc.
So something like:
if (there_are_differences) {
SampleResult.setSuccessful(false)
SampleResult.setResponseCode("500")
SampleResult.setResponseMessage("Files are different")
}
In the assertion you have AssertionResult for the same.
Also be aware that you should be using JSR223 Test Elements and Groovy language for scripting since JMeter 3.1 so it might be a good option for migration.
More information on JMeter API shorthands available for the JSR223 Test Elements - Top 8 JMeter Java Classes You Should Be Using with Groovy

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

Using BeanShell Assertion for saving failed response

I'm trying to save the response of failed requests into log but unsuccessfully.
I have HTTP requests which each one has its own Response assertion, and once in a while through my load test a response doesn't meet the requirements of the Response Assertion, so I want to fail it.
I tried to add this code to the BeanShell Assertion following this post:
if (Boolean.valueOf(vars.get("DEBUG"))) {
for (a: SampleResult.getAssertionResults()) {
if (a.isError() || a.isFailure()) {
log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion failed for response: " + new String((byte[]) ResponseData));
}
}
}
The Test plan itself looks like this:
In user defined variables I've added the following:
Name: DEBUG
Value: (none)
I ran this program through the non GUI mode - cmd and on purpose made a response fail (response assertion failed it) but I did not get any error response logged to my log file.
What am I doing wrong?
thanks
First of all if your goal is to just save failed responses, together with assertion results, you don't need any custom code. You can add Simple Data Writer configured like this:
Option 2: If you want to write responses into the log file, then you need custom script. But in that case I'd suggest using BeanShell Listener (unlike assertions and other objects, there's only 1 instance of listener for all threads, so it's more economical, and less chance to create a bottleneck at log writing).
Also according to your script, the value of DEBUG must be true, but as you mentioned, you did not set it to true (empty string will be resolved to false by Java's valueOf). So change it to
Name: DEBUG
Value: true
Here's how such BeanShell Listener's script looks:
if (Boolean.valueOf(vars.get("DEBUG"))) {
for (a: sampleResult.getAssertionResults()) {
if (a.isError() || a.isFailure()) {
log.error(sampleResult.getThreadName() + ": "
+ sampleResult.getSampleLabel() + ": Assertion failed for response: "
+ sampleResult.getResponseDataAsString());
}
}
}
Option 3 is to stay with your original solution, then you need to fix the variable assignment (as I mentioned before).
I would rather let JMeter do this automatically, it is the matter of a couple of properties, to wit:
First of all, you need to tell JMeter to store its results in XML format:
jmeter.save.saveservice.output_format=xml
Second, you can amend the following property values to configure JMeter to store assertion failure message and data into .jtl results file
jmeter.save.saveservice.assertion_results_failure_message=true
jmeter.save.saveservice.assertion_results=all
Probably the most useful option: simply save response data for failed samplers:
jmeter.save.saveservice.response_data.on_error=true
The above properties can be either put into user.properties file (located in JMeter's "bin" folder, JMeter restart is required to pick the changes up) or you can pass them via -J command line argument like:
jmeter -Jjmeter.save.saveservice.output_format=xml -Jjmeter.save.saveservice.response_data.on_error=true ....
See Apache JMeter Properties Customization Guide for more information on working with JMeter Properties to tune various aspects of JMeter configuration

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