I'm new to JMeter and am trying to write some Groovy Samplers. I am having trouble finding documentation/examples of how to communicate with the JMeter framework from the script code. I was hoping someone could point me to a good starting point for the documentation.
I tried the following in a JSR223 sampler
import org.apache.jmeter.samplers.SampleResult;
println(" running test")
SampleResult sr=new SampleResult();
sr.setResponseCode("300");
sr.setSuccessful(false);
sr.setErrorCount(2);
sr.setResponseData("This is the response");
ResponseCode=300
return sr;
But it looks as if it had no effect. Looking in a results tree listener output the sampler result is
Thread Name: Thread Group 1-1
Sample Start: 2016-03-22 17:38:07 CDT
Load time: 12
Connect Time: 0
Latency: 0
Size in bytes: 0
Headers size in bytes: 0
Body size in bytes: 0
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK
....
It won't work that way.
If you look into JSR223 Sampler GUI you will see some pre-defined variables like:
So you already have an instantiated SampleResult which can be used directly like:
SampleResult.setResponseCode("300");
SampleResult.setSuccessful(false);
SampleResult.setErrorCount(2);
SampleResult.setResponseData("This is the response");
For more information on using groovy scripts and scripting best practices check out Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With