Groovy Script Libraries

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.

Store your scripts

Specify the script folder

By default, ReadyAPI uses script libraries from the <ReadyAPI installation>\bin\scripts folder. To change it:

  1. Open Preferences > ReadyAPI.

  2. 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.

Organize your script packages

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.

Example

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.

1. Create the library

First, let's create the library:

  1. Open the <ReadyAPI installation>\bin\scripts folder.

  2. Inside that folder, create a Callee.groovy file.

  3. Add the following content to the file:

2. Import the package

Now that we have a package, we need to import it to ReadyAPI:

  1. Restart ReadyAPI to load the library.

  2. Create a Groovy Script test step.

  3. In that test step’s script editor, insert the following code:

  4. Click run.png to test your script.

  5. Check the script log. The script should post a result to it:

    Tue Jan 1 10:56:08 EST 2023:INFO:Hello world!

3. Modify the library file

Let's modify the library file to produce different output:

  1. Open the Callee.groovy file.

  2. Replace the existing content with the following code:

  3. 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...
  4. Modify the script you have created before:

  5. Click run.png to test your script.

  6. Check the script log. The similar result should appear:

    Tue Jan 1 10:56:08 EST 2023:INFO:Hello, Mike!

4. Call static methods

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!

Load the library dynamically

To load the library during test run, use the following script:

See Also

Publication date: