Search code examples
jmeterbeanshell

How to change child sample result to successful in jmeter?


I've made login process with the help of jmeter. In of of request samplers I'm expecting to get response code "401". I've added BeanShell Assertion

if (ResponseCode.equals("401") == true) { 
    SampleResult.setResponseOK();  
    SampleResult.setSuccessful(true);

}

And my Results Tree is looking like this now.

My question is - what i need to add to BeanShell in order to make child of the second sample green (passed) as well as it's parent sample?


Solution

  • The easiest way is using Response Assertion configured like:

    Response Assertion


    If you are still looking for Beanshell solution - you need to process all sub-results along with the main result so you should amend your code like:

    import org.apache.jmeter.samplers.SampleResult;
    
    //process main sample
    if (SampleResult.getResponseCode().equals("401")) {
        SampleResult.setResponseCodeOK();
        SampleResult.setSuccessful(true);
    }
    
    //process all subsamples
    for (SampleResult subResult : SampleResult.getSubResults()){
        if (subResult.getResponseCode().equals("401")){
            subResult.setResponseCodeOK();
            subResult.setSuccessful(true);
        }
    }
    

    See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using Beanshell in JMeter test scripts.