Convert Jmeter __V function variable into groovy vars.get - groovy

I have a JMeter variable like this:
${__V(myVar${counter})}
How to get the value of this variable inside JSR223 sampler by using vars.get() ?

Use nested get:
vars.get("myVar" + vars.get("counter" ));

There are 2 options:
Use "Parameters" section of the JSR223 Sampler (as the documentation suggests)
Use GString Template feature like:
vars.get("myVar${counter}")
where vars is the shorthand for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn more about JMeter API shorthands available for JSR223 Test Elements

Related

In what ways can I access a variable defined in a JSR223 Sampler from inside a child JSR223 PostProcessor?

In JMeter I have a JSR223 Sampler in which I have some variable with a timestamp value which changes on each sampler iteration.
Directly underneath the JSR223 Sampler, I have a JSR223 PostProcessor. In this PostProcessor, I want to get the variable from the parent JSR223 Sampler, do some processing on it (subtract some other timestamp value) and use the result into prev.elapsedTime(processedTimestampValue) to amend the time of the previous JSR223 Sampler.
In what ways can I achieve this?
I think that I can use vars.put('variableValue') in the JSR223 Sampler to make the variable value available to JMeter and then use vars.get('variableValue') in JSR223 PostProcessor to make use of this value, but I was wondering if there is another way.
Directly underneath means that the JSR223 PostProcessor will be applied to all Samplers in its scope, if you want to amend time of a single Sampler - make the JSR223 PostProcessor a child of this Sampler
I was wondering if there is another way - there is, you can use "Parameters" section as well:
More information: JSR223 Sampler Documentation

SOAPUI context variables - How does Groovy make this possible?

Sorry to all you Groovy dudes if this is a bit of a noob question.
In SOAPUI, i can create a Groovy script where i can define an arbitrary variable to the run context to retrieve at a later time.
context.previouslyUndefinedVariable = 3
def num = context.previouslyUndefinedVariable
What feature of Groovy allows previously undefined variables to be added to an object like this? I would like to learn more about it.
Many thanks in advance!
Groovy has the ability to dynamically add methods to a class through metaprogramming.
To learn more, see:
What is Groovy's MetaClass used for?
Groovy Goodness: Add Methods Dynamically to Classes with ExpandoMetaClass
Runtime and compile-time metaprogramming
The accepted answer is a bit of a poor explanation for how SoapUI is doing it.
In this case, context is always an instance of some SoapUI library java class (such as WsdlTestRunContext), and these are all implementations of Map. You can check context.getClass() and assert context in Map.
When you look up a property on a Map, Groovy uses the getAt and putAt methods. There are various syntaxes you can use. All of these are equivalent:
context.someUndef
context.'someUndef'
context[someUndef]
context['someUndef']
context.getAt('someUndef')
And
context.someUndef = 3
context.'someUndef' = 3
context[someUndef] = 3
context['someUndef'] = 3
context.putAt('someUndef', 3)
I like to use any of the above that include quote marks, so that Groovy-Eclipse doesn't flag it as a missing property.
It's also interesting that Groovy looks for a getAt() method before it checks for a get method being referred to as a property.
For example, consider evaluating "foo".class. The String instance doesn't have a property called class and it also doesn't have a method getAt(String), so the next thing it tries is to look for a "get" method with that name, i.e. getClass(), which it finds, and we get our result: String.
But with a map, ['class':'bar'].class refers to the method call getAt('class') first, which will be 'bar'. If we want to know what type of Map it is, we have to be more specific and write in full: ['class':'bar'].getClass() which will be LinkedHashMap.
We still have to specify getClass() even if that Map doesn't have a matching key, because ['foo':'bar'].class will still mean ['foo':'bar'].getAt('class'), which will be null.

jmeter pass variable between threads

TestPlan Thread Group HTTP Request1 ->Regular Expression Extractor - Return 10 Results - URLs -- Single Thread
ForEach Controller - Using variable from extractor - Successfully Loops through above results HTTP Request2 ->Regular Expression Extractor - Return 10 Results
This above is under 1 thread
I want to have ForEach Controller under different thread --run multiple therads and use the URLs extracted from 1 thread HTTP Sampler -- I tried to use these two approaches
https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups
How do I pass a variable from one Thread Group to another in JMeter
but somehow now managed it to work
Please help
ForEach Controller will not work with JMeter Properties, it is designed to work only with JMeter Variables so if you want to pass them between different Thread Groups you will need to do some scripting.
Add JSR223 PostProcessor after the Regular Expression Extractor and put the following code into "Script" area
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
replace foo with what you have as a Reference Name in the Regular Expression Extractor. The above code will convert all variables which names start with foo into the relevant JMeter Properties
Add Test Action sampler to the second Thread Group (you don't need to measure time of properties to variables conversion, do you)
Add JSR223 PreProcessor as a child of the Test Action sampler
Put the following code into "Script" area
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
the above code convert JMeter Properties into JMeter Variables so you will be able to use them in the ForEach Controller in the second Thread Group. Again, replace this foo with the reference name of your own variable.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests

JMeter Groovy Set a variable for each sampler

Is there a possibility to have variables that depend on the listener where there lay in?
So I want to execute two samplers which are working with a JSR223 Assertion. I use a groovy code which asks for the value of the variable "name". If I execute Sampler A it should say "Tom" and if i execute Sampler B it should say "Paul".
It has to be possible to execute both at the same time.
You have sampler shorthand in the JSR223 Assertion which stands for this or that Sampler. So you can check its name by calling sampler.getName() function which will basically execute underlying AbstractTestElement.getName() method. The relevant code would look like:
if (sampler.getName().equals('Sampler A')) {
log.info('Tom')
}
else if (sampler.getName().equals('Sampler B')) {
log.info('Paul')
}
You can set a JMeter Variable from Groovy code using vars shorthand which in its turn stands for JMeterVariables class instance like:
vars.put('foo', 'bar')
Once done you will be able to refer created variable as ${foo} where required - it will have the value of bar
Check out Scripting JMeter Assertions in Groovy - A Tutorial article for more details.

Why do we use context in Groovy

I have used context in groovy in and as:
def gu = new com.eviware.soapui.support.GroovyUtils(context)
What does it refer to?
Context holds information about the environment and is required to freely navigate in the mentioned environment.
#dmahapatro:
context available in groovy scripts in SoapUI is the context of executions of a test case/test suit.
From the documentation:
When scripting inside some kind of "run", there is always a context variable available for getting/setting context-specific variables. The contexts are:
SubmitContext - available within one submit of a request
TestRunContext - available from within all scripts in a TestCase run
LoadTestRunContext - available in LoadTest setup/tearDown scripts and
from the exectued TestCase context via the LoadTestContext context
variable
MockRunContext - available in MockService startup/shutdown scripts
and MockOperation/MockResponse dispatch scripts
All these inherit from the PropertyExpansionContext interface which
has methods for setting/getting properties and an expand method which
can be used to expand arbitrary strings containing
Property-Expansions, read more on this in the soapUI User-Guide.

Resources