BSF Assertion from script file does not load UDVs - groovy

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.

Related

How to parametrize JMeter Loop Controller with Groovy function?

I am pretty new to Jmeter but can handle "simple" test plans. I really do need help with the issue I am facing. Currently I run into a problem regarding how to parametrize the Loop Controller loop count Groovy function.
I did found a lot of examples but they are all with static loop count like: ${__groovy(new File('test.csv').readLines().size(),)}.
I really would like to parametrize the part 'test.csv'. Therefor I replaced that with ${csvFile}, like:
${__groovy(new File(${csvFile}).readLines().size(),)}.
In the 'User Defined Variables' section of the 'Test Plan' I created the variable 'csvFile'.
Test plan image in here
When I execute the script I retrieve an error in the log like:
2022-01-25 11:35:14,870 WARN o.a.j.f.Groovy: Error running groovy script
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script16.groovy: 1: Unexpected input: '\' # line 1, column 12.
new File(D:\devenv\projecten\JM_InterestDeterminationService\inputmessages.csv).readLines().size()
^
1 error
Is the error pointing to the backslash? Is that because of the Windows OS?
Must I replace the '\' with a '/'? How and where can I do that?
I started fiddling around with .replaceAll("\\", "/") and .replaceAll("\\\\", "/"), but I don't know where to put it and what the correct syntax is.
Could any of you please help me?
That's one of the reasons why it's not recommended to inline JMeter Functions or Variables into Groovy scripts.
Just use vars shorthand for JMeterVariables class instance to read your csvFile variable value like:
${__groovy(new File(vars.get('csvFile')).readLines().size(),)}
and it should resolve your issue.
More information on this and other JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy

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

Call function from different sampler in JMeter - 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

confusion in context variable and Context Class in soap Ui

context,testRunner variables(built-in) in SoapUI are objects/instances of which classes in soapui api.
I was under impression that context variable is an instance of
com.eviware.soapui.impl.wadl.inference.schema.Context
but in the Context class i could not find exapnd method which we use regualrly as shown below
context.expand('${#level#request}')
Please some body clarify...if both are difference where can i find list of all methods of context variable
Thanks
The class of the context variable could be different depending on context.
The best way to get the class name to print it out:
log.info( context.getClass() )
If we talk about groovy script test step then context should be
com.eviware.soapui.impl.wsdl.panels.support.MockTestRunContext if you run just your script without running the whole test case.
com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext if you run the whole or part of the test case including your script.
probably it could be other for different soapui project types..
So, what is the context of your groovy script?
Btw, how did you get com.eviware.soapui.impl.wadl.inference.schema.Context ?
SoapUI initializes certain variables in certain level.
Here are the list of variables available at different level.
Project Setup Script
context
project
runner
log
Similarly, below variables available at Test Suite Setup Script
context
testSuite
runner
log
And also certain variables available in Test Case Setup Script.
The same is with TearDown Scripts as well.
However, If I understand right, you were referring to Groovy Script test step.
And the following variables are available there are:
testRunner
context
log
To be more precise, context.expand() is used to read certain property values either from test case, suite or project.
In order to read test case level property, CASE_PROPERTY
User one of the two:
context.expand('${#TestCase#CASE_PROPERTY}')
context.testCase.getPropertyValue('CASE_PROPERTY')
In order to read test suite level property, SUITE_PROPERTY
User one of the two:
context.expand('${#TestSuite#SUITE_PROPERTY}')
context.testCase.testSuite.getPropertyValue('SUITE_PROPERTY')
In order to read test project level property, PROJECT_PROPERTY
User one of the two:
context.expand('${#TestSuite#SUITE_PROPERTY}')
context.testCase.testSuite.getPropertyValue('SUITE_PROPERTY')

How to read a txt file, get a data and store it as variable in Custom Properties of SOAPUI using Groovy?

i started to use Soapui this week and i did some tests like sending a request POST and save the response as txt file ina folder.
What i'm trying to do is to read this txt file, copy a sepcific data and store it in Custom Properties.
Because i want to use this object in the nest Request POST which is depending of the first request.
I want to do it in Groovy.
i have only the Open source SOAPUI version 5.0.0
Thank you
You've to add a groovy test step in your test case and do it similar as you would in java, check groovy documentation.
Only as a reference SOAPUI 5.2.0 has the groovy 2.1.7 version (check the dependency in pom.xml) so in groovy scripts which runs on SOAPUI you can use the java standard api included in the jre, the SOAPUI classes, the groovy 2.1.7 API among some others, additionally you can include other jars in SOAPUI\bin\ext in order to use them in groovy script.
Finally you're asking about to read some data from a file and write it to a custom property, so for example you can do it as follows:
// read the file from path
def file = new File('/path/yourFile')
// for example read line by line
def yourData = file.eachLine { line ->
// check if the line contains your data
if(line.contains('mySpecifiyData=')){
return line
}
}
// put the line in a custom property in the testCase
testRunner.testCase.setPropertyValue('yourProp',yourData)
Since your problem it's not clear I show you a possible sample showing how to read a file looking for specific content and saving this content in a custom property in the testCase.
Note that in groovy scripts the are a global objects which you can use: testRunner, context and log, in this sample I use testRunner to access testCase and its properties, in the same way you can go thought testRunner to access testSuites, project, testSteps etc... check the documentation:
http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
http://www.soapui.org/Functional-Testing/working-with-scripts.html
Hope this helps,

Resources