如何修复JavaScript错误的,而控制器?

问题描述 投票:0回答:2

我使用的是尽管控制器我的JMeter脚本。我已经给出,而控制器在以下条件

${__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]

任何人有这个错误背后的原因,任何想法?如何解决呢?

javascript jmeter
2个回答
0
投票

这可能是你的${counter}变量的While loop的第一次迭代过程中没有被定义的情况下,选择在:

  1. 得到${counter}变量的初始值使用即User Defined Variables
  2. 检查counter变量是否被设置,即使用__isVarDefined() function

然而,你可以通过迁移到__groovy() function(这是顺便recommended scripting option since JMeter 3.1),条件是一举两得:

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

0
投票

也许你错过一些反引号。

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

console.log(`${__javaScript(`${counter}` < 10)}`);
//                          ^          ^
© www.soinside.com 2019 - 2024. All rights reserved.