How to implement If Else block in Jmeter test plan?

10,032

There is no "Else" block in JMeter, you have only "If" therefore if you need "Else" behavior you need to use 2 IF Controllers with opposite conditions.

But, for 2nd If Controller you won't be able to use this as this ${JMeterThread.last_sample_ok} variable will be overwritten with the result of your SMTP Sampler so if your SMTP Sampler will be successful ${JMeterThread.last_sample_ok} variable will become true therefore second If Controller will be executed as well.

So I would recommend the following approach:

  • If your Assertion Fails

    • In Beanshell PreProcessor define a custom variable like:

      vars.put("run_while_controller", "false");
      
  • Put your While Controller under another If Controller and use ${run_while_controller} as a condition

Alternative option is stopping the test after sending an email, this can be done using i.e. Test Action sampler

Going forward I would suggest using Debug Sampler and View Results Tree Listener to double check all the JMeter Variables values, this is most convenient way to see all JMeter variables and properties values. See How to Debug your Apache JMeter Script article for more information on getting to the bottom of your JMeter test unexpected behavior.

Share:
10,032
Namrata
Author by

Namrata

Updated on June 04, 2022

Comments

  • Namrata
    Namrata almost 2 years

    I have a task where I need to perform two tasks based on the previous Sampler result.

    If Assertion fails { send email with the error message. }else { perform the tasks in while controller. }

    I have achieved the first part easily by providing the condition in If controller ${JMeterThread.last_sample_ok} == false

    I have problems going to else block. I have tried two IF controllers but with no success. Case here is need to perform one task only either IF block or Else block. But In my case it goes to both the blocks and perform both the tasks. Can anyone help how to achieve this?enter image description here

  • Franta
    Franta over 6 years
    Two IF-controllers require the same condition written twice (despite once negated), which is potential source of defects, to have one IF on two places. Dubious.