Groovy Script Libraries

Applies to ReadyAPI 3.52, last modified on April 25, 2024

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.

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:

    Groovy

    package readyapi.demo

    // Create the Callee class to be called from ReadyAPI.
    class Callee
    {
        String hello()
        {
            return "Hello world!"
        }
        def static salute( who, log ) { log.info "Hello again $who!" }
    }
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:

    Groovy

    import Callee
    // Call the class from the library and post the returned information to the log.
    c = new Callee()
    log.info c.hello()
  4. Click Run to test your script.

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

    Tue Jan 1 10:56:08 EST 2024: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:

    Groovy

    package readyapi.demo

    // Create the Callee class to be called from ReadyAPI.
    class Callee
    {
        String hello()
        {
            return "Hello world!"
        }

        String hello(String who)
        {
            return "Hello $who"
        }

        def static salute( who, log ) { log.info "Hello again $who!|" }
    }
  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 2024:INFO:C:\GroovyLib\Callee.groovy is new or has changed, reloading...
  4. Modify the script you have created before:

    Groovy

    import Callee
    // Call the class from the library and post the returned information to the log.
    c = new Callee()
    log.info c.hello("Mike")
  5. Click Run to test your script.

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

    Tue Jan 1 10:56:08 EST 2024: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:

Groovy

readyapi.demo.Callee.salute( "Mike", log )

It should produce the following output:

Tue Jan 1 10:56:08 EST 2024:INFO:Hello again Mike!

Load the library dynamically

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

Groovy

Set the path to the folder containing scripting libraries
System.properties[ 'soapui.scripting.library' ] = "C:\\Work\\GroovyLibs"
Thread.sleep(3000) //The three-second pause to make sure the library is loaded

See Also

Scripting

Highlight search results