P29JMeter IF Controller (If Controller)

Article directory

    • 1. IF Controller (If Controller) parameter description
    • 2. Test plan design
      • 2.1, groovy writing
      • 2.2. JavaScript writing method
      • 2.3, jexl3 writing method

1. IF Controller (If Controller) parameter description

You can control whether the child/descendant element below it is executed; if it is true, it will be executed; if it is false, it will not be executed

Select the right button of the thread group >>> Add >>> Logic Controller >>> IF Controller (If Controller)

(1), Expression (must evaluate to true or false): expression (value must be true or false), that is, the condition value entered in the right text box must be true or false, (by default)

(2), Use status of last sample: Shortcut, if the result of the last sampler is passed, a variable will be referenced. After clicking, it will be generated in the Expression box: ${JMeterThread.last_sample_ok}

(3), Interpret Condition as Variable Expression?: By default, the option is checked, and the condition is interpreted as a variable expression (need to use __jexl3 or __groovy expression) [When this item is selected, it means: determine whether the variable value is equal to the string true (not case sensitive)】

  • Uncheck: Just enter the expression we need to judge directly. When the judgment expression is true, execute the request under the if controller, such as “1!=2”, then the following http request will be executed

  • Check: At this time, the conditional expression cannot be directly filled in the expression, and the conditional expression needs to be calculated as true/false with the help of functions. The functions that can be used are _jexl3 and _groovy

(4), Evaluate for all children?: The condition acts on each child item, and the condition will be judged once when each child item is executed. Generally not checked, the condition is generally only judged once

Note:

  • The yellow exclamation mark on the text box is to remind you that it is recommended to use __jexl3 or __groovy expressions to improve performance, which is the default method

  • The IF controller can only act on the children under it

  • The full name of jexl: Jakarta Commons Jexl, is an expression language (Java Expression Language) interpreter

The conditional expression cannot be directly filled in the Expression of the if logic controller, and the conditional expression needs to be calculated as true/false with the help of functions. The functions that can be used are __jexl3 and __groovy functions.

A: Variables, such as ${flag}, if the value is true, it is considered to pass, otherwise it is considered to be false

B: Function, conditional judgment supports three scripting languages: js, groovy, and jexl3 at the same time. For performance reasons, it is not recommended to use js

  • js syntax example: KaTeX parse error: Expected ‘}’, got ‘EOF’ at end of input: …_\_javaScript(“{count}” == “1””)}

  • Groovy syntax example: ${__groovy(vars.get(“count”) != “1”)}

  • Example of jexl3 syntax: KaTeX parse error: Expected ‘}’, got ‘EOF’ at end of input: {\_\_jexl3({count}< 10)}

2. Test plan design

(1) Right click on the test plan <<< Add <<

(2), right click on the thread group <<< add <<< configuration element <<< user-defined variable

  • count: 1

  • flag: true

(3), Right click on the thread group <<< Add <<< Logic Controller <<< IF Controller

  • Check Interpret Condition as Variable Expression?
${<!-- -->flag}

(4), IF controller right click <<< Add <<< Sampler <<< Debug Sampler

(5), Right click on the thread group <<< Add <<< Listener <<< View the result tree

(6) Click to start, click to view the result tree, and check whether the debug sampler is executed

As shown in the figure, the IF controller returns true, so the debug sampler will be executed

(7), modify the user-defined variable component, and modify the flag parameter to false

(8) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

As shown in the figure, the IF controller returns false, so the debug sampler will not be executed

(9), modify the user-defined variable component, and modify the flag parameter to aaaaa

(10), click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

As shown in the figure, the IF controller returns false, so the debug sampler will not be executed

As long as the flag value in the user-defined variable component is not true, it is false

2.1, groovy writing

(1), modify the user-defined variable component, and modify the flag parameter to true

(2), modify the IF controller, using groovy syntax

// Choose one of the two writing methods

${<!-- -->__groovy(vars.get("count")=="1")}
${<!-- -->__groovy("${count}"=="1")}

(3) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

${__groovy(vars.get("count")=="1")} in the IF controller returns true, so the debug sampler will execute

(4), modify the user-defined variable component, and modify the count parameter to 2

(5) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

${__groovy(vars.get("count")=="1")} in the IF controller returns false and the count value is 2, so the debug sampler will not execute

2.2, javaScript writing

(1), modify the user-defined variable component, and modify the count parameter to 1

  • count: 1

(2) Modify the IF controller to use javaScript syntax

${<!-- -->__javaScript("${count}"=="1")}

(3) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

${__javaScript("${count}"=="1")} in the IF controller returns true, so the debug sampler will execute

(4), modify the user-defined variable component, and modify the count parameter to 2

(5) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

The return value of ${__javaScript("${count}"=="1")} in the IF controller is false, and the count value is 2, so the debug sampler will not execute

2.3, jexl3 writing

(1), modify the user-defined variable component, and modify the count parameter to 1

  • count: 1

(2), modify the IF controller, use jexl3 syntax

// Four ways of writing, just choose one

${<!-- -->__jexl3(${<!-- -->count}=="1")}
//${__jexl3(${count}==1)}
//${__jexl3(${count}<10)}
//${__jexl3("${count}"=="1")}

(3) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

${__jexl3(${count}=="1")} in the IF controller returns true, so the debug sampler will execute

(4), modify the user-defined variable component, and modify the count parameter to 2

(5) Click to clear all, click to start, click to view the result tree, and check whether the debug sampler is executed

The return value of ${__jexl3(${count}=="1")} in the IF controller is false, and the count value is 2, so the debug sampler will not be executed