How reuse groovy scripts in soapui project? - groovy

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!

Related

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

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

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.

Can I combine all Groovy sources to one script?

I have a project on Groovy, which is built with Maven. Can I somehow on build step to merge all files with a source code into a single Groovy script for delivery to the end user?
That is impossible. Another way is use evaluate:
evaluate(new File("../tools/Tools.groovy"))

Groovy script classpath issue with SystemClassLoader

I'm writing a Groovy script which uses third party java code that I can't change.
This code uses (badly, I think) ClassLoader.getSystemClassLoader().getResourceAsStream("/hard/file/path/in/jar/file")
and expect to read a file.
Everything goes well from Java when using java -cp "/path/to/jar/file" ...
However, the third-party code is now to be integrated with a bunch of Groovy code we've already written, so we wanted to run it from groovy.
So we wrote a Groovy script, let it call test.groovy, and ran it as groovy -cp "/path/to/jar/file" test.groovy.
The problem is that code can't access the file resource, as it seems Groovy doesn't load its jars in the System ClassLoader directly.
For proof, with Thread.currentThread().getContextClassLoader().getResourceAsStream("/hard/file/path/in/jar/file") within the Groovy Script, I can read the file, but with ClassLoader.getSystemClassLoader().getResourceAsStream("/hard/file/path/in/jar/file"), I can't.
So, does anyone know how to load the class in System ClassLoader from a Groovy Script without beginning to try some dirty hacks (like metaclassing getSystemClassloader to return the context classloader)?
You could try adding the jar to the system classloader as well when your script runs, like so:
ClassLoader.systemClassLoader.addURL new File( '/path/to/jar/file' ).toURI().toURL()
PS: I assume you mean ClassLoader.getSystemClassLoader() in your question, rather than System.getSystemClassLoader()
You can try to put your jar into %GROOVY_HOME%\lib folder or make a wrapper around your groovy command and modify %CLASSPATH% variable before you start your Groovy process.

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