Call function from different sampler in JMeter - Groovy - groovy

I have a sampler that get data from DB (budget), and I have some assertions that I made using JSR223 assertion.
In each assertion I write the same function (check_budget) and to each assertion I pass different values (start_budget, end_budget etc)
The problem is that I duplicate the code for each assertion and it is not friendly for maintenance.(if logic change need to change at 7 duplicate functions)
It there a way to create a sampler and write generic function in it (calc_budget) and to call it from each assertion,
like creating a class in java and perform import?

In Groovy you have evaluate() function, so given you stored it into a JMeter Variable or JMeter Property or into a file you can call it like:
evaluate(vars.get('your_var'))
or
evaluate(new File('your_test.groovy'))
See Scripting JMeter Assertions in Groovy - A Tutorial article for more information.

In that case, and in general also, you can keep groovy script in a script file and call the same script file from all JSR223 elements
Script File
Name of a file to be used as a JSR223 script, if a relative file path is used, then it will be relative to directory referenced by "user.dir" System property

Related

How to access Groovy script from SAP CPI Script Collection in another Script?

I try to access a groovy script from a script collection in another script (SAP CPI). The script from the script collection contains an enumeration and i want to access this enumeration in a message mapping script. So i do the following:
Create the script in the script collection (same package as the iFlow)
Reference the script in the integration flow
Import the enumeration and use it (it's a script function used in message mapping)
Simulate the mapping (which works if i copy enum to mapping script as well)
Runtime exception during processing target field mapping /ns1:Messages/ns1:Message1/PerPhone/PerPhone/phoneType. The root message is: Exception:[com.sap.aii.mappingtool.tf7.rt.BehaviorInvocationException: groovy.lang.MissingPropertyException: No such property: BonusScheme for class: Script29] in class com.sap.aii.mappingtool.tf7.ScriptHandler method addPicklistValue[[Ljava.lang.String;#1e57ab7f, [Ljava.lang.String;#29c56946, [Ljava.lang.String;#11ba6ab8, [Ljava.lang.String;#3d2f6b53, [Ljava.lang.String;#1e0033da, com.sap.aii.mappingtool.tf7.rt.ResultListImpl#1c8de605, com.sap.xi.mapping.camel.impl.MappingContextImpl#470ff907, com.sap.aii.mappingtool.tf7.rt.Context#5e20a086] on the exchange: Exchange[ID-ff2d2c8d-4286-4e5a-5b75-1556-1657430698694-1068-1]
Seems that my enumeration isn't known and therefore can't be referenced. I'm just asking myself - why? It's everything deployed. Someone has an idea?
I think You cannot reference a Script from Script Collection in a Messaging Mapping.
Even if you want to reuse a Common Script like getHeaders or getProperties Script as Custom Function from a Script Collection, You cannot.
it's just the way it was built.

Store custom Groovy function in JMeter UI

Been struggling with this a lot lately: How do I store a custom Groovy script with imports etc in the JMeter UI so I can reuse it later?
I don't want to alter JMeter startup property files in any way
I want to be able to call this Groovy 20+ times within the JMX with different parameters
From the JMeter doc:
Once the script is working properly, it can be stored as a variable on
the Test Plan. The script variable can then be used to create the
function call.
The groovy (compatible with Beanshell) is 62 lines and includes imports of custom JAR files. If I could store this as a var callable with __groovy(param) that would be great, I don't see how to do that from the docs. Setting up 20 JSR223s is incredibly clunky but I am coming up with workarounds if there is no JMeter way to do this.
References:
https://jmeter.apache.org/usermanual/best-practices.html#developing_scripts
Depending on what you're trying to achieve:
There is a possibility to specify a path to the file with your code in any of JSR223 Test Elements so you won't have to copy and paste it multiple times into "Script" area so in case of changes you will need to amend it in one place only:
There is groovy.utilities property where you can specify the path to your .groovy file containing the logic callable from the __groovy() function, it defaults to bin/utility.groovy
You can compile your code to .jar file and store it under JMeter Classpath, this way you will be able to call your functions from any place. See How to Reuse Your JMeter Code with JAR Files and Save Time article for example implementation/usage details

Transfer parameters between Groovy scripts in SoapUI

I have a Groovy script, it is present as a test step. I need to pass a parameter value, a variable from it to another Groovy script, which is in a script assertion, in a SOAP request. These are executed after each other. Property Transfer step is unable to do this.
You can use this statement in Script Assertion of Request
context.testCase.setPropertyValue("Prop","testing")
So here you are setting a testcase Property.
Now you can use that property in Groovy Script
def val=context.expand('${#TestCase#Prop}')
log.info val
So the value stored in Property Prop in Script Assertion is used in Groovy Script

BSF Assertion from script file does not load UDVs

I am trying to use groovy scripts as BSF assertion in JMeter. The script written inside the JMETER assertion script box works well, but when I try to use it through a groovy file it is not loading the User Defined Variables it needs for assertions
It says
org.apache.bsf.BSFException: exception from Groovy: groovy.lang.MissingPropertyException: No such property: mobileNumber class: D__RESTAPITesting_JmeterBSFAssertionScripts_Script1
Not sure why it is looking for property when ${..} refers to a variable (if I am not wrong). Any help on the error message and how to use a script file for assertions ?
The scripts I have written are saved as *.groovy. Do I need to save scripts in some other extensions for BSF to read it correctly ?
Pass your User Defined Variables via Parameters input like ${foo} ${bar}
In your .groovy script body refer variables as args[0] args[1]
See image below for details (the solution works fine for file inputs as well) and How to Use JMeter Assertions in 3 Easy Steps guide for advanced information on using JMeter's assertions.

Can I use Groovy scripts in SoapUI to enable/disable assertions?

I'm using SoapUI Pro and a DataSource/DataSink loop to test a web service.
To make life more fun, I need to pull from four distinct source files, all of which will cause different expected results.
I'd really like to do this in a single test loop, because having scripts with multiple loops tends to crash SoapUI more often than not, but the sticking point is assertions.
How can I enable or disable assertions in a Groovy script in SoapUI? GetData doesn't give me anything to hook onto, and a documentation dive did not reveal the proper syntax. I'd assume something like testCase.assertion, but there's no such property as "assertion" on testCase.
Alternately, can I use a Groovy script to change the assertion's content? In other words, if I want phrase X with file 1, phrase Y with file 2, I'm just as happy using the same assertion, so long as I can change the content it's trying to match.
You could use your Groovy script to set some kind of property testCase.setPropertyValue('expected', 'value'), based on which file you are reading. You could then use property expansion ${testCase#expected#} in the assertion content.

Resources