passing jmeter sampler result from groovy

Viewed 5945

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 Answers

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() );
Related