JMeter Groovy Set a variable for each sampler - groovy

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.

Related

Convert Jmeter __V function variable into groovy vars.get

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

Run generic groovy script assertion in SoapUI

Is it possible to / how do you run a groovy script from a SoapUI assertion without copy/pasting the script into all of your test steps where you need the same script executed? Is it possible to write a script outside of the assertion and run the script like you are calling a method? So that you can reuse the assertion script in multiple test steps.
So far, I've tried to call a groovy test step from within the assertion, but the run() method requires a testRunner variable which is unavailable from within the assertion. I've also tried to write a groovy script as a subsequent test step (not an assertion) that calls another groovy test step script, but I was unable to transfer the response from one test step to the next (Honestly, I'd rather not create test steps that are really just assertions).
Note: this is not a duplicate of How to create variables in soapui test case that can be accessed across all test steps - groovy test step & script assertion test step? because that question pertains to storing properties, not reusing scripts.
I was able to finally figure out my 2nd approach: add another groovy script as a subsequent test step that has assertions and passes the response. The script is:
context.response = context.expand('${MyTestStep#Response}') // store response to context variable
Object result = testRunner.testCase.testSuite.testCases['Validate Response'].testSteps['Validate Response'].run(testRunner, context)
if(result.getError() != null) {
log.error("error", result.getError())
assert false
}
assert true
MyTestStep is the test step before the groovy script. Validate Response is the name of the test case of the groovy script which is also called Validate Response and is executed via the run method.

JMeter share Regular Expression array variables between Thread Groups

In Thread Group #1 I have a Regular Expression with Match Number set to -1 and I want to use the complete variable in Thread Group #2.
I am currently able to share normal variables using props.put but I am not able to share the complete array to then obtain the values using __V function on Thread Group #2.
Is this feasible?
Just add JSR223 PostProcessor (make sure it goes after Regular Expression Extractor) and use the following code:
vars.entrySet().each { var ->
if (var.getKey().startsWith('foo')) {
props.put(var.getKey(), var.getValue())
}
}
In another Thread Group you can convert properties into variables using the same approach:
props.entrySet().each {prop ->
if (prop.getKey().startsWith('foo')){
vars.put(prop.getKey(),prop.getValue())
}
}
Just replace foo with your variable reference name and that would be it.
You can find information regarding Groovy scripting in JMeter in Apache Groovy - Why and How You Should Use It guide if needed.

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

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