'JSR223 Pre Processor code should run for only HTTP Sampler

I have a JSR223 Preprocessor code given below:

String method = sampler.getMethod(); 

JMeter Test Plan given below:

      - JSR223 Pre Processor
      - HTTP Sampler
      - JSR223 Sampler / Any other sampler apart from HTTP

As JSR223 PreProcessor will apply to all the Samplers in the hierarchy, it gives below error for all the Samplers apart from HTTP Sampler.

2022-05-18 17:32:43,668 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.protocol.java.sampler.JSR223Sampler.getMethod() is applicable for argument types: () values: [] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.7.jar:3.0.7] at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.7.jar:3.0.7] at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.4.3] at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:45) [ApacheJMeter_components.jar:5.4.3] at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:?] at java.lang.Thread.run(Thread.java:834) [?:?] Caused by: groovy.lang.MissingMethodException: No signature of method: org.apache.jmeter.protocol.java.sampler.JSR223Sampler.getMethod() is applicable for argument types: () values: [] at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70) ~[groovy-3.0.7.jar:3.0.7] at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46) ~[groovy-3.0.7.jar:3.0.7] at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-3.0.7.jar:3.0.7] at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase$getMethod.call(Unknown Source) ~[?:?] at Script1366.run(Script1366.groovy:1) ~[?:?] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-jsr223-3.0.7.jar:3.0.7] ... 9 more

I want my JSR223 code to run only for HTTP Sampler



Solution 1:[1]

Check if HTTP Sampler before:

if (sampler instanceof org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase) { 
   String method = sampler.getMethod(); 
}

Similar code in JMeter

if (testElement instanceof HTTPSamplerBase) {

Solution 2:[2]

How about moving JSR223 Pre Processor to be a child of the HTTP Request sampler?

enter image description here

In this case its scope will be limited to the parent sampler only and it will not be executed before every sampler.

More information:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dmitri T