Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

Can SilkTest be used to compare text in two files when the files contain the same text but have different formating?

Can SilkTest be used to compare text in two files when the files contain the same text but have different formating?

Yes it is possible to compare the the text in the two files. This can be done by reading the text from the files, removing any whitespace (i.e. spaces and tabs) and then comparing the text that remains.

The following is an example of how this can be achieved.

[-] testcase CompareFiles() appstate none
    [ ] STRING sString, sString2
    [ ] STRING sLine, sLine2
    [ ] STRING sParsedString =""
    [ ] STRING sParsedString2 =""
    [ ] 
    [ ] // the following reads the file line by line and adds the contents to a string
    [ ] // The tabs and then the spaces are removed from the string using the StrTran function. The middle parameter of the 
    [ ] // first StrTran contains a tab that has been typed. 
        [-] HFILE1 = FileOpen ("c:/test1.txt", FM_READ)
        [-] while (FileReadLine (HFILE1, sLine))
                  [ ] sParsedString = sParsedString + sLine
        [ ] sString = StrTran (sParsedString, "    ", "")
        [ ] sString = StrTran (sString , " ", "")
        [ ] //Print (sString)
    [ ] 
    [ ] // This does the same as the above for the second file
    [-] HFILE2 = FileOpen ("c:/test2.txt", FM_READ)
        [-] while (FileReadLine (HFILE2, sLine2))
            [ ] sParsedString2 = sParsedString2 + sLine2
        [ ] sString2 = StrTran (sParsedString2, "  ", "")
        [ ] sString2 = StrTran (sString2 , " ", "")
        [ ] 
    [ ] 
    [ ] // Now the strings can be compared to verify whether they are the same
    [ ] // Either print a success message if the files contents are the same
    [ ] // or by using the "raise" statement we raise an error that the files are different you could change this if you dont want the error to occur.
    [-] if sString == sString2
        [ ] Print ("The contents of the Files are the same")
    [-] else 
        [ ] raise 1, "ERROR: The contents of the files are different"
        [ ] 
    [ ] //closing the handle to files.
    [ ] FileClose (HFILE1)
    [ ] FileClose (HFILE2)



Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:15
Updated by:
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.