Click here to Skip to main content
14,367,789 members

Automating SoapUI using Groovy – A Walk Through

Rate this:
5.00 (2 votes)
Please Sign up or sign in to vote.
5.00 (2 votes)
18 Sep 2014CPOL
How to automate SoapUI using Groovy

Introduction

I had a requirement to capture the response from at least 100 calls to a web service. This was to enable a manual check of the response by a non IT staff member who needed the responses in the form of an Excel file.

I was able to complete this using the free version of SoapUI 4.6.4 utilizing some groovy scripting. You may also like to download Fiddler2 to check things are working.

The scripting was largely a cut and paste job using code provided by M. McDonald at http://forum.loadui.org/viewtopic.php?f=5&t=16354#p38935.

If you are not familiar with SoapUI Groovy and Java, it can be very confusing but don't despair!

SoapUI Set up

Download a free version of SoapUI and install it.

When you first run it, it looks something like:

Image 1

I have created a workspace to store my projects in called Automation. At this point, we go to ‘File’-> ‘New Soap Project’ and fill in the Project Name and the location of the .wsdl file for the web service.

Image 2

Ensure you tick 'Create Requests' and 'Create TestSuite', then click 'OK'.

SoapUI will check the web service and return all the operations/methods you can call on that service. In my case, there is only one operation called SubmitRequestResponse, but I have selected 'Single TestCase with One Request for Each Operation' anyway.

Image 3

Click 'OK' and you will be prompted to name the Test.

 I called mine 'Multiple Calls TestSuit'.

Image 4

Now when SoapUI has setup the project, expand the structure to look something like this:

Image 5

If you double click on Request 1, you get a basic request form.

Image 6

We need to ensure that the calls will get a response from the web service before we proceed so we add a soap request. In my case, the web service requires a customer number followed by a site location tag followed by one or more SalesOrders. So I replace the <part>?</part> tags in the body with my soap request.

Image 7

Click the green arrow (top left) and you will get an XML response.

Image 8

If you have a response, you are where you need to be.

Setting up the Test Files, Test Steps and Test Scripts

At this point, we know that SoapUI can send requests to our web service and return a response.

Now we need to set up our Test Steps and Test Scripts.

The Test files

Create a directory in your file system and add two subdirectories.

Image 9

In requests, we need to add the request files.

The files must be of the same format that you added in Request 1, i.e.:

<ns0:CheckGuaranteeRequest xmlns:ns0="http://www.####.com/namespaces/portal/checkguarantee/in/2014.03">
<CustomerNumber>102277</CustomerNumber>
<SiteLocation>COAST</SiteLocation>
<SalesOrders>
<Id>1000495222</Id>
<Id>1000498217</Id>
</SalesOrders>
</ns0:CheckGuaranteeRequest>

They must also have a file extension of .xml.

The Test Steps

Examine the structure of the project and ensure that the Test Steps (1) section is expanded. Double clicking the SubmitRequestResponse shows a basic soap header. We can ignore this for now.

Image 10

Right click the Test Steps header and select 'Add Step' and choose 'Groovy Script'.

Image 11

Rename it to Step1.

Image 12

Click 'OK' and the Groovy editor will appear.

Image 13

It is here that we add our first script.

The Scripts

The test scripts were supplied by M. McDonald (see above).

Script 1

The first script is:

def fileList = []
new File("C:\\requests").eachFile { f ->
if (f.isFile() && f.name.endsWith('.xml')) {
   def filename = f.name[0..-5]
     fileList.add(filename)
}
}
if (fileList.size() < 1) {
   testRunner.fail("No request files")
}
context.put('fileList', fileList)

The first step defines an array.

Next is a section that creates a file from our test files directory and adds it to the fileList array. It also trims off some characters, checks the size of the array to ensure some files exist, then adds the array to the 'context object' so the array is available to the other test steps we will create.

We need to change the second line to reflect the paths to our files and add a quick check.

In my case, the script becomes:

def fileList = []

new File("C:\\GroovyTest\\requests").eachFile
{ f ->
if (f.isFile()&& f.name.endsWith('.xml'))
{
 def filename = f.name[0..-1]
 fileList.add(filename)
 log.info filename
}
}

if (fileList.size() <1)
{
testRunner.fail("No request files found")
}
context.put('fileList',fileList)

The line 'log.info filename' causes the names of the files in the request directory to be printed to the script log.

Click the script log button at the bottom of the Step1 window, then click the green arrow top left to run the script. You should see the list of files printed out.

Image 14

Ok so far. We have successfully read the names of the files in our request directory and displayed them. It might be good at this point to comment out the line which prints out the filenames from the array.

Image 15

Script 2

We already have step 2, it's called SubmitRequestResponse. Rename it to 'Step2'.

Remove the ' <part>?</part>' nodes in the body of the Soap request and add the following:

${=new File("C:\\GroovyTest\\requests\\" + (context.get('fileList')).last()).text}

NOTICE I HAVE CHANGED THE PATH TO THE REQUEST DIRECTORY !!!

Image 16

We now need to drag Step2 below Step1 in the project structure, so it looks like:

Image 17

We can now test both scripts to see if they are working.

Time to Set Up Fiddler

