Blazemeter 运行 Jmeter 脚本 - JSR223 脚本中的问题,检索任务 javax.script.ScriptException:groovy.lang.MissingMethodException

Blazemeter running Jmeter script - Problem in JSR223 script, Retrieve Tasks javax.script.ScriptException: groovy.lang.MissingMethodException

我试图在 blazemeter 中 运行 一个工作的 jmeter 脚本,但是当它尝试 运行 我的 groovy 脚本时出现以下错误。我正在尝试访问在先前 groovy 脚本中设置的 jmeter 属性。

<JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="Get Tasks to be Added" enabled="true">
            <stringProp name="cacheKey">true</stringProp>
            <stringProp name="filename"></stringProp>
            <stringProp name="parameters"></stringProp>
            <stringProp name="script">

import groovy.json.JsonOutput;

def failureMessage = &quot;&quot;;
def jsonResponse = null;

JsonSlurper JSON = new JsonSlurper ();
JsonOutput JsonOut = new JsonOutput ();

try {
    def numTasksToAdd = vars[&apos;numTasksToAdd&apos;] as Integer;    
    jsonResponse = JSON.parseText(prev.getResponseDataAsString());
    props.put(&quot;tasks&quot;, JsonOut.toJson(jsonResponse.result[0..numTasksToAdd]))
    } catch (Exception e) {
    failureMessage += &quot;Invalid JSON.\n&quot;
    log.info(e.toString());
}
</stringProp>
<stringProp name="scriptLanguage">groovy</stringProp>
</JSR223PostProcessor>

第二个groovy脚本

<JSR223PostProcessor guiclass="TestBeanGUI" testclass="JSR223PostProcessor" testname="Retrieve Tasks " enabled="true">
          <stringProp name="cacheKey">true</stringProp>
          <stringProp name="filename"></stringProp>
          <stringProp name="parameters"></stringProp>
          <stringProp name="script">

import groovy.json.JsonSlurper;

JsonSlurper JSON = new JsonSlurper ();

var props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties();

def failureMessage = &quot;&quot;;
def tasks = null;

try {
    
     tasks = JSON.parseText(props.get(&quot;tasks&quot;));

    for (i = 0; i &lt; tasks.size(); i++) {
        
            vars.put(&quot;taskId&quot; + i.toString(), tasks[i].id.toString());
            vars.put(&quot;taskTag&quot; + i.toString(), tasks[i].sourceTag.toString());
        
    }  

    vars.put(&quot;taskCount&quot;, tasks.size().toString());
         
    } catch (Exception e) {
    failureMessage += &quot;Invalid JSON.\n&quot;
    log.info(e.toString());
}


</stringProp>
<stringProp name="scriptLanguage">groovy</stringProp>
</JSR223PostProcessor>

在我的 jmeter 文件中,出现以下错误

2021-11-17 20:18:51,294 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, Retrieve Tasks 
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.var() is applicable for argument types: (java.util.Properties) values: [[jdbc.config.jdbc.driver.class:com.mysql.jdbc.Driver|org.postgresql.Driver|oracle.jdbc.OracleDriver|com.ingres.jdbc.IngresDriver|com.microsoft.sqlserver.jdbc.SQLServerDriver|com.microsoft.jdbc.sqlserver.SQLServerDriver|org.apache.derby.jdbc.ClientDriver|org.hsqldb.jdbc.JDBCDriver|com.ibm.db2.jcc.DB2Driver|org.apache.derby.jdbc.ClientDriver|org.h2.Driver|org.firebirdsql.jdbc.FBDriver|org.mariadb.jdbc.Driver|org.sqlite.JDBC|net.sourceforge.jtds.jdbc.Driver|com.exasol.jdbc.EXADriver, ...]]
Possible solutions: eval(java.io.Reader), eval(java.lang.String), wait(), every(), any(), wait(long)
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:44) [ApacheJMeter_components.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:931) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:569) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) [ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) [ApacheJMeter_core.jar:5.2.1]
    at java.lang.Thread.run(Thread.java:829) [?:?]

为了解决编译失败的问题,您需要更改:

var props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties();

对此:

def props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties();

通常您甚至不需要这一行,因为 props shorthand 是 JSR223 Test Elements:

的预定义变量

查看 Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details on this and other JMeter API shorthand 暴露于 Groovy。