vpachpute1
8 years agoFrequent Contributor
How to assert attribute values if there are multiple occurences of attribute
Hi
I have one scenario which returns multiple subscriptions and I have to assert status as 1 (i.e. Active ) for each subscription.
can you please suggest solution how to do so.
Attached sample response file.
Thanks in advance
Regards
Vishal
There are probably multiple ways to do this but I would use a Script assertion, something like:
import groovy.util.XmlSlurper // Get your XML from the appropriate test step response def xmlText = '''<response> <subscriptions-v1> <subscription id="1376" status="1"> <charging-method>1</charging-method> </subscription> <subscription id="1372" status="1"> <charging-method>1</charging-method> </subscription> </subscriptions-v1> </response>''' def response = new XmlSlurper().parseText(xmlText) // Normally you don't need quotes when referencing the result of the parse text (a GPathResult object) // but in this case we do as the XML element name contains a hyphen (-). response.'subscriptions-v1'.children().each{ subscription -> //Next three lines just for demo purpose def subscriptionId = subscription.attributes().get('id') def subscriptionStatus = subscription.attributes().get('status') log.info('subscription id = ' + subscriptionId + ' has the status = ' + subscriptionStatus) // Note: attribute will be a string value, so need to assert againat a string. assert subscription.attributes().get('status') == '1' }
More information on the XmlSlurper can be found here.
You can use the XPath Match assertion to check that there are no subscriptions with status other than 1.
XPath expression: count(//subscription[@status!=1])
Expected result: 0