Just Install Fiddler Web Debugger.

Fiddler allows you to monitor what you are sending to and what you are receiving from the web service.

When you open Fiddler, it needs a little configuration in order for you to view the traffic to and from your web service. In the Taskbar, go to 'Tools' -> Fiddler options and open the connections tab. Fiddler normally listens on port 8888, so change this to 8080 as below:

Image 18

That’s it for Fiddler.

Go back to SoapUI and look at 'File'-> 'Preferences'-> 'Proxy Settings'. My settings are set to 'Automatic'. I had some problems with this as I was initially using SSL.

My 'HTTP Settings' are set to the default options. A good site to set up Fiddler with SoapUI is:

By Dinesh K Mandal, 8 Sep 2011.

To test if everything is working, go back to SoapUI and double click your test case. In my case, it's called 'BasicHttpBinding_ITwoWayAsync TestSuite'.

The test case window will appear:

Image 19

You might be lucky here and see a session appear in Fiddler like this:

Image 20

This is good. If you didn’t see a session appearing, save everything in SoapUI and restart it, then try again.

Go back to SoapUI.

In the Test case window, click the green arrow to run the test steps:

Image 21

Take a look at Fiddler and you will see a session has been captured.

If you click on the session and view the results as XML, you will see the request going to the web service in the top panel and the response from the web service in the bottom panel.

Image 22

We have successfully called our web service and had a response returned. We now need a step to save the response to a file.

Script 3

Back to SoapUI and right click on 'Test Steps (2) and select 'Add Step' ->'Groovy Script'. Rename it to 'Step3'.

Add the following script to the Groovy window, changing the directories to match your directory structure.

def fileList = context.get('fileList')

def fileName = fileList.pop()
def newname = fileName[0..-5]

def response = context.expand( '${Step2#Response}' )
def f = new File("C:\\GroovyTest\\responses\\${fileName}_Response.xml")
f.write(response, "UTF-8")

if(fileList.size() >0)
{
testRunner.gotoStepByName("Step2")
}

This piece of scripting gets the array from the context (see Step1), takes a file from the array, gets its name and removes the .xml from name. It then gets the response that was returned from Step2 and stores it as 'response'.

It then creates a new file in the response directory and names it as the old file but also adding ' _Response.xml' to the file name.

It then writes the response from the web service into the file.

There is a final section that checks the size of the array and if it is greater than 0, there is still a test to run so the gotoStepByName function says goto 'Step2'.

Double click the 'Test Steps (3)’ test case and run the Test Suite.

Image 23

Check Fiddler and take a look in the Response directory.

Image 24

This was to save me grief in the future but hey the real information was from M. McDonald and Dinesh K. Mandal.

Hope it helps someone else.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sore_ron
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionHelp Required in Step 3 Groovy Script ( SOAPUI Freeversion 5.4.0) Pin
Member 1461834010-Oct-19 1:21
memberMember 1461834010-Oct-19 1:21 
Questionthis worked like a charm, thanks Pin
Member 1418057213-Mar-19 1:10
memberMember 1418057213-Mar-19 1:10 
QuestionExtract response values Pin
Member 1413605831-Jan-19 4:13
memberMember 1413605831-Jan-19 4:13 
QuestionAll the XML Requests are parallel ?? Pin
shravan9930-Jun-18 13:38
membershravan9930-Jun-18 13:38 
QuestionSending JSON request in SoapUI tool using Groovy Script Pin
Member 1332115721-Jul-17 1:15
memberMember 1332115721-Jul-17 1:15 
QuestionExecute Multiple requests Pin
Member 132478258-Jun-17 0:47
memberMember 132478258-Jun-17 0:47 
AnswerRe: Execute Multiple requests Pin
Member 1405559615-Nov-18 1:26
memberMember 1405559615-Nov-18 1:26 
QuestionGetting java.lang.NullPointerException:Cannot invoke method pop() on null object Pin
Member 1320885818-May-17 5:53
memberMember 1320885818-May-17 5:53 
QuestionGetting error. Pin
Member 1209018826-Oct-15 22:30
memberMember 1209018826-Oct-15 22:30 
AnswerRe: Getting error. Pin
Ashwij Ugrankar1-Nov-15 23:02
memberAshwij Ugrankar1-Nov-15 23:02 
GeneralRe: Getting error. Pin
Member 1239294615-Mar-16 0:09
memberMember 1239294615-Mar-16 0:09 
GeneralRe: Getting error. Pin
Member 1398884518-Sep-18 5:38
memberMember 1398884518-Sep-18 5:38 
QuestionHelp Required Pin
Member 1191128415-Aug-15 5:29
memberMember 1191128415-Aug-15 5:29 
AnswerRe: Help Required Pin
Ashwij Ugrankar1-Nov-15 23:06
memberAshwij Ugrankar1-Nov-15 23:06 
Questionarticle on soap ui automation Pin
Member 1164204225-Apr-15 14:33
memberMember 1164204225-Apr-15 14:33 
AnswerRe: article on soap ui automation Pin
Ashwij Ugrankar1-Nov-15 23:10
memberAshwij Ugrankar1-Nov-15 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 18 Sep 2014

Tagged as

Stats

109.2K views
5 bookmarked