Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resend a sampler upon a failed assertion on JMeter?

I am designing a load test in JMeter. With the current application that we have whenever a HTTP request is sent, the web server will very occasionally send back a page with a message. To get around this we just have to reload the page. This page could come up for literally any HTTP request.

Is there any way to design a test in JMeter where when a sampler fails, the sampler simply retries?
I'm not sure how I can get a Beanshell sampler to resend a HTTP request.

like image 743
Michael Galos Avatar asked Dec 02 '25 14:12

Michael Galos


1 Answers

It is possible via additional Beanshell Assertion

You can re-run arbitrary sampler from the Beanshell Assertion as simple as

ctx.getCurrentSampler().sample(null);

Add a Beanshell Assertion after all other assertions. It matters as assertions are being executed upside-down.

Put the following code into Beanshell Assertion's "Script" area (just change "message" to what your server returns on error.

import org.apache.jmeter.samplers.SampleResult;

if (new String(ResponseData).equals("message")) {
    SampleResult result = ctx.getCurrentSampler().sample(null);
    if (result.getResponseDataAsString().equals("message")) {
        Failure = true;
    } else {
        SampleResult.setSuccessful(true);
    }

}

You'll have only one result recorded.

  • If assertion passes 1st time - it'll be successful
  • If assertion fails 1st time and passes 2nd time - it'll be successful
  • If assertion fails 2 times - it'll be recorded as failed.

For extended information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide.

like image 90
Dmitri T Avatar answered Dec 04 '25 10:12

Dmitri T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!