Groovy 将空值发送到 JMeter 请求中

Groovy sends the empty value into the JMeter Request

来自上一个请求Groovy脚本正确地将数据保存到 csv 文件中,但在以下请求中传递空值。

不确定到底出了什么问题。尝试对值进行硬编码,请求正常。

https://xx.xx.xx.xxx/xx/projects/2228/week-endings/${__groovy(new%20File('${DH-week-endings}').readLines().get(${counter_value}),)}/departments/${id}/timecards

这是日志中的错误

2022-01-11 12:56:55,519 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2022-01-11 12:56:55,549 WARN o.a.j.f.Groovy: Error running groovy script
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.$() is applicable for argument types: (Script66$_run_closure1) values: [Script66$_run_closure1@61d664cd]
Possible solutions: is(java.lang.Object), any(), get(java.lang.String), use([Ljava.lang.Object;), tap(groovy.lang.Closure), any(groovy.lang.Closure)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_311]
    at org.apache.jmeter.functions.Groovy.execute(Groovy.java:120) [ApacheJMeter_functions.jar:5.4.3]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:138) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:113) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:91) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.testbeans.TestBeanHelper.unwrapProperty(TestBeanHelper.java:129) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.testbeans.TestBeanHelper.prepare(TestBeanHelper.java:84) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.engine.StandardJMeterEngine.notifyTestListenersOfStart(StandardJMeterEngine.java:202) [ApacheJMeter_core.jar:5.4.3]
    at org.apache.jmeter.engine.StandardJMeterEngine.run(StandardJMeterEngine.java:382) [ApacheJMeter_core.jar:5.4.3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_311]
Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.$() is applicable for argument types: (Script66$_run_closure1) values: [Script66$_run_closure1@61d664cd]
Possible solutions: is(java.lang.Object), any(), get(java.lang.String), use([Ljava.lang.Object;), tap(groovy.lang.Closure), any(groovy.lang.Closure)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    ... 10 more

请看下面的截图

我认为正确的语法应该是这样的:

${__groovy(new File(vars.get('DH-week-endings')).readLines().get(vars.get('counter_value') as int),)}
  1. 如果您不确定是否要构建合适的 JMeter 函数 - 请选择 Function Helper Dialog
  2. Since JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting 所以考虑迁移所有剩余的 Beanshell 元素
  3. 不要内联 JMeter Functions or Variables into Groovy/Beanshell scripts, go for vars shorthand for JMeterVariables class instance 而是