Search code examples
groovyjmeterjmeter-pluginsblazemeter

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


I am attempting to run a working jmeter script in blazemeter, but getting the following error where it tries to run my groovy scripts. I am writing trying to access jmeter properties that were set in a previou groovy script.

<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>

second groovy script

<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>

is in my jmeter file, and getting the following error

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) [?:?]

Solution

  • In order to resolve the compilation failure issue you need to change this:

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

    to this:

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

    In general you don't even need this line because props shorthand is a pre-defined variable for the JSR223 Test Elements:

    enter image description here

    See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details on this and other JMeter API shorthands which are exposed to Groovy.