Groovy Data Source

Applies to ReadyAPI 3.51, last modified on March 21, 2024

The Groovy data source generates property values by executing a groovy script.

In this topic, we focus on how to configure the Groovy data source. We assume you are already familiar with data-driven testing and how to configure a data-driven loop. Otherwise, please read Basic Concepts of Data-Driven Testing, or complete some of our tutorials before you continue.

You can use data sources of the Groovy type in the Data Source test step in functional tests. They are not available in virtual services.

Configuration

Specify the Groovy code that will generate the desired data.

The Groovy data source

Click the image to enlarge it.

Tip: If the font of the editor is not comfortable for you, change it by using Ctrl + mouse wheel.

To get an output value, use the built-in result object. It is an associative array, keys of which match the specified properties. You can use any string to name properties. For example, the following codesnippet assigns a string to the Sample property property:

Groovy

result["Sample property"] = "value"

You can use the following objects in your scripts:

  • context – Provides a scripting interface to the properties specific to the current test run context.

  • result – An associative array, keys of which match the data source properties.

  • testRunner – Provides a scripting interface to the test runner object that is executing the current test case and test step.

  • log – Provides a scripting interface to the ReadyAPI test log.

Generating multiple rows per iteration

ReadyAPI runs the data source script as many times as you set in the Rows Per Iteration option. At each iteration, it generates values for several rows. To get a value from a specific row, use the following syntax:

${DataSource-Test-Step#Property-Name::Row-Index}

To learn more, see Property Expansion.

Example

Let's imagine that you want your data source to return names and sizes of files in the home directory (for example, C:\Users\<username> on Windows). To do that:

  1. Create the File and Size properties in the data source.

  2. Set the data source type to Groovy.

  3. Paste the following script to the editor:

    Groovy

    // Import required class
    import groovy.io.FileType

    // Get current row
    def row = testRunner.testCase.testSteps["Data Source"].currentRow;

    // Get the home user folder
    def folderName = System.getProperty("user.home");
    // If you want to get files from another folder, use the following line instead:
    // def folderName = "C:\<your folder path>";

    // Declare an array that will contain file names
    def fileNames = [];

    // Declare an array that will contain file sizes
    def fileSizes = [];

    // Get a list of files in the directory
    new File(folderName).eachFile(FileType.FILES)
    {
        // Add file information to the arrays
        file -> {
            fileNames.add(file.name);
            fileSizes.add(file.size().toString());
        } }

    // Check that the current row number does not exceed the array size
    if (row < fileNames.size) {
        // Return the name to data source's "File" and "Source" properties
        result["File"] = fileNames[row];
        result["Size"] = fileSizes[row];
    }

    Sample Groovy script

    Click the image to enlarge it.

Click to test the data source. The retrieved values for the properties will be shown in the Data Log panel:

Sample Groovy run

Click the image to enlarge it.

See Also

Basic Concepts of Data-Driven Testing
Data Source Test Step
Data Source Loop Test Step
Excel Data Source
Data Source Types

Highlight search results