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 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 :
Do provide your inputs/comments/question on the same blog and share if you have something interesting. Thanks!
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 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!