Jmeter log only HTTPSampler with JSR223 Listener

J. Doe

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;
}

enter image description here

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.

enter image description here

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) ???

user7294900

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store 2 variable's value into .csv file as 2 separate columns using JSR223 listener in Jmeter

JMeter - groovy MultipleCompilationErrorsException in JSR223 Sampler

JMeter JSR223 PostProcessor GET COOKIE

Problem in JSR223 script JSR223 Sampler, while connecting to Azure Cosmos DB from Jmeter

Jmeter - declare array variable in one JSR223 Sampler in order to access it in another JSR223 Sampler

JSR223 Listener with same UUID for every request

JMeter JSR223 Postprocessor not updating User Parameter

Scala JSR223 script using JMeter/Java context

JMeter - Can't use Java stream in JSR223 script

JMeter - Exception when using Beanshell as JSR223 Sampler with file

JMeter - JSR223 Sampler missing character encoding

JMeter external JSR223 java edited in Intellij

How to set success message in Jmeter JSR223 assertion

API Jmeter request modification upon using JSR223 PreProcessor

JSR223 PreProcessor generates invalid format using Groovy in Jmeter

how to use xml slurper with jmeter jsr223 assertion

Comparing two values in JMeter with JSR223 Assertion

Groovy method declaration in JMeter JSR223 Assertion

getting response times using JSR223 + JMeter

Jmeter JSR223 Sampler - Unable to Write Data To CSV File

JMeter - Access JSR223 variable in HTTP sampler

JMeter - Calling HTTP samplers in JSR223/BeanShell samplers

Jmeter - Using varible: from JDBC request into JSR223 PostProcessor

Create JMeter Test Plan using JSR223 Sampler

Read json file in jsr223 sampler in jmeter and extract data

Generate random values in JSR223 sampler in JMeter

Code in JMeter JSR223 Sampler comments is executed

JMeter Javascript language not available for JSR223 Sampler and WebDriver Sampler

How to update User Defined Variable in JMeter in JSR223 Sampler?