a1dutch
a1dutch

Reputation: 48

Validating User defined variables

I have several user defined variables set via properties i would like to validate.

I can use a jsr223 sampler to validate these but i dont want to duplicate all the names which are already set in the user defined variables.

Is there some programatic way to get hold of the declared user defined variables, possibly via the sampler or context variables?

Upvotes: 0

Views: 126

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

There is vars shorthand for JMeterVariables class instance which holds all the JMeter Variables (including the ones you set via User Defined Variables class).

Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on vars and other JMeter API shorthands available for the JSR223 Test Elements.


However this way you will get all the JMeter Variables including pre-defined ones

If you want to validate only the variables you have defined in the User Defined Variables configuration element you can play a little trick with the Reflection:

import org.apache.jmeter.config.Arguments
import org.apache.jorphan.collections.SearchByClass


def engine = engine = ctx.getEngine()
def test = engine.getClass().getDeclaredField('test')

test.setAccessible(true)

def testPlanTree = test.get(engine)

SearchByClass<Arguments> udvSearch = new SearchByClass<>(Arguments.class)
testPlanTree.traverse(udvSearch)
Collection<Arguments> udv = udvSearch.getSearchResults()

udv.each { arguments ->
    0.upto(arguments.size() - 1, { rowNum ->
        def arg = arguments.getArgument(rowNum)
        log.info('Variable name = ' + arg.getName() + ', variable value = ' + arg.getValue())
    })
}

Upvotes: 1

Related Questions