Applies to ReadyAPI 2.7, last modified on May 14, 2019

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

Note: You can only use this data source in the SoapUI data source test step. It is not available in ServiceV.

Configuration

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.

Specify the Groovy code that will generate the desired data. To get an output value, use the built-in result object. It is an associative array, keys of which match the specified properties. For example, the following codesnippet assigns a string to the foo property:

Groovy

result["foo"] = "bar"

You can use the following objects in your scripts:

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

  • results – 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.

Example

The following script returns a list of files in the home directory.

Note: Script returns values to the File property. Make sure you create it before you run the script.

Groovy

// Import required class
import groovy.io.FileType

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

// Get the home user directory
def directoryName = System.getProperty("user.home");

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

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

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

Here is a sample of the script run:

Sample Groovy run

Click the image to enlarge it.

See Also

DataSource Test Step
Excel Data Source
Data Source Types

Highlight search results