You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JMeter If Controller未执行:变量存在且值符合预期仍不生效

JMeter测试计划流程控制问题

我的JMeter测试计划配置如下:

  • Get Shelves(HTTP请求)
  • 内置JSON Extractor提取变量shelf__matchNr
  • 紧随其后的Debug Sampler确认shelf__matchNr = 0
  • 后续配置If Controller,条件代码:
${__groovy(
    def val = vars.get("shelf__matchNr");
    log.info(":test_tube: shelf__matchNr is: " + val);
    return val != null && val.toInteger() == 0;
)}

问题现象

  • Debug Sampler明确显示shelf__matchNr = 0
  • If Controller内日志打印变量为null
  • If Controller下的流程未执行

已确认事项

  • JSON Extractor作用域正确配置在Get Shelves HTTP取样器内
  • If Controller位于HTTP请求和提取器之后
  • 已通过Debug Sampler确认变量值

解决方法

1. 修复Groovy代码的转义问题

你在Groovy代码里使用了HTML转义字符"和&&,这会导致Groovy引擎无法正确解析变量名和逻辑运算符。直接替换为普通双引号和&&即可:

${__groovy(
    def val = vars.get("shelf__matchNr");
    log.info(":test_tube: shelf__matchNr is: " + val);
    return val != null && val.toInteger() == 0;
)}

2. 改用更简洁的内置函数判断

如果不需要日志打印,直接用JMeter的__jexl3函数可以避免Groovy的语法问题,写法更直观:

${__jexl3("${shelf__matchNr}" == "0",)}

如果确认变量是数字类型,也可以直接做数值比较:

${__jexl3(${shelf__matchNr} == 0,)}

3. 检查If Controller的核心配置

确保If Controller的**"Interpret Condition as Variable Expression?"**选项处于勾选状态,否则JMeter会把整个条件当成字符串,不会执行表达式计算。

4. 再次验证JSON Extractor的提取范围

确认JSON Extractor的"Apply to"选项设置为**"Main Sample only"**(如果Get Shelves请求没有子取样器),或者"Main Sample and Sub-samples"(如果有子请求),确保变量被正确写入当前线程的变量空间。

内容的提问来源于stack exchange,提问作者Sagarika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:42:13