Forum Discussion

mchelikani's avatar
mchelikani
Contributor
13 years ago

How To Create New Report

My Project Has Requirement to Compare the XML Responses from Two Folders which Would have same Directory Structure and Files.

I have the code(groovy script) which I currently put it in ProjectRunListener.afterRun. But I would like to put the differences in a Custom Report.

Appreciate any ones help in how to approach this. An example and step by step instructions would be great.

3 Replies

  • Thanks Prakash!

    Currently, I have the Code which I put it in projectRunListener.afterRun. The below I am creating an html file with differences. But this is not working properly. The formatting is getting messed up. I am using MarkupBuilder.

    Instead I am wondering if I can use Custom Reports.



    import groovy.io.FileType
    import org.custommonkey.xmlunit.*
    import groovy.xml.MarkupBuilder


    def compareFromFolder = context.expand('${#Project#resultsComparePath}' )
    def compareToFolder = context.expand('${#Project#resultsPath}' )

    //Create a File For Difference Report.
    def diffReportFile = new File(compareToFolder + 'output-diffs.html' )

    def compareFrom = new File(compareFromFolder)
    def compareTo = new File(compareToFolder)

    def diffContent = new StringBuilder()

    //recreate the file
    if(diffReportFile.exists()) {
    diffReportFile.delete()
    diffReportFile.createNewFile()
    }

    compareFrom.eachFile (FileType.DIRECTORIES){ directory ->
    log.info("compareFromDir..." +directory.name)
    log.info("compareToDirPath..."+compareToFolder +directory.name)

    def compareFromCurrDir = directory.path
    def compareToCurrDir = compareToFolder + directory.name

    new File(compareFromCurrDir ).eachFileRecurse {file ->
    def fileName = file.name

    if( fileName.contains('_RS.xml') ){
    def compareFromFileAbsPath = compareFromCurrDir + "\\"+file.name
    def compareToFileAbsPath = compareToCurrDir+ "\\"+ file.name
    //def lineSeperator = System.getProperty("line.separator")
    def lineSeperator = System.getProperty("line.separator") + ' <BR> '
    def lineend = System.getProperty("line.separator") + '</BR>'

    println compareFromFileAbsPath
    println compareToFileAbsPath
    //Check if CompareTo Directory Exists
    if(new File(compareToCurrDir).exists()){
    def compareFromXML = getXMLAsString(compareFromCurrDir + "\\"+ file.name)
    def compareToXML = getXMLAsString(compareToCurrDir + "\\"+file.name)

    //println compareFromXML
    //println compareToXML
    //Compare for Nodes or Attributes
    diffContent.append lineSeperator
    diffContent.append '**************Comparing Files ' + compareFromFileAbsPath + ' And ' + compareToFileAbsPath + '************'
    diffContent.append lineend
    if(compareToXML ){
    compareResponseXMLs(compareToXML, compareFromXML, compareFromFileAbsPath, compareToFileAbsPath, diffContent, lineSeperator, lineend);
    }else{
    diffReportFile.append lineSeperator + compareToFileAbsPath + ' DOES NOT EXIST.'+lineend
    }
    diffContent.append lineSeperator
    diffContent.append '******************************************************************************************************* '
    diffContent.append lineend
    }
    }
    }
    }

    //println diffContent.toString()
    StringWriter writer = new StringWriter()
    def build = new MarkupBuilder(writer)
    build.html{
    head{
    title 'Response Diff Report'
    style(type:"text/css", '''
    .bigPaddingAndGreen {
    margin: 30px;
    padding: 30px;
    background-color: #00FF00
    }
    ''')
    }
    body{
    out.println diffContent.toString()
    }
    }

    diffReportFile.append writer.toString()

    /*
    compareFrom.eachFileRecurse (FileType.FILES) {
    //file ->list << file
    def currentDir = new File('.')

    println it.path
    log.info("file..."+it.path)
    log.info("currentDir..."+currentDir)

    }
    */

    def getXMLAsString(filePath){
    def File f = new File(filePath);
    def xml
    //Check if File Exists to Compare
    if(f.exists()){
    xml = f.text
    }
    return xml;
    }

    def compareResponseXMLs( actualXml,expectedXml, compareFromFileAbsPath, compareToFileAbsPath, diffContent, lineSeperator, lineend){
    XMLUnit.setIgnoreWhitespace(true)
    XMLUnit.setIgnoreComments(true)
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
    XMLUnit.setNormalizeWhitespace(true)
    //XMLUnit.setCompareUnmatched(true)
    //Compare for Values
    /*
    def xmlDiff = XMLUnit.compareXML(expectedXml, actualXml)
    //Print the Differences
    System.out.println(xmlDiff)
    log.info(xmlDiff);
    */
    def diff = new Diff(expectedXml, actualXml)
    def myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    diff.overrideDifferenceListener(myDifferenceListener)
    log.info("###################"+diff.identical())
    log.info("###################"+diff.similar())
    if(!diff.similar()) {
    diffContent.append lineSeperator
    diffContent.append 'Files are Different '
    diffContent.append lineend

    def detailedDef = new DetailedDiff(diff)
    def defferences = detailedDef.getAllDifferences()
    //Build Report
    buildDiffs(diffContent ,defferences, lineSeperator, lineend)
    } else {
    diffContent.append lineSeperator
    diffContent.append 'Files are Identical'
    diffContent.append lineend
    }

    }

    def buildDiffs(diffContent, differences, lineSeperator, lineend){
    differences.each { nodeDiff ->
    diffContent.append lineSeperator
    diffContent.append nodeDiff.toString()
    diffContent.append lineend
    //println outputDiffs.append nodeDiff.toString() + newLine
    }
    }
  • Hi!

    There's no build-in support for diff in reports in SoapUI unfortunately. I believe that using a Groovy Script, as you have started with is the way forward.
    If you would like, we could provide a feature request for this.

    --
    Regards

    Erik
    SmartBear Sweden