ReadyAPI supports Groovy script libraries for storing classes. This can be useful if you use the same scripts for common tasks throughout your project; in this case, you can store the scripts in Groovy libraries and easily reuse them. You can also create full-scale script extensions to be used within your projects.
By default, ReadyAPI uses script libraries from the <ReadyAPI installation>\bin\scripts
folder. To change it:
Open Preferences > ReadyAPI.
Specify the folder you want to use for your scripts in the Script Library field and click OK.
ReadyAPI will scan this folder for Groovy files and compile these during the startup. It also checks it for updates every 5 seconds and compiles or recompiles the new or updated scripts.
Tip
If you plan to run your project on a remote computer using VirtServer, LoadUI agent, TestEngine, or a CI/CD tool such as Jenkins or Azure DevOps, do not forget to copy the needed script libraries to the scripts folder on the remote computer.
Scripts from your packages should be placed in subfolders with the same name as the package. For example, a script in the soapui.demo
package should be placed in the /soapui/demo
folder.
The classes ReadyAPI compiles will appear in the parent class loader of all Groovy scripts. You can access them as standard Java classes.
Important
Each script file must be a valid class rather than an ordinary script.
The section below describes how to set up a Groovy object to use it in ReadyAPI, and later modify and reload it. In this example, we will use the Callee.groovy
library.
First, let's create the library:
Open the
<ReadyAPI installation>\bin\scripts
folder.Inside that folder, create a
Callee.groovy
file.Add the following content to the file:
Now that we have a package, we need to import it to ReadyAPI:
Restart ReadyAPI to load the library.
Create a Groovy Script test step.
In that test step’s script editor, insert the following code:
Click
to test your script.
Check the script log. The script should post a result to it:
Tue Jan 1 10:56:08 EST 2023:INFO:Hello world!
Let's modify the library file to produce different output:
Open the
Callee.groovy
file.Replace the existing content with the following code:
ReadyAPI automatically detects changes after you save the file. The appropriate information is posted to the ReadyAPI log:
Tue Jan 1 10:56:08 EST 2023:INFO:C:\GroovyLib\Callee.groovy is new or has changed, reloading...
Modify the script you have created before:
Click
to test your script.
Check the script log. The similar result should appear:
Tue Jan 1 10:56:08 EST 2023:INFO:Hello, Mike!
To call the static methods specified in your library, you don’t need to create a class instance:
It should produce the following output:
Tue Jan 1 10:56:08 EST 2023:INFO:Hello again Mike!
To load the library during test run, use the following script: