Use this test step to execute Groovy or JavaScript code from your tests.
Note
The default scripting language is Groovy (ver. 2.4.17). To use JavaScript, use the project setting Script Language.
Scripts allow you to implement complex behavior of your test. For example:
Modifying to test step assertions during the test run.
Modifying the underlying test case (for example dynamic generation of test steps from a database) for advanced data-driven scenarios.
Complex validation of messages that is not possible with default assertion mechanisms.
Integration with external systems for reading or writing data.
Triggering of external actions or processes, for example, sending emails or starting other programs.
Interacting with the user (dialogs, prompts, and so on) for getting the input or controlling execution.
and a lot more.
To extend your scripts, you can also use the Script Library.
You can modify test step settings in its editor:
![]() |
In the editor, you can enter the script code, run it and analyze the output to check if it works correctly. If the font of the script editor is not comfortable for you, change it by using Ctrl + mouse wheel.
In the script code, you can use the following objects:
Object | Description |
---|---|
| Provides a scripting interface to the test case run. |
| Provides a scripting interface to the Script log (to view this log, click Logs in the bottom left corner of the ReadyAPI window and switch to the Script log tab). You use this for debugging and logging purposes: to post and view data in the log. |
| Provides a scripting interface to the test runner object that is executing the current test case and test step. It exposes methods related to test execution and the underlying object model (via the testCase property). |
See the common usage scenarios for testRunner
below.
You can view these objects’ methods and properties in the Code Completion window. To invoke it, in the editor, type the object name and a period (for instance, testRunner.
) and press Ctrl+Space.
Besides the test step editor, you can adjust the test step’s behavior by using its properties in the GroovyScript Properties and Custom Groovy Script Test Step Properties panels in the Navigator.
GroovyScript Properties
Name | Description |
---|---|
Name | The test step’s name. |
Description | Text describing the test step. |
Custom Groovy Script Test Step Properties
Values on the Custom Groovy Script Test Step Properties tab are available to other test steps in your project. For instance, you can verify these property values with the Assertion test step, or check them and change the execution flow with the Conditional GoTo test step.
You can modify, add, remove and change custom properties load values of custom properties from a file, or save them to a file. To learn more, see About Properties.
This tab contains the following properties that provide access to the test step data:
Name | Description |
---|---|
result | A read-only property. Returns the result of the script execution. NoteThe execution result is the value passed in the ...
return myVariable;
...
|
script | Returns the entire script code as a string. |
The Test Step toolbar contains commands that allow you to modify a test step or the appearance of the test step editor.
![]() |
Debugging scripts means halting the test execution on a certain line and then running through the test step by step, stopping on operations or script lines. Every time the debugger stops, you can check the values of variables. However, merely stopping before certain operations or lines is often a great help in tracking what your test is doing.
During the debugging, ReadyAPI displays information about all available variables in the Debug Info tab:
![]() |
To use Groovy script debugging you need a ReadyAPI Test license.
You can debug scripts only in the Groovy Script test step. Debugging of event handler scripts, setup, teardown and report scripts, Script assertions, data source and data sink scripts, and virtual service scripts is not supported.
You can debug only Groovy scripts. Debugging of JavaScript scripts is not supported.
You can only debug one script at a time. Debugging several scripts or test cases in parallel is not supported.
To save operating system resources, the debugger is disabled by default. If you click while the debugger is disabled, you will be prompted to restart ReadyAPI to enable it.
To enable the debugger when you run ReadyAPI via shell script (.bat or .sh file), use the groovy
parameter:
readyapi.bat groovy
If you want to make the debugger enabled by default, do the following:
To start debugging, click . You can run debugging in two ways:
From a Groovy Script test step. This executes only the script you run it from.
From a test case, test suite or a project. This executes the entire test in debug mode. The test will stop on every breakpoint it encounters in Groovy Script test steps.
A breakpoint is a location in your script where you want the script or test to pause during the execution. Once the execution is paused, ReadyAPI will open the line with the breakpoint so that you can check the state of the test, its output and its variables.
To set a breakpoint:
Click the left margin of a source code line.
– or –
Right-click the source code line and select Toggle Line Breakpoint.
When the test run reaches a breakpoint, it stops. ReadyAPI opens the Groovy script editor and enables the following commands:
Step over – The execution proceeds one statement or operation at a time within the script where the execution point is now located. If the next statement calls another script, the whole call is executed as a single step.
Step into – The execution proceeds one statement or operation at a time regardless of where the execution point is now located. If during the execution you enter another function or script, the debugger will open it and continue the step-by-step execution.
Resume debugging – The execution proceeds until the next breakpoint is reached and stops on it.
Continue to end – The execution proceeds until the end of current script ignoring any breakpoints. If there are other scripts with breakpoints in the test run, the test will still stop on them.
When you are using a script editor, you have the Log Output panel for the test runs. It contains different script messages. For example, you can post any message you want by using the log.info
method:
In the Groovy script test step, you can perform assertions like you do in the Groovy Script assertion.
Below, you can find information on common tasks that you can perform with the Groovy Script test step.
To obtain the object which refers to the containing test case, use the following code snippet:
By using the testCase
object, you can access and manipulate test items of the project.
There are two ways to fail the test run from your scripts:
To fail only the Groovy Script test step, throw an exception in your script:
throw new Exception("Result not as expected!")
If the Abort test if an error occurs option is enabled in Test Case Options, the test will stop. Otherwise, it will continue.
To fail the entire test run, use the
testRunner.fail
method. This marks the test failed and stops the test run regardless of the Abort test if an error occurs option.testRunner.fail("Result not as expected!")
The testRunner
scripting object has two methods to stop the current test run:
cancel(String reason)
– Stops the test run and marks it as Canceled. The string argument specifies the reason.fail(String reason)
– Stops the test run and marks it as Fail. The string argument specifies the reason.
You can run any test step in the current test case. To do this, use the runTestStepByName
method of the testRunner
object. This method runs the specified test step and returns the result. For example, the following code snippet runs ten random requests before executing the remaining script:
Important
This method does not work for the Data Sink and FTP test steps. To run them from a Groovy script, use the following approach:
By using the gotoStepByName
method of the testRunner
object, you can command ReadyAPI to jump the test execution to the specified test step after the script has finished. For example the following script randomly selects the next test step:
To create an assertion:
Obtain a test step by using the
getTestStepByName
method of thetestCase
object.Use the
addAssertion
method to create an assertion.Specify the assertion name as a string.
ReadyAPI will create a new assertion with the specified name and default settings.
Note
If you already have an assertion with the same name, you will be prompted to specify the unique assertion name. The test will not continue until you close the dialog.
For example, the following code snippet shows how to create a Valid HTTP Status Codes assertion for the Test Request test step:
You can modify the created assertion by using assertion-specific methods.
For example, to change the code checked by Valid HTTP Status Codes and Invalid HTTP Status Codes by using the setCodes
method of the Assertion
object.
You can get this object when creating it in the following way:
If you have already created an assertion, you can access it in the following way:
To remove an assertion, use the removeAssertion
method.
You need to specify the assertion object to remove.
Here is the sample code that will remove the assertion created in the example above.
To get a property value:
Obtain the containing object.
Use the
getPropertyValue()
method to get a property object.
For example, the following code snippet gets a test suite property:
To write a value to a property, use the setPropertyValue()
method. For example, the following code snippet, specifies the Username parameter of the HTTP Request test step: