cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to compare two text files

Occasional Contributor

Groovy script to compare two text files

Hi,

 

Please share me the groovy script.

 

  • to read two text files
  • compare the above text files and print the difference

 

Thanks

Naveen

14 REPLIES 14
Community Hero

Re: Groovy script to compare two text files

It would be good if you can show your data, need not be the actual, at least to resembling your data.


Regards,
Rao.
Occasional Contributor

Re: Groovy script to compare two text files

Thanks for your response.

 

Actually there will be some upgrade in the server.

 

Before upgrade, i need to run my request against the server and  i ll capture response and store in my drive. 

After upgrade i need to hit the same server with the same request. At that time i need to compare the current response with the already stored response dynamically using groovy.

 

test data is normal xml response.

 

<?xml version="1.0" ?>
<Process>
<Execution>
<Execution_Date>2015/11/16 15:58:05</Execution_Date>
<Process_File>Sample_2015-11-16_153023.xml</Process_File>
<Client>IBLLC</Client>
<Client_Master_Acct>FXXXXXXX</Client_Master_Acct>
</Execution>
<Applications>
<Application External_ID="ABCD1002" Status="Success">
<Customer>Joseph Clientman</Customer>
<Users>
<User external_id="ABCD1002USR" password="xxxxxxxx">jocmn841</User>
</Users>
<Accounts>
<Account external_id="ABCD1002AC">UXXXXXXX</Account>
</Accounts>
<Entities>
<Entity external_id="ABCD1002AH">20951330</Entity>
</Entities>
<Pending_Tasks>
<Task Task_Number="87745956" Form_Number="1005" Form_Name="Margin Agreement" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745947" Form_Number="2192" Form_Name="Privacy Statement" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745948" Form_Number="3007" Form_Name="Customer Agreement (v2)" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745949" Form_Number="3024" Form_Name="Forex and Multi-Currency Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745955" Form_Number="3044" Form_Name="US Bond Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745950" Form_Number="3070" Form_Name="Business Continuity Plan Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745951" Form_Number="3071" Form_Name="Day Trading Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745952" Form_Number="3074" Form_Name="Order Routing and Payment for Order Flow Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745963" Form_Number="3076" Form_Name="ISE Disclosure for Option Orders Over 500 Contracts" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745964" Form_Number="3077" Form_Name="Municipal Bond Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745968" Form_Number="4003" Form_Name="OCC Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745953" Form_Number="4016" Form_Name="After-hours Trading Risk Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745941" Form_Number="4035" Form_Name="Mutual Fund Agreement" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745973" Form_Number="4036" Form_Name="Mutual Fund Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745936" Form_Number="6105" Form_Name="Client FA Agreement" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745937" Form_Number="6108" Form_Name="Agreement Limiting Interactive Brokers Liability" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
<Task Task_Number="87745981" Form_Number="9130" Form_Name="US Stock Stop Order Disclosure" Action="to sign" Is_Required_For_Approval="true" Is_Required_For_Trading="false" Is_Online_Task="true"></Task>
</Pending_Tasks>
</Application>
</Applications>
</Process>

 

Contributor

Re: Groovy script to compare two text files

Hi Team,

 

Could you please let me know the solution for the same.

Contributor

Re: Groovy script to compare two text files

Hi can someone share the same.
Occasional Contributor

Re: Groovy script to compare two text files

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)

 

Contributor

Re: Groovy script to compare two text files

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.

Occasional Contributor

Re: Groovy script to compare two text files

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();
        }
    }
 
Contributor

Re: Groovy script to compare two text files

Hi,

 

Thanks for the code but i need to compare two json responses.the code that you have given is for xml.

Contributor

Re: Groovy script to compare two text files

hey this code is not working.

New Here?
Join us and watch the welcome video:
Watch the New Interview
7Mistakes
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win