如何修复while控制器中的Javascript错误?

时间:2019-02-02 10:54:58

标签: javascript jmeter

我在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]

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

2 个答案:

答案 0 :(得分:0)

也许您还缺少一些反引号。

var counter = 42,
    __javaScript = v => `got ${v}`;

console.log(`${__javaScript(`${counter}` < 10)}`);
//                          ^          ^

答案 1 :(得分:0)

While loop的第一次迭代过程中可能未定义${counter}变量的情况,这些选项位于:

  1. 使用User Defined Variables来给${counter}变量赋予初始值
  2. 检查是否使用__isVarDefined() function来设置counter变量

但是,您可以通过迁移到__groovy() function(通过recommended scripting option since JMeter 3.1的方式用一块石头杀死两只鸟),条件是:

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