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!
Please learn to write English properly before you commit such rubbish next time.
ReplyDeleteAt 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.
ReplyDeleteThanks Pradeep, it helped me a lot! :P
ReplyDeleteThat's true...we want knowledge not grammer
ReplyDeleteHi..
ReplyDeleteI'm new to groovy. one thing which is bothering me is "context" in the line
"def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )"
what do you mean by "context"
grammar*
ReplyDeleteI needed to thank you for this fantastic read!! I absolutely enjoyed every little bit of it.
ReplyDeleteI've got you saved as a favorite to look at new stuff you postÖ
My page: tmz celebrity news and gossip
That is a very good tip especially to those
ReplyDeletefresh 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
Worked perfect to get count of nodes
ReplyDeletein 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?
ReplyDeletedef holder = groovyUtils.getXmlHolder ("testCaseName#myTestStepName#Response") does not help.
After launching Testrunner, test results are exported using the option "Exports all results(not only errors)".
ReplyDeleteBut, 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.
Regards,
Madhu