Forum Discussion

shahid24's avatar
shahid24
Contributor
8 months ago
Solved

XML Checkpoint

Hi ,

 

I would like to do something similar to  https://support.smartbear.com/testcomplete/docs/testing-with/checkpoints/files/comparing-with-variable-parts.html but for XML File Comparisons?

 

I would like to use regexp to ignore some of the stuff in the XML comparison? Is this doable ?

 

Thanks,

shahid 

  • XML is just a text file, so it can be compared, providing it is in the same format using Files.Compare Method

     

    If you want to exclude certain fields using regex, then you would want to export the contents to a separate file, then do the comparison.

     

5 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    XML is just a text file, so it can be compared, providing it is in the same format using Files.Compare Method

     

    If you want to exclude certain fields using regex, then you would want to export the contents to a separate file, then do the comparison.

     

  • rraghvani Any way I can update the below to show in the log the differences if found?

     

    Function CompareFiles(fileName1, fileName2)
      Dim fso, file1, file2, regEx
      Dim fileText1, fileText2, newText1, newText2
      Const ForReading = 1

      ' Creates the FileSystemObject object
      Set fso = CreateObject("Scripting.FileSystemObject")

      ' Reads the first text file
      Set file1 = fso.OpenTextFile(fileName1, ForReading)
      fileText1 = file1.ReadAll
      file1.Close

      ' Reads the second text file
      Set file2 = fso.OpenTextFile(fileName2, ForReading)
      fileText2 = file2.ReadAll
      file2.Close

      ' Creates the regular expression object
      Set regEx = New RegExp

      ' Specifies the pattern for the date/time mask
      ' MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)

      regEx.Pattern = "\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}"
      regEx.IgnoreCase = True
      regEx.Global = True

      ' Replaces the text matching the specified date/time format with <ignore>
      newText1 = regEx.Replace(fileText1, "<ignore>")
      newText2 = regEx.Replace(fileText2, "<ignore>")

      ' Compares the text
      CompareFiles = (newText1 = newText2)
    End Function

    Sub Main
      Dim fileName1, fileName2

      fileName1 = "d:\text1.txt"
      fileName2 = "d:\text2.txt"

      If CompareFiles(fileName1, fileName2) Then
        Log.Message("The files are equal")
      Else
        Log.Error("The files are different")
      End If
    End Sub

  • Also how to i enter two reg expressions patterns over here, do i simply separate them via a comma? 

     

      regEx.Pattern = "\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}" 

     

    as in regEx.Pattern = pattern1, pattern2