首页 > 解决方案 > 如何修复while控制器中的Javascript错误?

问题描述

我在我的 Jmeter 脚本中使用 While 控制器。我在 while 控制器中给出了以下条件

${__javaScript(${counter} < 10)}

尽管条件按预期工作,但它会在日志中引发以下错误。

2019-02-02 15:58:21,315 ERROR o.a.j.f.JavaScript: Error processing Javascript: [${counter} < 10]

javax.script.ScriptException: <eval>:1:1 Expected ; but found {
${counter} < 10
 ^ in <eval> at line number 1 at column number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:537) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:524) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402) ~[nashorn.jar:?]
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155) ~[nashorn.jar:?]
    at org.apache.jmeter.functions.JavaScript.executeWithNashorn(JavaScript.java:142) [ApacheJMeter_functions.jar:3.2 r1790748]
    at org.apache.jmeter.functions.JavaScript.execute(JavaScript.java:103) [ApacheJMeter_functions.jar:3.2 r1790748]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:141) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:116) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:101) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.WhileController.getCondition(WhileController.java:124) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.WhileController.endOfLoop(WhileController.java:56) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.WhileController.next(WhileController.java:102) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:219) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.GenericController.next(GenericController.java:173) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.LoopController.next(LoopController.java:123) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.GenericController.nextIsAController(GenericController.java:219) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.GenericController.next(GenericController.java:173) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.control.LoopController.next(LoopController.java:123) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.threads.AbstractThreadGroup.next(AbstractThreadGroup.java:87) [ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:274) [ApacheJMeter_core.jar:3.2 r1790748]

有人知道这个错误背后的原因吗?如何解决?

标签: javascriptjmeter

解决方案


可能是您的变量在While 循环${counter}的第一次迭代期间未定义的情况,选项位于:

  1. 使用即用户定义的变量${counter}变量一个初始值
  2. 检查counter变量是否设置使用ie __isVarDefined() 函数

但是,您可以通过迁移到__groovy() 函数(顺便说一下,这是自JMeter 3.1以来推荐的脚本选项 ),用一块石头杀死两只鸟,条件是:

${__groovy(vars.get('counter') == null || (vars.get('counter') as int) < 10,)}

推荐阅读