cancel
Showing results for 
Search instead for 
Did you mean: 

compare two soap responses using groovy

Highlighted
Community Hero

Re: compare two soap responses using groovy

Unable to use the given information to try out a solution.


Regards,
Rao.
Highlighted
Contributor

Re: compare two soap responses using groovy

Hi @nmrao ,

 

I'm not understanding what kind of extra information you need. Can you please elaborate

Highlighted
Contributor

Re: compare two soap responses using groovy

@roja1 

 

Currently I'm also working on same task. I'm using XMLUnit2 to compare two soap responses.

The only diffrenece from your case, I'm comaring two resaponses from external files(C://SoapUI//response1 etc..).

I would suggest to use XMLUnit2, we can ignore some attributes/node upon our requirement. 

 

Please refer some post which I did, 

Post1: https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-print-all-differences-when-comparing-tw...

Post2: https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-ignore-nodes-with-It-values-when-compar...

 

lety me know, if you have any questions.

Highlighted
Contributor

Re: compare two soap responses using groovy

@Redde ,

 

Sorry for the late response. I have tried the way you suggested. And I'm getting org.xmlunit.XMLUnitException:Content is not allowed in prolog.

 

Please find the below 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
import org.xmlunit.builder.DiffBuilder
import org.xmlunit.diff.Diff
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)
(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)
if(f.getContents()!='')
{
step_name=f.getContents()
Cell f1=inputSheet.getCell(3,j)
def String step_response=f1.getContents()
step_response=new XmlSlurper().parseText(step_response)
def String response = context.expand(step1.getPropertyValue('Response'))
//response=response.replaceAll(" ","")
if(step1.name==step_name&&step1.name!=null){
Diff mydiff=DiffBuilder.compare(response).withTest(step_response).ignoreComments().ignoreWhitespace().checkForSimilar().build()
log.info (mydiff.toString())
}
}


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

Highlighted
Contributor

Re: compare two soap responses using groovy

@roja1 

 

I'm using XMLUnit with .xml files. No Idea exactly with excel sheet. 

Can you please show how you are storing XML in Excel,(If you can).

Highlighted
Contributor

Re: compare two soap responses using groovy

@Redde ,

 

I'm just adding my xml data to a cell.I have attached the screenshot for your reference. 

 

Highlighted
Contributor

Re: compare two soap responses using groovy

I got to know that this kind of error occurs when something is present before <?xml . But my response doesn't have <?xml at all. How can I get rid of this error and compare my responses?

 

Highlighted
Contributor

Re: compare two soap responses using groovy

@roja1 

 

Did a sample, stored 1 xml in Excel cell, as like your snapshot, once see below code, It worked for me.

 

import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
import com.eviware.soapui.support.*;
import org.xmlunit.builder.DiffBuilder
import org.xmlunit.diff.Diff
import org.xmlunit.diff.*
def counter,next,previous,size
Workbook workbook1 = Workbook.getWorkbook(new File("C:\\SOAP\\Book1.xls"))
Sheet sheet1 = workbook1.getSheet(0)

size= sheet1.getRows().toInteger()
//Intially set count 0 or 1 (Deepends on rows in excel) at test case
counter1 = context.testCase.getPropertyValue("Count").toString()
counter = counter1.toInteger()
def response = testRunner.testCase.getTestStepByName("getSoapTestStep").getPropertyValue("response");
next = (counter > size-2? 1: counter+1)

Cell c1 = sheet1.getCell(3,counter)
baseXML= c1.getContents()

context.testCase.setPropertyValue("Count", next.toString()) ;
def Count = context.testCase.getPropertyValue("Count") ;
Diff mydiff=DiffBuilder.compare(response).withTest(baseXML).ignoreComments().ignoreWhitespace().checkForSimilar().build()
for (Difference d : mydiff.getDifferences()) {
log.error d
}
next++ //increase next value

workbook1.close()

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