6.Introduction to Groovy Script in SOAPUI

WHAT IS GROOVY?

  • Groovy is a Scripting Language which internally includes all the Java Libraries, therefore all Java related Keywords and Functions can be used in the groovy script directly.
  • Groovy is a dynamic programming language in which most of the program execution processes are done at runtime instead of compile time.
  • Groovy can be categorized into the same family of scripting languages such as Ruby, Perl, or JavaScript.
  • Groovy uses Java-like bracket syntax and most Java code is syntactically valid in Groovy. Groovy scripts run on JVM similar to Java programs, hence we do not need to install and configure additional libraries.
  • The Java Libraries come with SOAPUI and are integrated during the SOAPUI Pro Installation itself.
  • Groovy is a loosely-typed language, which means there is no need to define the data types for the variables and for the return types of the methods.
  • Groovy Script Test Step is included for custom Automation Test Script creation in SOAPUI / pro. It can be used for Functional/Load/Regression.

Let's go through some basic principles of Groovy with examples. In order to try out the simple Groovy examples that we are going to try out, you can use the following two approaches:

  • Download the latest version of Groovy binary distribution from http://groovy.codehaus.org/Download (at the time of writing, the latest stable version was Groovy 1.8). 
  • Install Groovy on your machine as per the instructions given in the official Groovy installation guide (http://groovy.codehaus.org/Installing+Groovy). Then, you can use the interactive Groovy shell to write and run the example Groovy scripts.
  • Use soapUI Groovy Script TestStep to test the sample scripts.

We will use the second approach as it minimizes the setup time and we can quickly try out some basic principles of the Groovy scripting language.

We will use a new workspace and a project in soapUI for Groovy examples as these are not a part of our sample hotel reservation project. 
1. Go to File > New workspace and add a new workspace in soapUI. Name it GroovyExamplesWorkspace.
2. Once the new workspace is created, right-click on it and add a new soapUI project. Name the project GroovyExamplesProject. 
3. We do not add an initial WSDL for the project as we are not going to test any web services using this project. 
4. Now, add a new TestSuite and a TestCase in the project. 
5. Add a new Groovy Script Test Step by right-clicking on the TestCase and selecting Add Step, then selecting Groovy Script out of the steps available in the list. 
6. Finally, we will get a Groovy Script editor, as shown in the following screenshot, where we can try out the example scripts.

What is Groovy?

Hello World with Groovy:
Let's begin with usual Hello World script. Write the following script in the Groovy Script editor.  

// print "Hello World" string in console
log.info "Hello World" 

OUTPUTHello World

Run the script by clicking on Green Arrow Icon which is at the upper left corner of the script editor.We will observe output in the SOAPUI startup console.

Variable and object declaration in Groovy:

As with any programming language, variables or objects must be declared before they are referenced by somewhere else. The variables can be declared with the keyword def, as shown in the following script:

def  name = "SOAPUI" //Declare variable name and assign value SOAPUI 

log.info name

OUTPUT: SOAPUI

To read the value of a variable, you can just prefix the variable name with $ as in Case 1 or append it as in Java (Case 2).

Case 1:
def name = "soapui"
log.info "This is $name"

OUTPUT: This is soapui

Case 2:
def name = "soapui"
log.info "This is\t" + name

OUTPUTThis is soapui

Groovy has support for two collection data types:

Lists: 
These are used to store ordered collections of data. A list can be declared as follows:

myList = [0,56,-98,345,78976]
log.info myList

OUTPUT[0,56,-98,345,78976]

The above statement declares a list object, which holds integer values. We can access a value stored in list with myList[n], where n is the index of list.

myList = [0,56,-98,345,78976]
log.info myList[3]

OUTPUT: 345

Maps: 
These store different types of data in key-value pairs. For example, consider the following script:

myMap = ["key1":"soapui", "key2":100, "key3":30.05] 
log.info myMap["key2"] // access the value assigned to "key2"

log.info myMap.key3  // another of accessing key value

OUTPUT: 100
                  30.05

Control structures in Groovy
The syntax of control structures such as "if-else", "for", and "while" are very similar to what we have in Java. Look at the following code snippet:






No comments:

Post a Comment