5
votes

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

....

2

2 Answers

4
votes

It won't work that way.

If you look into JSR223 Sampler GUI you will see some pre-defined variables like:

  • ctx
  • vars
  • props
  • SampleResult
  • etc.

JSR223 Pre Defined Variables

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.

0
votes

JMeter 5.4 JSR223 Sampler

more

//set Response Code
SampleResult.setResponseCode("201");
SampleResult.setSuccessful(false);
 
//set Response Message
SampleResult.setResponseMessage("This is message returned from JSR223 script");
 
//set Response Data
SampleResult.setResponseData("You will see this sentence in Response Data tab", "UTF-8");

println( "The Sample Label is : " + SampleResult.getSampleLabel() );
println( "The Start Time in miliseconds is : " + SampleResult.getStartTime() );
println( "The Response Code is : " + SampleResult.getResponseCode() );
println( "The Response Message is : " + SampleResult.getResponseMessage() );