cancel
Showing results for 
Search instead for 
Did you mean: 

compare two soap responses using groovy

Highlighted
Contributor

compare two soap responses using groovy

Hi @avidCoder / @nmrao ,

 

I have expected response in an excel file. I want to compare the expected response with the actual response from soapUI and while comparing I need to ignore some fields using groovy script. Please help me with this.

 

Thank you in advance

17 REPLIES 17
Highlighted
Community Hero

Re: compare two soap responses using groovy

1. Why response in excel file? Can't it be in xml file (if SOAP response)?

2. Question is generic, so I can only point you to existing solutions such as

https://github.com/nmrao/soapUIGroovyScripts/tree/master/groovy/compare

 

Post sample data with same structure if you still have issues.



Regards,
Rao.
Contributor

Re: compare two soap responses using groovy

Hi,

 

I have expected responses of all the test steps in an excel and I'm comparing the soapUI response with the expected response . So as per my requirement I'm reading expected response from excel

Highlighted
Contributor

Re: compare two soap responses using groovy

Hi,

 

Sample data:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Receiver</env:Value>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">InvalidRequest: Token not found!</env:Text>
</env:Reason>
</env:Fault>
</env:Body>
</env:Envelope>

Below is the code which I have used for reading expected response from excel and response from SoapUI. After reading I'm unable to proceed further for comparision

Code:

import static java.nio.charset.StandardCharsets.*;
import java.lang.*;
import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import groovy.json.JsonSlurper
/*XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)*/
def count=0,i=1,j=1,k=1
def inputFileName,outputFileName
def inputFile = testRunner.testCase.testSuite.getPropertyValue("inputFileName")
def outputFile = testRunner.testCase.testSuite.getPropertyValue("outputFileName")
def testStep = testRunner.testCase.testSteps["Properties"]

Workbook inputWB = Workbook.getWorkbook(new File(inputFile))
Sheet inputSheet = inputWB.getSheet(0)
no_of_rows= inputSheet.getRows().toInteger()

Workbook existingInputWB=Workbook.getWorkbook(new File(inputFile))
WritableWorkbook outputWB=Workbook.createWorkbook(new File(outputFile),existingInputWB)

testCaseName=testRunner.testCase.name
outputWB.createSheet(testCaseName,2)
WritableSheet sheet_writable = outputWB.getSheet(testCaseName)
jxl.write.Label testStepCell = new jxl.write.Label(0,0 ,"Test Step")
jxl.write.Label reqUrlCell = new jxl.write.Label(2,0 ,"Request URL")
jxl.write.Label reqPayloadCell = new jxl.write.Label(3,0 ,"Request Payload")
jxl.write.Label responseCell = new jxl.write.Label(4,0 ,"Response")
jxl.write.Label statusCell = new jxl.write.Label(1,0 ,"Status")
jxl.write.Label reasonCell = new jxl.write.Label(5,0 ,"Reason")
sheet_writable.addCell(testStepCell)
sheet_writable.addCell(reqUrlCell)
sheet_writable.addCell(reqPayloadCell)
sheet_writable.addCell(responseCell)
sheet_writable.addCell(statusCell)
sheet_writable.addCell(reasonCell)
(0..context.testCase.testStepCount-1).each{
def step = context.testCase.testStepList[it]
if ( step instanceof WsdlTestRequestStep) {
jxl.write.Label stepName = new jxl.write.Label(0,i ,step.name)
sheet_writable.addCell(stepName)
def tr=testRunner.testCase.getTestStepByName(step.name)
def String endPointUrl= tr.getHttpRequest().getResponse().getURL()
jxl.write.Label reqUrl = new jxl.write.Label(2,i ,endPointUrl)
sheet_writable.addCell(reqUrl)
def payload = context.expand(step.getPropertyValue('Request'))
jxl.write.Label reqPayload = new jxl.write.Label(3,i ,payload)
sheet_writable.addCell(reqPayload)
def response = context.expand(step.getPropertyValue('Response'))
jxl.write.Label reqResponse = new jxl.write.Label(4,i ,response)
sheet_writable.addCell(reqResponse)
count=count+1
i=i+1
}
}

(0..context.testCase.testStepCount-1).each{
def step1 = context.testCase.testStepList[it]
if ( step1 instanceof WsdlTestRequestStep) {
for(j=1;j<no_of_rows;j++){
Cell f=inputSheet.getCell(0,j)
step_name=f.getContents()
Cell f1=inputSheet.getCell(3,j)
def String step_response=f1.getContents()
step_response=step_response.replaceAll(" ","")
def String response = context.expand(step1.getPropertyValue('Response'))
log.info response
response=response.replaceAll(" ","")
/*if(step1.name==step_name&step1.name!=null){
}*/

}
}
}
outputWB.write()
outputWB.close()
existingInputWB.close()

 

Highlighted
Community Hero

Re: compare two soap responses using groovy

This is repeated post you are posting without use.

Can't read your long code.

Who can imagine what is in excel?
Why can't the response be in a file which will be simple to read?
At least provide expected data which you are comparing?

You were talking about ignoring elements, please mention the same.


Regards,
Rao.
Highlighted
Contributor

Re: compare two soap responses using groovy

Hi @nmrao ,

 

This is not a repeated post. Previously I was working on rest api's and I'm reusing the same code used for rest service to achieve the same thing for soap service.Atteched the screenshot of the excel.Attached the template of output excel as well. As per requirement we are reading from excel.

In the below output I want to ignore comparision of Auth.

Expected data that I'm trying to compare:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:ZeppEsCheckOnlCustIfResponse xmlns:ns3="urn:sap-com:document:sap:soap:functions:mc-style">
<Auth>36962796d68753535353f2a79637f536865636b6f6e6c636573747f213537353839303631393336353c745543545</Auth>
<number>224354</number>
<Ok>X</Ok>
<Failure/>
</ns3:abcd>
</env:Body>
</env:Envelope> 

Highlighted
Community Hero

Re: compare two soap responses using groovy

Two sample data have been provided in two different replies. But could not related them / map one to one to compare.


Regards,
Rao.
Highlighted
Contributor

Re: compare two soap responses using groovy

Hi @nmrao ,

 

I want to write a generic code, which can be used to any kind of soap responses.

Please consider the below data :

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:ZeppEsCheckOnlCustIfResponse xmlns:ns3="urn:sap-com:document:sap:soap:functions:mc-style">
<Auth>36962796d68753535353f2a79637f536865636b6f6e6c636573747f213537353839303631393336353c745543545</Auth>
<number>224354</number>
<Ok>X</Ok>
<Failure/>
</ns3:abcd>
</env:Body>
</env:Envelope> 

Highlighted
Community Hero

Re: compare two soap responses using groovy

You may.

But in this context, it is not clear what to compare. Or missing something?


Regards,
Rao.
Highlighted
Contributor

Re: compare two soap responses using groovy

The sample data which I have provided , I'm reading it from excel and I'm comparing it with the soap response from soapUI which I get when I run a test step

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors