Forum Discussion

kkumar1ta's avatar
11 years ago

Comparing the two xml responses when the order is different

Hi

I need to compare two xml responses using groovy script, two xml responses are having array loops. I am able to compare one to one mapping using groovy , but order is coming differently in both the responses.

It would be great if some body help me in doing this ,
Example given below
RESPONSE1:
<RecordDetails>
<Record>
<Name>sam</Name>
<Price>1.20</Price>
</Record>
<Record>
<Name>David</Name>
<Price>4.30</Price>
</Record>
<Record>
<Name>George</Name>
<Price>3.20</Price>
</Record>
</RecordDetails>

RESPONSE 2:
<RecordDetails>
<Record>
<Name>David</Name>
<Price>4.30</Price>
</Record>
<Record>
<Name>George</Name>
<Price>3.20</Price>
</Record>
<Record>
<Name>sam</Name>
<Price>1.20</Price>
</Record>
</RecordDetails>

Regards,
kkumar
  • GiscardN's avatar
    GiscardN
    Frequent Contributor
    Hi,

    You can do this with a Groovy script assertion using XMLSlurper.
    This is an example comparing two responses:
    def response1 = context.expand( '${JDBC result#JDBC_Response}' )
    def response2 = context.expand( '${Request 2#Response}' )

    def firstrecords = new XmlSlurper().parseText(response1)
    def secondrecords = new XmlSlurper().parseText(response2)

    Then for each record in response 1, you loop through response 2 and assert. Return true if a match is found in the second response, false otherwise.
    Ex: you are comparing each firstrecords.Record.price with each secondrecords.Record.price[j]

    You can learn more about XMLSlurper here: http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper

    Regards,

    Giscard
    SmartBear Support