Forum Discussion
You can save the response and store it externally using code:
def ResFile ="C:/Response1.xml"
def Res = context.expand( '${TestStep#Response}' )
def j = new File(ResFile)
j.write(Res, "UTF-8")
You can use the Groovy XMLUnit utility like this to compare the XML files.
XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)
XMLUnit.compareXML(expectedXml, actualXml)
hello,
Could you please give me the script to compare two json files and print the mismatches in tags and values please...Any help is highly appreciated.
- ShasiSingh6 years agoOccasional Contributor
Before running the below code download xmlunit jar file and Copy XMLUnit jar file under SOAPUI_HOME/bin/ext, then restart soapui.
The below code is written inside ( TestSuite-->TestCase-->Groovy Script -->)
Look for references here
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.List; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.Difference; import org.xml.sax.SAXException; def String path01="C:\\SoapOutPut\\Base\\TEST_ReqV03_GetV02.xml"; def String path02="C:\\SoapOutPut\\Base\\TEST_ReqV04_GetV03.xml"; // Calling function to validate the outcome ComparisonTest (path01,path02) def ComparisonTest (filepathOrginal,filepathnew) { FileReader fr1 = null; FileReader fr2 = null; try { fr1 = new FileReader(filepathOrginal); fr2 = new FileReader(filepathnew); } catch (FileNotFoundException e) { e.printStackTrace(); } try { Diff diff = new Diff(fr1, fr2); log.info ("Similar record is --> " + diff.similar()); log.info ("Identical record is --> " + diff.identical()); DetailedDiff detDiff = new DetailedDiff(diff); List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object; log.info ("***********************"); log.info(difference); log.info("***********************"); } } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
- radhika16 years agoContributor
Hi,
Thanks for the code but i need to compare two json responses.the code that you have given is for xml.
- radhika16 years agoContributor
hey this code is not working.
- ShasiSingh6 years agoOccasional Contributor
Hi, This is a working Code, if you are using XML, But your question Title suggest ( Compare text files ) , and your shared sample talking about XML and your comments are talking about jason.
What you are looking for ? and where you have an error. could you please share the same.