Applies to ReadyAPI 3.3, last modified on July 03, 2020

ReadyAPI supports the libraries for Groovy classes. With these libraries, you can centralize common tasks and functionality, or create full-scale script extensions to be used within your projects.

Specify the scripts 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. First, create it:

  1. Create a folder – for example, C:\GroovyLibrary.

  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!" }
    }

Import the package

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

  1. Open Preferences > ReadyAPI.

  2. In the Script Library field, specify the folder with the created library, for example C:\GroovyLibrary.

  3. Restart ReadyAPI to load the library.

  4. Create a Groovy Script test step.

  5. 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()
  6. Click Run to test your script.

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

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

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 2020: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 2020:INFO:Hello, Mike!

Call static methods

To call the static methods specified in your library, use the following script:

Groovy

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

It should produce the following output:

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

Load the library dynamically

To load the library during the runtime, use the following script:

Groovy

project.scriptLibrary = project.getPropertyValue("dataPath")
Thread.sleep(12000)

See Also

Groovy Script Test Step
Script Editors
Scripting

Highlight search results