Forum Discussion

ashly's avatar
ashly
Champion Level 1
2 months ago

csv file comparison

Hi, In TestComplete keyword testing I am using Compare files checkpoint to compare two csv files. Currently, the Compare files operator returns True or False depending on the comparison result of two csv files. However it does not show (or save into a separate file) what differences where found. The files can contain many lines of the records and it could be tedious job going through and find what exactly has changed. Is there a way the TestComplete not only report that the files differ from each other but also show all differences

  • ashly's avatar
    ashly
    Champion Level 1

    Yes got it! But according to our requirement we cannot make a baseline copy of the existing csv file and store it in the Files folder within the Stores. Instead both the comparing files should be able to be saved within some other folder. Hence we have opted Compare Files operator but as mentioned earlier it does not show what differences were found. So is there any other alternative to fix this?

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      If you have a baseline copy of the file, you can temporarily add it to Stores, perform the comparison, then delete it afterwards.

      There's also the option of using third-party diff utility within TC.

       

      • ashly's avatar
        ashly
        Champion Level 1

        ok .. can you please share an example for the third- party diff utility within TC

  • JDR2500's avatar
    JDR2500
    Frequent Contributor

    Hi, we use Powershell's Compare-Object method to compare CSV and XML files.  We pass the function the full paths of the files to compare.  The function returns True or False depending on the comparison result.  Additionally, if the comparison fails it outputs a file showing only the differences.  Below is the VBScript function.

    Function AtsCompareFilesPS(RFile, TFile)
      Dim oShell, oExec, strOutput
      Dim o_Tfile
    
      s_gpDirectory = Project.Variables.s_gpDirectory
      Set oShell = Sys.OleObject("WScript.Shell") 
      
      '-- Get the full path of the test file w/out the extension
      i_FNameLen = Len(TFile)
      s_FNameNoExt = Left(TFile, i_FNameLen-4) 'Strip the extension
      
      '-- Compare the files using Powershell
      s_PowershellInput = "powershell -command Compare-Object -ReferenceObject $(Get-Content '" & RFile & "') -DifferenceObject $(Get-Content '" & TFile & "') | where {$_.SideIndicator -eq '<=' -and !$_.InputObject.StartsWith('#')} | select -ExpandProperty inputObject"
      Set oExec = oShell.Exec(s_PowershellInput)
      oExec.StdIn.Close ' Close standard input before reading output
    
      ' Get PowerShell output with the comparison results
      strOutput = oExec.StdOut.ReadAll
    
      '-- If strOutput is "" then the files matched
      If strOutput = "" Then 
        AtsCompareFilesPS = True
      Else  'Return false and save the output to a text file in the same location as the test file
        AtsCompareFilesPS = False
        s_LogFullPath = s_FNameNoExt & "-PS-Diifs"& ".txt"
        'Delete an existing log with the same name if it exists
        If aqFile.Exists(s_LogFullPath) Then aqFile.Delete(s_LogFullPath)
        'Create the new log
        If (aqFile.Create(s_LogFullPath) = 0) Then
            ' If the file has been created successfully, write the comparison results to it
            Call aqFile.WriteToTextFile(s_LogFullPath, strOutput, aqFile.ctUTF8)
            Call Log.Message("A log file was written with the failed comparison results.")
          Else 
            Call Log.Warning("The comparison log could not be created.")
       End If
      End If
    End Function