Functional Web Services Testing Made Easy with SoapUI - Part 2

Part 1 of this series helped provide the background needed to begin exploring web services testing. We learned the basics of SoapUI and how easy it was to write functional tests without writing a single line of code. We also saw how to add assertions to these tests. What we will examine now is how to use Groovy within SoapUI for test setup, test teardown, response validation, and much, much more.

Before we begin, a quick Groovy primer. We start with the fact that Groovy is Java and Java is Groovy. If you have written Java code, you have written Groovy code. Our tasks within this article will be to do 3 things: format a date, read and write to a properties file, and parse XML. Let’s look at the Groovy code for each of these in detail.

1. Format a date
The standard Java class for this task is SimpleDateFormat. This is a concrete subclass of DateFormat formats and parses dates and times using a string pattern, in our case, "yyyy-MM-dd". Let’s start by writing this in Java; later we will see how easily we can groovify this to use within SoapUI. Here is the Java code to get the date in the format “yyyy-MM-dd”.

java.util.Date today = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
String todayStr = sdf.format(today);

We could leave the Groovy code exactly as it is above, but let’s make it groovier, removing the bells and whistles necessary in Java. Our code finally looks like this:

today = new Date()
sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(today)

Not a lot of change, but considerably faster to type. The similarities to Java are just as we have said.

2. Reading and writing to a properties file

The java.util.Properties object does the work of reading and writing to a properties file in Java. This time we’ll go directly to the Groovy code:

sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
todayStr = sdf.format(new Date())
props = new java.util.Properties ()
file = new File("C:/web-services-test/testprops.txt")
if(!file.exists())
{
file.createNewFile()
props.today = todayStr
props.zipCode = "20904"
fos = new java.io.FileOutputStream ( file )
props.store(fos, "Writing the zipcode and today's date")
}fis = new FileInputStream (file )
props.load (fis)
today = props.getProperty ( "today" )
zipCode = props.getProperty ( "zipCode" ) 

Simple, right? Similarly, if you need to read from a database, you can do so in a few simple lines of Groovy code.

3.Parse XML

If you have been writing enterprise Java applications you have certainly needed to use XML, and you can testify that XML and Java is not easy. Parsing XML in Java requires a lot of boilerplate code and is really tedious.

You may remember from part 1 that I said I was lazy. Fortunately, Groovy comes to the rescue. We can parse XML in just 5 lines of code. Yes, that’s what I said, just 5 lines. Let’s see how to parse the response from the LatLonListZipCode web service from Part 1. The XML looks like this:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:LatLonListZipCodeResponse xmlns:ns1="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<listLatLonOut xsi:type="xsd:string"><?xml version='1.0' ?>
<dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/mdl/survey/pgb_survey/dev/DWMLgen/schema/DWML.xsd">
<latLonList>39.0138,-77.0242</latLonList>
</dwml></listLatLonOut>
</ns1:LatLonListZipCodeResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The code to parse this in Groovy is so easy:

groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
holder = groovyUtils.getXmlHolder("LatLonListZipCode - Request 1#Response")
listLatLonOut = holder.getNodeValue( "//listLatLonOut" )
latlonNode = groovyUtils.getXmlHolder(listLatLonOut)
latlon = latlonNode.getNodeValue("//latLonList")

Subtitle: 
SoapUI's so Groovy
Article Type: 
How-to
Article Resources: 
0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

jbaker replied on Wed, 2008/11/19 - 3:20am

Thanks for the great article, Meera! I had figured out the basics of soapUI for testing simple requests, but I needed a way to propagate a larger number of test cases with various combinations of optional reqeust elements. Following you example I now have the tools I need to get moving. I also feel motivated to learn more about Groovy. Thanks again!

Meera Subbarao replied on Wed, 2008/11/19 - 8:05am

I am glad this article helped. Yes, there are numerous ways you can use Groovy, not just with SoapUI, and I am glad I learnt this.

esad48 replied on Fri, 2009/01/16 - 6:26am

hi,

one question, is it possible to pass the responce from one test case to another test case. What i'm trying to do is to get a session id and then use that session id to run a load test.

Thx.

Meera Subbarao replied on Fri, 2009/01/16 - 10:58am in response to: esad48

Yes, it is possible. See part 2 which shows exactly how to do this.

shanbala replied on Fri, 2009/02/27 - 2:25am

Hi Meera Can I write tests within SOAP UI and invoke it in a seperate Java program ? Also I need to parse the result in this Java code. Thanks Shanthi

Meera Subbarao replied on Tue, 2009/03/03 - 11:18am in response to: shanbala

Yes, you can do that. You can easily do that.

sureshreddy04 replied on Wed, 2009/03/18 - 10:24pm

After seeing into ur article I whould like to know how can i obtain session ID to logout automatically when I login and logout of the webapplicaiton.

mevric75 replied on Tue, 2009/06/16 - 6:47pm

Hi Meera, Thanx for the wonderful tutorial. I am using soapUI as a mock webservice server. I have tried the various options currently available and they are good. I have one need. Using groovy I want to evaluate the incoming request and based on some condition would like to return response from a list of available responses. Please remember this needs to done from the script itself. Can you please provide an approach?

jafar_bt replied on Thu, 2009/08/06 - 4:25am

Hi Meera,

How could I change the project properties, test suite properties or testcase properties values using Groovy script test step. I wasn't able to understand the syntax.

def sessionID = context.expand( '${#Project#SessionID}' )
def testSuiteProperty = context.expand( '${#TestSuite#TestSuiteProperty}' )
def test1 = context.expand('${#TestCase#test}' )

I understood the above lines collect the values of the given properties. Please help me to set the value for  SessionID, TestSuiteProperty, test parameters. Thanks in advance.

 

 

jafar_bt replied on Fri, 2009/08/07 - 2:29am in response to: jafar_bt

I have found the info. The format is as follows:

testRunner.testCase.testSuite.project.setPropertyValue("SessionID","2002")
testRunner.testCase.testSuite.setPropertyValue("TestSuiteProperty","testSuiteProperty")
testRunner.testCase.setPropertyValue("test","testcase property")

However, I have another issue. If the assertion is faliled I am unable to continue to execute the further steps of testcase. Please help me regarding this concern...

kkairan replied on Mon, 2009/09/14 - 4:16am

Hi,

I am trying to get the Node Value but I get it as Null

IST 2009:INFO:null

This is the Script I am running groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) holder = groovyUtils.getXmlHolder("GetData - Request 1#Response") dwmlByDayOut = holder.getNodeValue( "//GetDataResponse" )

Below are the Request Iam sending and the response I get

${Properties#Addvalue}

This is my response

You entered: 500

sztgeza replied on Fri, 2009/10/09 - 3:13am

Hi all!

For the TestCase setup script to work, i think it needs to add the following lines before file.createNewFile(), Otherwise i experienced an IOException, which was thrown by file.createNewFile(), when the parent directory had not existed before.

...

if(!file.exists())
{   
    if (file.getParentFile() != null) {
        file.getParentFile().mkdirs()
    }
    file.createNewFile()

...

 Nice article, anyway!

 

sunrise1 replied on Mon, 2009/10/26 - 3:26am

I am glad this article helped. Yes, there are nike shoes russia numerous ways you can use Groovy, not just with SoapUI, and I am glad I learnt this.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.