Functional Web Services Testing Made Easy with SoapUI - Part 2
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")
- Login or register to post comments
- 119102 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
John Baker replied on Wed, 2008/11/19 - 3:20am
Meera Subbarao replied on Wed, 2008/11/19 - 8:05am
Ehsan Sadeghi 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
shanthi bala replied on Fri, 2009/02/27 - 2:25am
Meera Subbarao replied on Tue, 2009/03/03 - 11:18am
in response to: shanbala
Suresh Reddy 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.
Nelson Nazareth replied on Tue, 2009/06/16 - 6:47pm
syed ali 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.
syed ali 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...
kiran kmk replied on Mon, 2009/09/14 - 4:16am
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
Geza Szent-andrassy 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!
john green green replied on Mon, 2009/10/26 - 3:26am
Naveen Kumar replied on Wed, 2009/11/25 - 7:31am
Rambabu Nadakudati replied on Fri, 2010/03/05 - 12:37pm
Rambabu Nadakudati replied on Tue, 2010/03/09 - 3:32pm
Nikhil Gupta replied on Fri, 2010/07/23 - 6:09am
Test Dude replied on Wed, 2010/10/20 - 3:17pm
Durga Nuvvula replied on Tue, 2010/12/07 - 5:49pm
My objective is to get the response values out of the Response that is obtained from the SOAP UI in the assertion script.
I have tried to parse the response the way you suggested in the article and its returning null.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def responseHolder = groovyUtils.getXmlHolder("get-xyz-response Request 1#Response" )
when trying to get the
XPath
def appearanceColorResponse = responseHolder.getNodeValue("//appeareance-color")
I am getting the appearanceColorResponse as null.
Also I tried the other way registering the namespaces before doing the xpath.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
declare namespace ns4='http://www.xyz.com/schemas/xyz/abc';
declare namespace ns1='http://www.xyz.com/schemas/xyz/def';
declare namespace ns2='http://www.xyz.com/schemas/xyz/klm';
declare namespace ns3='http://www.xyz.com/schemas/xyz/pqr';
def appearanceColorResponse = responseHolder.getNodeValue("//ns2:message[1]/ns2:message-body[1]/ns1:person-detail[1]/ns3:person[1]/ns4:entity[1]/ns4:appeareance-color[1]);
I am getting the appearanceColorResponse as null.
Ram Kumar replied on Tue, 2011/04/19 - 9:35am
Hi meera,
How do i do data driven testing using excel sheets in soap ui along with groovy.. i mean reading data through an excel sheet. and i would like to capture the result from soap response and to write in to the specific cell in the out put excel document.
Please give me the details..I need to write some regression suites ..
Thanks
-Ram
Mahadeva Prasad replied on Wed, 2011/06/15 - 6:01am
I am new to SOAP UI.I wrote my application in using cakephp framework.I want to conduct web testing using a SOAP UI,like how to click a links and how to click a submit button once all input fields are entered.And also i want information about WSDL files.
Sandeep Manganellore replied on Fri, 2011/10/14 - 10:31am
in response to: meera
Vivek Kumar replied on Fri, 2011/12/30 - 10:39am