JMeter - submit JMeter function inside a script (JSR223 element) - groovy

Functions are very useful for writing a functional script and also helpful without writing real code.
Currently I can execute functions in most of Test Plan except in Script of JSR223 Sampler for example.
Is there a way to enable JMeter function inside JSR223/Beanshell script?
For example ${__log(Message)} or ${__Random(0,10)}
If the call for variable as ${..} is an issue, is there a workaround for calling a function?
Maybe there will be a problem calling groovy function inside groovy script, But can other functions be executed?

You should not be inlining JMeter functions and/or Variables into scripts as :
They might resolve into something causing compilation failure or unexpected behavior
In case of Groovy they will conflict with GStringTemplate, moreover compiled scripts caching will not be possible
So you have 2 options:
Use "Parameters" section of the JSR223 Test Elements like:
Go for code-based equivalents, i.e. using RandomUtils class:
More information: Apache Groovy - Why and How You Should Use It

Related

How to use #CompileStatic for pure groovy scripts in JMeter environment?

I'd like to take advantage of #CompileStatic annotation in my groovy scripts in jmeter environment. It helps a lot to discover issues in compile time.
I already started to use it in my classes but I don't know how to use it in case of plain groovy scripts. For example, I have the script below and there are the log and vars variables which are kind of global variables in JMeter environment. So, eventually they will be used.
If I add the #CompileStatic annotation to the method below IntelliJ paints red everything and compile will fail because the compiler doesn't know what these variables are.
So, the question is how to tell the compiler in a case of a script these variables has type and what type they have, and how an instance will be provided for the script?
I apologize, I'm not a groovy expert at all.
void checkingInputParameters() {
log.info("variable value:" + vars.get("some_variable_name"))
}
checkingInputParameters()
I think you are in the wrong path,
because CompileStatic is a groovy compiler option
let the Groovy compiler use compile time checks in the style of Java then perform static compilation, thus bypassing the Groovy meta object protocol.
JMeter (and I assume your tests in Intellij ) is using a java compiler
I don't think you should mix them for tests.
In JMeter use Cache compiled checkbox/feature
checking Cache compiled script if available

Can I create 'shared library' in Jenkins Pipeline in other language than Groovy?

I have python script which does a REST command and processes the result. I want this script to be used by different Jenkins Pipelines, one way I found through Jenkins official documentation is to use 'Shared library' and that examples(also others example which I found online) they use the Groovy.
My question is, is it possible to create a shared lib in other language than Groovy? For ex. python?
Short answer is no. All Jenkins Pipeline execution (right now) is specialized Groovy that is executed with the Pipeline: Groovy plugin that uses the Groovy CPS library to perform the compilation and runtime transformations. The Jenkins Pipeline ecosystem is very heavily tied to Groovy. That may change in the future, but is not what worth the effort right now.
What you can do, if you really want to use Python code in a Shared Library, is to put it in the resources/ folder of the library and then load and execute it with pipeline steps. Your use case for why you want to use Python is not stated (or what problem you are trying to solve), so below is a contrived example:
In your Shared Library: resources/com/mkobit/sharedlib/my_file.py
#!/usr/bin/env python
print("Hello")
Shared Library Groovy global variable: vars/mkobitVar.groovy
def runMyPython() {
final pythonContent = libraryResource('com/mkobit/sharedlib/my_file.py')
// There are definitely better ways to do this without having to write to the consumer's workspace
writeFile(file: 'my_file.py', text: pythonContent)
sh('chmod +x my_file.py && ./my_file.py')
}
In a consumer
#Library('mkobitLib') _
node('python') {
mkobitVar.runMyPython()
}

Reusing the groovy code in multiple Groovy Script test steps of SoapUI

I created functions(methods)in step1 using groovy in soapUI(open source) and calling in step2, it is not getting called, It is getting called only to that step1. I want to make those functions as global. can any one suggest me how to do that?
In order to achieve, what you are looking for is, to do the following:
Create classes (either in groovy or java of your preferred language)
Add the reusable methods into those classes
Compile the classes and create jar file
Copy the jar under SOAPUI_HOME/bin/ext directory
Restart the soapui tool
Now you should be able to call the reusable method from any of your project /test suite / test case/ test step.
Hope this is useful.

How reuse groovy scripts in soapui project?

Suppose I write a groovy function. Is it possible to store it to some teststep or another place in soapui project and include it with import in other groovy test steps?
SoapUI offers three options:
You can use the script library, where you store all your scripts in a separate directory and just call the classes.
You can use the run testcase step, where you store your script as a test which you can call from any other test.
You can compile your script into a jar, place it in $SOAPUI_HOME/bin/ext, and then call the classes inside your jar.
Note that the first two options are -Pro only features!

How to run a set of dependent groovy scripts without compilation?

I've got a set of groovy scripts (or should I say a groovy app) which has hierarchical java alike package structure and script names the same as class names. All of them are called from the main script (like a java class with main method). I need to call just that particular main script and get all other scripts executed in sequence when needed (or loaded and executed).
Practically, this can be acieved by having all scripts compiled and .class files obtained and put into classpath while running the main script, but that's quite redundant for a scripting (the idea is to have it working without compilation, even though groovy will do it somwhere behind the scene)
How can I achieve it?
Groovy can be used in a scripting environment quite easily - no compile step required. Read this:
http://groovy.codehaus.org/Running
You can write your usual main method etc. and call it like this,
groovy -cp foo/ foo/MyScript.groovy [arguments]
Or if you're in a *nix environment you can give it a shebang like so,
#!/usr/bin/env groovy
println("Hello world")
for (a in this.args) {
println("Argument: " + a)
}
and run it using ./fileName (provided you've marked it as executable)

Resources