How to verify response content if it has valid DateTime or Decimal value, or if it has no value?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to verify response content if it has valid DateTime or Decimal value, or if it has no value?
Please help me to correctly determine the data type of the response as it seems that regardless of the actual response value it will always evaluate to a string. Please see the following example which I also got from one of the posting here in smartbear community forum. Thank you.
def xml='''
<xml>
<node>
<val1>1</val1>
<val2>12.00</val2>
<val3>true</val3>
<val4>testvalue</val4>
</node>
</xml>'''
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def reader = groovyUtils.getXmlHolder(xml )
use (groovy.xml.dom.DOMCategory) {
for( node in reader.getDomNodes( "//node" )) {
node.children().each { child ->
if (child.text() instanceof Integer) {
log.info child.text() +" is integer"
} else if (child.text() instanceof Double) {
log.info child.text()+" is double"
} else if (child.text() instanceof Boolean) {
log.info child.text()+" is boolean"
} else if (child.text() instanceof String) {
log.info child.text()+" is string"
}
}
}
}
//===============================================//
OUTPUT
Fri Feb 13 07:46:03 IST 2015:INFO:1 is string
Fri Feb 13 07:46:03 IST 2015:INFO:12.00 is string
Fri Feb 13 07:46:03 IST 2015:INFO:true is string
Fri Feb 13 07:46:03 IST 2015:INFO:testvalue is string
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your response nmrao, I am hoping that I can validate response data through script assertion because I might need to do some mathematical procedure to validate a Decimal or a DateTime values, such as to test if a response DateTime value is within a certain period like January 1 to March 31, 2017.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Convert the string to date or double, then you could test the values.
http://stackoverflow.com/questions/3817862/groovy-string-to-date
http://stackoverflow.com/questions/5769669/convert-string-to-double-in-java
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
By the way, the sample data provided does not have any such type though.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here is what I did to convert and test my response data.
def response = new XmlSlurper().parseText(hl.prettyXml)
def val1 = response.RootNode.ChildNode
def val2= Date.parse('yyyy-MM-dd' , val1.toString())
assert (val2 instanceof Date)
