Go Groovy with soapUI – nifty tool for Webservice testing!

Posted: May 19, 2011 in context, groovy, soapUI, testrunner
Tags: , , , ,

Here i go with first groovy post about using Groovy script in soapUI. groovy comes very handy when you want to put some script assertion or performing certain task before/after each teststep execution etc.

First couple of things to remember before we proceed with actual code lines.

# Groovy script can be written in Assertion, Test Step, TestCase/TestSuite level as TearDown/Setup script.
# To invoke the groovy script at each different level, script editor provides us with the list of variables on top right corner of script editor. So it is possible that the code you have written in groovy test step or as teardown script would not work properly for teststep script assertion.

In most of the cases we use either “context” or “testrunner” variable to invoke or create a new object of any class or to call required method.

I frequently use these 2 standard lines of groovy code, to acheive the required script objective, are :

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder (java.lang.String xmlPropertyOrString) // where this xmlProperty can have either ‘teststepname#response’ ‘teststepname#request’ or any string value.

After having the object created refer the groovyUtils APIDocs (http://www.soapui.org/apidocs/com/eviware/soapui/support/GroovyUtils.html) to find out all the relevant methods which can be called with particular input parameter.

Now below are few code lines for quickly performing certain task :

@Author : Pradeep Bishnoi
@Description : To count the number of nodes under any node in response using xPath & groovy script.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder (“myTestStepName#Response”)
holder.namespaces[“namespacename1”] = “http://mywebservice.com/soapui/testing”
log.info(“Number of nodes :” + holder.getNodeValue(“count(//namespacename1:GetSampleMethodResponse/namespacename1:StudentDetails/namespacename1:Address)”))

// Address is comprised of house number, street name, city, country – so result = 4

@Description : To store the response into a simple text file, append this code to the above.

new File(‘D:/response_myTestStepName.txt’).write(holder.prettyXml)

Do provide your inputs/comments/question on the same blog and share if you have something interesting. Thanks!

  1. Sandeep says:

    Hi Pradeep,

    How to log result of complete testcase. I can decide based on result of individual teststeps but it would be great if I get result by testcase.
    Some thing like this
    import com.eviware.soapui.model.testsuite.Assertable

    def project = context.testCase.testSuite.project
    def testSuite = project.getTestSuiteAt(0)
    def testCase = testSuite.getTestCaseAt(0)

    The result can be UNKNOWN, PASSED or FAILED.

    Thanks in advance,

    • Hi Sandeep,

      If i am not wrong soapUI doesn’t provide a direct method to fetch the status of a testcase.
      It actually derives the status of the testcase based on the status of executed teststeps.
      So you can use the same approach and iterate through all the teststeps inside a testcase. If a failure of teststep found then set the failure of the testcase else it is passed. Unkonwn would be only status when all the teststep under exuection are having unkown status.

      Hope this will help.
      {Pradeep Bishnoi}

  2. Raj says:

    Hi Pradeep,

    I am very new to groovy scripting and soap Ui.

    I have a requirement to validate 2 version of services by comparing their responses.Its migration testing.

    Do you have any scripts for the same ? I am using soapUi free version.

    the services are soap/http .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s