Ask a Question

How to compare 2 response XMLs from 2 Different Test Steps and print in Excel ?

Avik21
Visitor

How to compare 2 response XMLs from 2 Different Test Steps and print in Excel ?

Hi,

 

I have a response XML from a SOAP Test Request. I have another response XML from from the JDBC Test Step. Both of the response XMLs print out the category ID & Category Name.

 

I want to compare both of them and then print out the result in Excel.

 

Response XMLfrom SOAP Request

==========================

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/retail/namespaces">
<SOAP-ENV:Body>
<ns1:GetAllCategoriesResponse xmlns:ns1="urn:RetailApp">
<return xsi:type="tns:allcats">
<allcategories xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:categorydetail[4]">
<item xsi:type="tns:categorydetail">
<category_id xsi:type="xsd:string">7</category_id>
<category_name xsi:type="xsd:string">Human Resource</category_name>
</item>
<item xsi:type="tns:categorydetail">
<category_id xsi:type="xsd:string">5</category_id>
<category_name xsi:type="xsd:string">IT</category_name>
</item>
<item xsi:type="tns:categorydetail">
<category_id xsi:type="xsd:string">6</category_id>
<category_name xsi:type="xsd:string">Manufacturing</category_name>
</item>
<item xsi:type="tns:categorydetail">
<category_id xsi:type="xsd:string">8</category_id>
<category_name xsi:type="xsd:string">Marketing</category_name>
</item>
</allcategories>
<sessiondata xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:sessiondetail[1]">
<item xsi:type="tns:sessiondetail">
<sessionid xsi:type="xsd:string">O1GHg65lCq1492324865</sessionid>
<username xsi:type="xsd:string">dey.avik5@gmail.com</username>
</item>
</sessiondata>
</return>
</ns1:GetAllCategoriesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Response XML from JDBC

====================

<Results>
<ResultSet fetchSize="0">
<Row rowNumber="1">
<CATEGORY.CAT_ID>7</CATEGORY.CAT_ID>
<CATEGORY.CAT_NAME>Human Resource</CATEGORY.CAT_NAME>
</Row>
<Row rowNumber="2">
<CATEGORY.CAT_ID>5</CATEGORY.CAT_ID>
<CATEGORY.CAT_NAME>IT</CATEGORY.CAT_NAME>
</Row>
<Row rowNumber="3">
<CATEGORY.CAT_ID>6</CATEGORY.CAT_ID>
<CATEGORY.CAT_NAME>Manufacturing</CATEGORY.CAT_NAME>
</Row>
<Row rowNumber="4">
<CATEGORY.CAT_ID>8</CATEGORY.CAT_ID>
<CATEGORY.CAT_NAME>Marketing</CATEGORY.CAT_NAME>
</Row>
</ResultSet>
</Results>

 

I am a Software Tester
1 REPLY 1
nmrao
Community Hero

 

Here is the groovy script :

 

//assign response data for below two statements, not done here to save space
def soapResponse = 
def jdbcResponse = 

def getDetails = { data, recordElement, id, name ->
 new XmlSlurper().parseText(data).'**'.findAll{it.name() == recordElement && !it."$id".isEmpty()}.inject([:]){map,item -> map[item."$id".text()] = item."$name".text();map}.sort()
}

def soap = getDetails(soapResponse, 'item', 'category_id', 'category_name')
log.info "Soap response details : $soap"
def jdbc = getDetails(jdbcResponse, 'Row', 'CATEGORY.CAT_ID', 'CATEGORY.CAT_NAME')
log.info "Jdbc response details : $jdbc"
assert soap == jdbc, 'both are not matching'

You can quickly try online demo with your responses.

Also the same script is available at github



Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: