Search code examples
if-statementjmetercomparisonbeanshell

How to write Bean Shell Script to compare two variables and print the output. JMETER


I have two variable 1)Expected(taken from excel file) & 2)Actual(regex from result) Now i tried to compare both the variables using bean Shell Post processor Scripting.

if(("${Expected}").equals("${Actual}"))
{
IsSuccess = true;
}
else
{
IsSuccess = false;
}

which should actually mark sample as pass and fail according to the comparison. But i can't find the sampler marked fail when the comparison returns false. Is their any error in my condition. Also Please help to print output to console Help is appreciated. Thank You


Solution

    1. Don't inline JMeter Variables or Functions into a Beanshell (or any other) script, they might resolve into something which will cause compilation failure
    2. If you want to mark sampler as passed or failed from the Post-Processor you should be doing it a little bit differently

    Example suggested code:

    if (vars.get("Expected").equals(vars.get("Actual"))) {
        prev.setSuccessful(true);
    }
    else {
        prev.setSuccessful(false);
    }
    

    Other recommendations:

    1. Going forward consider switching to JSR223 PostProcessor and Groovy language as Groovy performance is much higher for any scripting implementations
    2. You can achieve the same without having to write a single line of code via "normal" Response Assertion, example configuration would be something like:

      JMeter Assertion Compare 2 Variables

      See How to Use JMeter Assertions in Three Easy Steps article for more information on conditionally passing/failing your JMeter samplers using assertions.