Trying to have a JSR223 Listener on Test Plan level so it listens to all thread groups. What I like to achieve is that it only logs every HTTP Request's methods (GET, POST, HEAD, OPTIONS, PUT, DELETE and TRACE) and print every Method, URL, Response Code, Value and Response Data.
Having looked around, found some snippets left right and center and have come up with this:
try{
if (sampler.getUrl().toString().contains("http")){
log.warn('Method: ' + sampler.getMethod() + ' URL: ' + sampler.getUrl().toString());
sampler.getArguments().each {arg ->
log.warn('CODE: ' + prev.getResponseCode() + ' Value: ' + arg.getStringValue())
}
log.warn('CODE: ' + prev.getResponseCode() + ' ResponseData: ' + prev.getResponseDataAsString());
}else{
log.warn('Not a HTTPRequest')
}
}
catch (Throwable ex)
{
log.warn('Something went wrong', ex);
throw ex;
}
And I get the results in my log viewer as desired.
The challenge I am facing is if any other sampler type is active, it logs an error because that sampler is not an HTTP request but a dummy sampler, so it cannot return the requested values.
groovy.lang.MissingMethodException: No signature of method: kg.apc.jmeter.samplers.DummySampler.getUrl()
Now my example shows only two samplers in the Thread Group, but imagine a couple of 100 various samplers Figuring I need to change my 'if' statement to not check for the sampler argument, but for the sampler type instead (HTTPRequest), but good old google returns not much to go on. So, is it even possible to have an if statement that checks for sampler type, and what would that syntax be?
if (sampler==HTTPRequest)
???
You can use sampler.getClass()
and compare it to check if HTTP Request:
if("HTTPSamplerProxy".equals(sampler.getClass().getSimpleName())) {
You can check class name using log:
log.info(sampler.getClass().getSimpleName());