Thursday, May 19, 2011

Go Groovy with soapUI - nifty tool for Webservice testing!

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 Assertaion, 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 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 ( 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 context )
def holder = groovyUtils.getXmlHolder ("myTestStepName#Response")
holder.namespaces["namespacename1"] = """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. Please learn to write English properly before you commit such rubbish next time.

  2. At least he is trying to explain what he gained through his experience. You fool has not at least done anything in your life. The English in this post is not too bad. As a techie I concentrate more on content rather than grammar.

  3. Thanks Pradeep, it helped me a lot! :P

  4. That's true...we want knowledge not grammer

  5. Hi..
    I'm new to groovy. one thing which is bothering me is "context" in the line
    "def groovyUtils = new context )"
    what do you mean by "context"

  6. I needed to thank you for this fantastic read!! I absolutely enjoyed every little bit of it.

    I've got you saved as a favorite to look at new stuff you postÖ

    My page: tmz celebrity news and gossip

  7. That is a very good tip especially to those
    fresh to the blogosphere. Short but very precise informationÖ
    Many thanks for sharing this one. A must read article!

    Here is my page :: Angry Birds Online

  8. Worked perfect to get count of nodes

  9. in the line def holder = groovyUtils.getXmlHolder ("myTestStepName#Response"), if I want to refer to a test step belonging to a different test case, how do I do that?
    def holder = groovyUtils.getXmlHolder ("testCaseName#myTestStepName#Response") does not help.

  10. After launching Testrunner, test results are exported using the option "Exports all results(not only errors)".
    But, the test results file name is ending with "UNKNOWN". Because of this, the test results are not getting
    exported completely.

    Can some one let me know the solution for this. Thanks in advance.