SoapUI Groovy Script

Apache Groovy is an object-oriented, Java syntax compatible programming language based on the Java platform. It has many static and dynamic features similar to the python, Ruby, Pero and the Small talk language. It can be used as a programming and scripting language that enables java platforms to use the groovy source code compiled into Java Virtual Machine (JVM) Bytecode. Furthermore, it uses the Java library internally with Java-related keywords and functions directly in the Groovy script. It is a scripting language that contains all Java libraries. If we want to use Java keywords and functions directly in Groovy script, we can easily use it.

Uses of Groovy Script in SoapUI tool for API or Web service testing.

  1. The groovy script is used to generate and provide data into the groovy test request.
  2. It is used to verify the groovy response.
  3. It can be used to set the script property values at different levels, such as to request test step, test case and test suite levels.
  4. Using a groovy script, we can enable or disable the test suites and test cases during the requested groovy script.

Create and Test a Groovy Script in a SoapUI Tool

For creating the groovy script in a SoapUI, we have to follows the given steps, as shown.

Step 1: Select the CalculatorSoap TestSuite and then select the Test Case in which we are going to create the Groovy script. Right-click on the Test Steps, and then go to the Add Step to select the Groovy Script from the available service, as shown below.

SoapUI Groovy Script

Step 2: clicking the Groovy Script, it shows a pop dialog box to specify the groovy script's name for a new step, or we can leave the Groovy Script name as default and then click the OK button.

SoapUI Groovy Script

Step 3: As the OK button is clicked, it shows a Groovy Script editor where we can write the script and test it by clicking on the run button, as shown below.

SoapUI Groovy Script

Step 4: Now, we write some text in a Groovy script editor and then run it.

Let's write a message in a Groovy Script editor that displays the message in Log Output.

Step 5: For executing the Groovy Script in the SoapUI tool, click on the green color run button that shows the script output in the log.

SoapUI Groovy Script

Example: Write a program to return the number of methods used in the SoapUI Groovy script editor.

Create a new Groovy script by right-clicking on the old groovy script and then select the new groovy script, as shown below.

SoapUI Groovy Script

After selecting the Groovy Script service, it shows the groovy Script window, as shown below.

SoapUI Groovy Script

Now enter the Groovy Script statement, as shown below.

SoapUI Groovy Script

After clicking on the Run button, it shows all the methods used in the SoapUI Groovy Script language.

Example: Write a program to create a class in groovy.

To create a class in SoapUI, right-click on TestStep and choose Groovy Script to open the editor. Save the class as 'MyClass' and then write the following code, as shown below.

MyClass


SoapUI Groovy Script

Click on the run button to execute the groovy script.

Output:

SoapUI Groovy Script

Call One Groovy Class from Another Groovy Class

To create a class in SoapUI, right-click on TestStep and choose Groovy Script to open the editor. Save the class as MyClass and then write the following code, as shown below.

MyClass


SoapUI Groovy Script

Click on the run button to execute the Groovy script. If any error occurs during the running of the script, the color of the Groovy icon changes to red, and if there is no error, the color of the icon changes to green.

Similarly, right-click the Test Steps and then create the groovy script from the available resource via Add Step. Save the groovy file with named Call Groovy Script MyClass and then write the following code to call the MyClass, as shown below.

Call Groovy Script MyClass


SoapUI Groovy Script

Click on the run button to execute the groovy script.

Output:

SoapUI Groovy Script




Latest Courses