Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
8 years ago
Solved

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

6 Replies

  • Radford's avatar
    Radford
    Super Contributor

    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.

    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Hi

       

      Que1: For parsing the response from my request, I have used following. But it doesn't work. can you please correct if I am missing something.

       

       

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

      def responseHolder = groovyUtils.getXmlHolder("SelfFullSubs-Subscription-Filter-ChargingMethod-1#Response")

       

      From your response

      // 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>'''

       

      Que 2: Actually for each subscription, I have to assert value as 1 for each charging-method attribute. And there are multiple such subscriptions. So can you please tell me how to do so.

      <charging-method>1</charging-method>

       

      Regards

      Vishal

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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

    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Thanks a lot.

       

      Both of above solutions worked perfectly fine.

       

      Regards

      Vishal

       

    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Hi

       

      In the same file, if I have to validate multiple occurrences of <charging-method>1</charging-method>

       

      How should I proceed for same.

       

      Actually I have used this

      count(//subscription/[@charging-method!=1])

      Expected: 0

       

      Note: Just I have validate value as 1 for each occurrence of <charging-method> element.

       

      can you please help.

       

      Regards

      Vishal

       

       

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        vpachpute1 wrote:

        In the same file, if I have to validate multiple occurrences of <charging-method>1</charging-method>

         

        How should I proceed for same.

         

        Actually I have used this

        count(//subscription/[@charging-method!=1])

        Expected: 0

         

        Note: Just I have validate value as 1 for each occurrence of <charging-method> element. 


        Change the XPath expression to:

        count(//charging-method[.!=1])

        This will check for the presence of the "charging-method" elements with the node value other than 1.