1
votes

I created a script in jmeter, few positive cases and few are negative cases. For Positive Cases - Response Code will come as 200 For Negative Cases - Response Code will come as 412.

As per Jmeter if Response Code 4xx or 5xx will be considered as Fail but in my case i am expecting result as 412 in negative cases and i want to consider that as Pass.

I tried with BeanShell Assertion but i didn't get the expected.

Code is as below:

String ErrorValue = "${ExpectedError}";

if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
    Failure = false;
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
     Failure = false;
}
else
{
    Failure=true;
}

with about code i am able to check the expected error and response is same but if that is same how to change the status to pass i didn't get.

Please anyone help me.

Thanks Sarada

2

2 Answers

3
votes

Your Failure = false bit sets only Beanshell Assertion success. As far as I understand you need to change status of the parent sampler. In order to do so you need to invoke SampleResult.setSuccessful() method and set it to "true" as follows:

SampleResult.setSuccessful(true);

Full code:

String ErrorValue = "${ExpectedError}";

if((ErrorValue.equals("ERROR")) && (ResponseCode.equals("412")))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else if(ErrorValue.equals("NO ERROR") && ResponseCode.equals("200"))
{
Failure = false;
SampleResult.setSuccessful(true);
}
else
{
Failure=true;
}

References:

2
votes

If you are expecting a HTTP Response Code "failure" in JMeter but wish to flag the sample as successful this can be accomplished by a response assertion:

For example: When validating a DELETE call works, we might want to re-try a GET and validate 404 as expected. Normally JMeter would consider this a failure, but in the context of our test it is not.

  • Add A Response Assertion to the after-delete GET call.
    • Apply To: Main Sample
    • Response Field to Test: Response Code
    • Check off "Ignore Status"
    • Pattern Matching Rules: Equals
    • Pattern to Test: 404

The status of failed or not is always ignored. However, only if the assertion of 404 matches will the request be a success.

For example, if the call returned a 500 jmeter would still ignore the "failed" status, but mark the sample as a failure because 500 != 404.

-Addled