Archive for the ‘messageexchange’ Category

Couple of days back, i was exploring REST API and thought of writing some generic groovy code lines – to count the number of nodes in the response & iterate (and to assert values) through them (actually one of my friend asked me ;-)) So i started thinking on same lines & luckily found the solution only in 5 minutes. Amazing…isn’t it :-p

So before i share the lines of code, here i have couple of quick question for you all to think & then answer :

“What is REST API?”
“What is JSON?”
“When to use REST api & How i can identify if it is?”

Now many of you (i hope you all) might know the answer to these silly question. So here is a REST request to all such people to share their views/answers in the comment section. Few others may think :
“Why the hell in world, i should care whether it is REST api or some __ api?”
and for those people i would say “Good you dumb***! Now stop testing/developing, if you don’t care”.

Before you find the correct answer & share, let me copy paste my script code lines here (for those who don’t care about concept).

/*
@Author: Pradeep Bishnoi
@Description : To read the REST response and iterate node using Xpath
*/
testStepName = messageExchange.modelItem.testStep.name   //to get the Test Step Name
xmlHold = messageExchange.responseContentAsXml.toString()  //to store the response as Xml string
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context )
def holder = groovyUtils.getXmlHolder (xmlHold)
log.info holder.getXml()  // will display /Log the response in Xml
count  = holder.getNodeValue(“count(//Response/e)”)
count = count.toInteger() + 1  //converting count into Integer & adding 1 for looping purpose
xPath = “//Response/e[“
for (i in 1..<count)
{
xPath1 = xPath + i + “]/Name”  // Name value
xPath2 = xPath + i + “]/EmailAddresses/e”   //email Node value
log.info holder.getNodeValue(xPath1)
log.info holder.getNodeValue(xPath2)
i++
}

if you noticed in the above code lines.. i have used an XPath expression to navigate to the Xml node tree structure. Since the response received is in JSON format [as mentioned in post header], soapui automatically generates/performs the XML structure/translation  of the received response. And the xml response would have Root node as “Response” followed by child nodes named as “e”. To access each individual child node and their child you have to use an XPath expression like  //Response/e[1]/childNode

Now i don’t have anything else to share, so please stop reading and consider this as the end of this blog. And if are still reading, then … well

Hint to find answer : Use any search engine and you will get the answer :-p

Till next blog post… happy reading & happy sharing 🙂

Advertisements