0
votes

I'm new to Jmeter. I have a task where I want to send an email of Assertion result failure. I have an http request where I'm checking the result of an API, and if it contains appropriate response that test is successful. Now the problem is if test is failed, I need to send its result (with failure message) via email. I have configured SMTP Sampler properly, it is sending mails. But I don't know how to send response assertion failure message as message body with that email. Can Anyone please help?

1

1 Answers

3
votes

You can use Beanshell PreProcessor in order to get assertion failure message(s) from the previous sampler.

  1. Add Beanshell PreProcessor as a child of SMTP Request sampler
  2. Put the following code into the PreProcessor's "Script" area:

    import org.apache.jmeter.assertions.AssertionResult;
    
    AssertionResult[] results = prev.getAssertionResults();
    StringBuilder body = new StringBuilder();
    for (AssertionResult result : results) {
        body.append(result.getFailureMessage());
        body.append(System.getProperty("line.separator"));
    }
    vars.put("body", body.toString());
    
  3. Use ${body} JMeter Variable in the SMTP Request sampler.

Remember that it will work only for assertion failure messages of the previous sampler

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting in JMeter tests.