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

JMeter中检查JSR223变量值的If条件失效问题排查

JMeter API性能测试路径拼接问题

测试流程

HTTP Authorization Manager
HTTP Sampler (Base API)
JSON Extractor
ForEach Controller
JSR223 Postprocessor (通过if条件修改API路径)
If Controller (检查值不为"true")
HTTP Sampler

相关代码

JSR223 Postprocessor代码

def currentURL = vars.get("currentURL")
def modifiedURL = ""
    
log.info("currentURL: " + currentURL)
log.info("modifiedURL1: " + modifiedURL)

if (currentURL.equals("example")){
   modifiedURL = 'example?parameter1&parameter2'
   log.info("modifiedURL2: " + modifiedURL)
} else if (currentURL.equals("example2")){
   modifiedURL = 'example2?parameter3&parameter4'
   log.info("modifiedURL3: " + modifiedURL)
}

log.info("modifiedURL4: " + modifiedURL)

vars.put("modifiedURL", modifiedURL)

def ifvar = ${__groovy(vars.get("modifiedURL") != "",)}

def condition = ${__groovy(vars.get("modifiedURL") != null && !vars.get("modifiedURL").isEmpty(),)}

log.info("ifvar: " + ifvar)

log.info("condition: " + condition)

If控制器条件

${__groovy(vars.get("modifiedURL") != "",)}

问题现象

最后一个HTTP Sampler路径设置为https://API.com/path1/${modifiedURL},预期生成两个目标路径:

  • https://API.com/path1/example?parameter1&parameter2
  • https://API.com/path1/example2?parameter3&parameter4

但实际运行时,采样器仅使用基础API路径,未拼接${modifiedURL}。调试发现日志中ifvar和condition显示为false,但modifiedURL并非空值或null。

解决方案

1. 修复JSR223脚本的语法错误

JSR223本身就是Groovy环境,无需嵌套__groovy函数,修改变量判断逻辑:

// 替换原有的ifvar和condition定义
def ifvar = vars.get("modifiedURL") != ""
def condition = vars.get("modifiedURL") != null && !vars.get("modifiedURL").isEmpty()

2. 校验ForEach Controller配置

  • 确认“输入变量前缀”与JSON Extractor提取的变量名匹配(比如JSON Extractor生成currentURL_1、currentURL_2,则前缀填currentURL)
  • 勾选“Add "_" before number”选项,保证循环变量能正确读取currentURL值

3. 简化If Controller条件

直接使用Groovy表达式判断,避免函数嵌套:

${modifiedURL} != ""

或切换到JSR223表达式模式,条件写:

vars.get("modifiedURL") != null && !vars.get("modifiedURL").isEmpty()

4. 规范HTTP Sampler路径配置

将基础域名放在HTTP Request Defaults或Sampler的“Server Name or IP”字段中,Path字段仅写相对路径:

/path1/${modifiedURL}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 20:55:19