Forum Discussion

Dheepha's avatar
Dheepha
Contributor
13 years ago

Fetching a tag value from SOAP Response using Groovy

Hi,
I'm trying to fetch the tag value from the soap response after execution.
Say <ServerDt>2013-09-23 12:12:12</ServerDt>

Here I need to fetch the ServerDt value and verify the format of the ServerDt as per the XSD.
In my xsd , the ServerDt is mentioned as DateTime and the format is 'yyyy-mm-dd hh:mm:ss'.

So I have written a groovy to fetch the value and check the format using Joda Calendar.

Here is my groovy script:

import com.eviware.soapui.support.GroovyUtils;
import org.joda.time.DateTime
import org.joda.time.format.ISODateTimeFormat

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def response = context.expand( '${Request#Response}' )
def holderResponse = groovyUtils.getXmlHolder( response )
def dateString = holderResponse.getNodeValue("//*[ServerDt]");

log.info (dateString);
log.info("-------------Entering Date Verification Groovy Script-------------");

try {
ISODateTimeFormat.dateTimeParser().parseDateTime(dateString)
} catch (java.lang.IllegalArgumentException iae) {
assert false
log.info("invalid date");
}
assert true

But 'null' is printing in the log.info of 'dateString'.
Mon Sep 23 12:21:03 IST 2013:INFO:null

I don't know where I made a mistake. Can anyone tell me the way how to fetch the tag value from soap response?

3 Replies

  • Hello,
    I think you can replace following statement:
    def dateString = holderResponse.getNodeValue("//*[ServerDt]");
    by, def dateString = holderResponse.getNodeValue(".//[ServerDt]");
    and then use 'toString()' to get string form to compare/assert.
    But as the result of above statement is enclosed between '[' and ']', so need to get rid off of '[' and ']' use following:
    def substring = dateString [1..-2]
    and finally use datetime formatter for asserting
    Hope it helps...
  • Hi Reshma,

    I tried the following as per your comments and my code looks like:

    def dateString = holderResponse.getNodeValue(".//ServerDt")
    def date=dateString.toString()

    Initially I have tried with the def dateString = holderResponse.getNodeValue(".//[ServerDt]") and I was getting
    "java.lang.RuntimeException: net.sf.saxon.trans.XPathException: XPath syntax error at char 4 on line 2 in {\n.//[ServerDt}: Unexpected token "[" in path expression"

    So I have removed the braces '[' , ']'.
    But When I try to run , still I'm getting the null as the dateString.

    Also I'm not sure where to use the substring function as the dateString is returning null. Could you please help me on this and correct my code if I made a mistake.
  • I have read from some other post and updated my groovy script with the namespace and it got worked.

    My final Groovy script to fetch the value from tag and verify the format of date:

    import com.eviware.soapui.support.GroovyUtils;
    import org.joda.time.DateTime
    import org.joda.time.format.ISODateTimeFormat

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def response = context.expand( '${Request#Response}' )
    def holderResponse = groovyUtils.getXmlHolder( response )
    holderResponse.namespaces["ns"] = "http://www.ifxforum.org/IFX_150"
    def dateString = holderResponse.getNodeValue(["//ns:ServerDt"])

    log.info (dateString);

    try {
    ISODateTimeFormat.dateTimeParser().parseDateTime(dateString)
    } catch (java.lang.IllegalArgumentException iae) {
    assert false
    log.info("invalid date");
    }
    assert true

    Thanks.