Forum Discussion

TestQA1's avatar
TestQA1
Frequent Contributor
2 years ago

Comparing two text file content without using Store - File object

Hi,

 

I have two text files with some content and I need to compare those files contents using javascript in my test scripts.

I don't want to store files in test complete to use File object. Is there any other way I can compare files?

 

Thanks

9 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    These two files may look the same,

    But they are totally different. The first file contains a tab (->) character followed by carriage return (CR) and linefeed (LF). The second file just contains a space (.). ASCII characters are made visible by using Notepad++.

     

    Byte-by-byte will compare the first character in the file, then the second, third, fourth and so on. If you've opened the file using Windows Notepad, the contents may look the same, but there's probably hidden characters which Notepad will not show. 

  • Kitt's avatar
    Kitt
    Regular Contributor

    You do not need to store files within TestComplete to compare them, as long as your local file paths do not change you should be able to use their direct paths like so:

    if (!Files.Compare("C:\\Work\\OrdersList_Var1.txt", "C:\\Work\\OrdersList.txt"))
        Log.Warning("List of orders was changed");

     official TestComplete reference [here].

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Kitt I tried that but it says 'File is not defined' until I create a Store.

      Instead I am using aqFile.Compare which works fine but the problem is that it seems to compare attributes of two files besides content. So for example, if my file1 has 'abc' and file2 also has 'abc' it fails the comparison until file2 is the copy of file1 which does not make sense. If I create file2 (i.e I do not copy/paste file1 and rename it to file2) the comparison fails.

      Any idea what else the compare method checks besides content as my only requirement is to match content not any metadata.

       

      Thanks 

  • TestQA1's avatar
    TestQA1
    Frequent Contributor

    Found aqFile that seems to be working, but it's showing that the files are different even though the contents are same. I think it's strickly looking for the same attributes not just content/data in the files.

    Is there a possibility to only make aqFile check for similar data in files not any other attribute?

     

    Thank you

  • TestQA1's avatar
    TestQA1
    Frequent Contributor

    What if I write my own logic and don't use Compare methods. 

    I tried a below code but the loop seems to be running more than the indexes/lines I have in the first file and the values at index shows single character.

    function CompareFiles() {
    var firstFile = "abc.txt";
    var secFile = "aac.txt";
    for(let i =0; i<firstFile.length;i++) {
    if(firstFile[i] !== secFile[i])
    return true;
    else {
    return false;
    }
    }

    }

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The aqFile.Compare method, compares two files byte-by-byte. 

     

    Can you add those two files that fail to compare?