Introduction to Groovy Scripting in SoapUI

Marcin Łącki

SoapUI is a tool for web service testing. With SoapUI you are able to mock, inspect and develop code. Groovy is a programming language which is compatible with Java syntax. A perfect pair?

Introduction to Groovy Scripting in SoapUI

Overview

SoapUI is a tool for web service testing. With SoapUI you are able to mock, inspect and develop code. 
Groovy is a programming language which is compatible with Java syntax. A perfect pair?

Prerequisites

Installed SoapUI I created a simple project which you can see below (you can use a wsdl example from the Internet) Test suite is generated and Groovy Script added

Hello Jlabs

Traditional Hello World. In Groovy Script window line add:

log.info "Hello Jlabs"

Press Play button and say hello:

Properties

By Groovy you can get and set properties for objects. We can use two methods getPropertyValue and setPropertyValue. Below you can find a few examples:

Get a test case property:

def testCaseProperty = testRunner.testCase.getPropertyValue("PropertyName")

Get a test suite property:

def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "PropertyName" )

Get a project property:

def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "PropertyName" )

Set a test case property:

def projectProperty = testRunner.testCase.setPropertyValue( "PropertyName", Value )

Set a test suite property:

testRunner.testCase.testSuite.setPropertyValue( "PropertyName", Value )

Set a project property:

testRunner.testCase.testSuite.project.setPropertyValue( "PropertyName", Value ) 

Check system username

Now something cooler than a simple hello. We will check the system’s username:

def name = context.expand('${#System#user.home}')
log.info ("System user name is: " + name);

I`m Marcin, and you?

Groovy and Java together?

Yes, In Groovy you can add java packages. Let’s try to divide something:

import java.io.*;

int x = 49 ;
int y = 7 ;
int z = x/y ;
log.info ("Result: "+z) ;

Assertions in Groovy

Positive Assert:

def name =  "Marcin"  
assert name ==  "Marcin"

Negative Assert:

def name =  "Marcin"  
assert name ==  "Maciek"

When an error occurs:

You can define your own message for a negative assertion:

def name =  "Marcin"  
assert name ==  "Maciek":"Maciek and Marcin are not the same person"

and another error occurs:

Summary

Groovy is a scripting language that includes Java libraries.
Groovy increases the possibilities for functional, load and regression testing in SoapUI. You can maintain SOAP and REST tests better and easier. You can generate reports and run SoapUI tests on Jenkins.

Links

http://groovy-lang.org/https://www.soapui.org/

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami