Articles

[Collapse]TestComplete
 [Expand]Writing Automated Script Code
 [Expand]Working With Files, Databases and Excel Sheets
 [Expand]Web Testing - Functional Testing of Web Pages
 [Expand]TestComplete Trial Version Questions
 [Expand]Test Log and Results
 [Expand]Supported Applications, Operating Systems and Develompent Tools
 [Expand]Recording, Running and Debugging Automated Tests
 [Expand]Objects, Files and Images Comparison
 [Expand]Miscellaneous
 [Expand]Licensing Questions/viewarticle/65756/
 [Expand]General Questions About Automated Testing
 [Expand]Automated Testing Tasks - Basic
 [Expand]Automated Testing Tasks - Advanced
 [Expand]Testing Mobile Applications
 [Expand]Accessing Application's Internal Objects, Methods and Properties
 [Expand]Working With Source Control
Updated: 7/16/2015 Applies To: TestComplete Rating: PoorPoorPoorPoorPoor 14 vote(s) Click to rate: PoorNot badAverageGoodExcellent
Q.: How can I read data from and write them to a file in scripts?

A.: TestComplete provides the ability to read data of various formats from external files. For example:

  • To read data from text files, you can use the aqFile and aqTextFile program objects. You can also use the methods of the Scripting.FileSystemObject to work with text files.

  • To access data stored in XML files, you can use the Storages object or the XML DOM object. For more information on reading XML files, see the "Working With XML Files From Scripts" help topic.

  • You can also read data from binary files. You can do it the same way you work with text files (via the aqFile and aqTextFile program objects).

  • You can read data from CSV (comma-separated values) files. To do it, you can use either the DB Table variables, or the DDTDriver program object.

To learn more information about all of the possible ways to read data from files in TestComplete, please see the "Reading Data From External Files" help topic.

We would like you to pay special attention to using the aqFile program object. This object provides unified methods and properties to deal with binary and text files. Namely, the aqFile.ReadWholeTextFile method retrieves the entire contents of a text file, and the aqFile.WriteToTextFile method writes the specified string to a text file either appending it or replacing the current contents.

The code below illustrates how to use the aqFile scripting object:

VBScriptCopy Code

Sub FileTest
  Dim sPath
  sPath = "c:\MyFile.txt"
  If  Not aqFile.Exists(sPath) Then
       Call aqFile.Create(sPath)
  End If
  Call aqFile.WriteToTextFile(sPath, "Hello, world!" &_
                                vbCrlf , aqFile.ctANSI, False)
  Call Log.Message("The file contents is:")
  Call Log.Message(aqFile.ReadWholeTextFile(sPath, aqFile.ctANSI))
End Sub

JScriptCopy Code

function FileTest()
{
  var sPath;
  sPath = "c:\\MyFile.txt";
  if (!aqFile.Exists(sPath)) 
    aqFile.Create(sPath);
  aqFile.WriteToTextFile(sPath, "Hello, world!" + "\r\n", aqFile.ctANSI, false);
  Log.Message("The file contents is:");
  Log.Message(aqFile.ReadWholeTextFile(sPath, aqFile.ctANSI));
}

PythonCopy Code

def FileTest():
  sPath = "c:\\MyFile.txt"
  if not aqFile.Exists(sPath):
    aqFile.Create(sPath)
  aqFile.WriteToTextFile(sPath, "Hello, world!" + "\r\n", aqFile.ctANSI, False)
  Log.Message("The file contents is:")
  Log.Message(aqFile.ReadWholeTextFile(sPath, aqFile.ctANSI))

DelphiScriptCopy Code

procedure FileTest;
var  sPath; 
begin
  sPath := 'c:\MyFile.txt';
if  not aqFile.Exists(sPath) then
    aqFile.Create(sPath);
  aqFile.WriteToTextFile(sPath, 'Hello, world!' + #13#10, aqFile.ctANSI, false);
  Log.Message('The file contents is:');
  Log.Message(aqFile.ReadWholeTextFile(sPath, aqFile.ctANSI));
end;

C++Script, C#ScriptCopy Code

function FileTest()
{
 var sPath;
  sPath = "c:\\MyFile.txt";
 if (!aqFile["Exists"](sPath)) 
    aqFile["Create"](sPath);
  aqFile["WriteToTextFile"](sPath, "Hello, world!" +
                            "\r\n", aqFile["ctANSI"], false);
  Log["Message"]("The file contents is:");
  Log["Message"](aqFile["ReadWholeTextFile"](sPath, aqFile.ctANSI));
}

DelphiScript and Python support built-in file operations. For VBScript, JScript, C++Script, and C#Script, the situation is different. These languages do not include functions to write or read data files. The solution, as the examples show, is to create FileSystemObject via COM and then use this object to work with the file.

VBScriptCopy Code

' Reading a file
Sub ReadFile(AFileName)
    ForReading = 1
    ForWriting = 2
    ForAppending = 8
    ' Creates a new file object
    Set FS = Sys.OleObject("Scripting.FileSystemObject") 
    Set F = FS.OpenTextFile(AFileName, ForReading)
    While Not F.AtEndOfStream
         s = F.ReadLine
         Log.Message s
    WEnd
    F.Close
End Sub
' Writing to a file
Sub  WriteToFile(AFileName)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0
    Dim fs, f, s
     s = "Hello, world!"
    ' Creates a new file object
    Set  fs = CreateObject("Scripting.FileSystemObject")
    If Not fs.FileExists(AFileName) Then
          Set f = fs.CreateTextFile(AFileName)
    Else
          Set f = fs.OpenTextFile(AFileName, ForAppending, TristateFalse)
    End If
    f.Write s
    f.Close
End Sub

JScriptCopy Code

// Reading a file
function ReadFile(AFileName)
{
    ForReading = 1;
    ForWriting = 2;
    ForAppending = 8;
     // Creates a new file object
    fs = Sys.OleObject("Scripting.FileSystemObject");
    f = fs.OpenTextFile(AFileName, ForReading);
    while (! f.AtEndOfStream)
    {
        s = f.RadLine();
        Log.Message(s);
    }
    f.Close();
}
// Writing to a file
function WriteToFile(AFileName)
{
    ForReading = 1;
    ForWriting = 2;
    ForAppending = 8;
    TristateFalse = 0;
    s = "Hello, world!";
    // Creates a new file object
    fs = new ActiveXObject("Scripting.FileSystemObject")
    if (! fs.FileExists(AFileName))
        f = fs.CreateTextFile(AFileName);
    else
        f = fs.OpenTextFile(AFileName, ForAppending, TristateFalse);
    // Writes string to file
    f.Write(s);
    // Closes the file
    f.Close();
}

PythonCopy Code

# Reading a file
def ReadFile(AFileName):
    ForReading = 1
    ForWriting = 2
    ForAppending = 8
    # Creates a new file object
    fs = Sys.OleObject["Scripting.FileSystemObject"]
    f = fs.OpenTextFile(AFileName, ForReading)
    while not f.AtEndOfStream:
      s = f.ReadLine()
      Log.Message(s)
    f.Close()
# Writing to a file
def WriteToFile(AFileName):
    ForReading = 1
    ForWriting = 2
    ForAppending = 8
    TristateFalse = 0
    s = "Hello, world!"
    # Creates a new file object
    fs = Sys.OleObject["Scripting.FileSystemObject"]
    if not fs.FileExists(AFileName):
        f = fs.CreateTextFile(AFileName)
    else:
        f = fs.OpenTextFile(AFileName, ForAppending, TristateFalse)
    # Writes string to file
    f.Write(s)
    # Closes the file
    f.Close()
    

DelphiScriptCopy Code

// Reading a file
procedure ReadFile(AFileName : string);
var FileVar, s : OleVariant;
begin
 AssignFile(FileVar, AFileName);
 Reset(FileVar);
 while not Eof(FileVar) do
  begin
    Readln(FileVar, s);
    Log.Message(s);
  end
 CloseFile(FileVar);
end
// Writing to a file
procedure WriteToFile(AFileName);
var f, s : OleVariant;
begin
 s := 'Hello, world!';
 AssignFile(f, AFileName);
 if  Utilities.FileExists(AFileName) then
  Append(f)
 else
   Rewrite(f);
    Write(f, s);
    CloseFile(f);
end;

C++Script, C#ScriptCopy Code

// Reading a file
function ReadFile(AFileName)
{
    var ForReading, ForWriting, ForAppending;
    var f, fs, s;
    ForReading = 1;
    ForWriting = 2;
    ForAppending = 8;
     // Creates a new file object
    fs = Sys["OleObject"]("Scripting.FileSystemObject");
    f = fs["OpenTextFile"](AFileName, ForReading);
    while (! f["AtEndOfStream"])
    {
        s = f["ReadLine"]();
        Log["Message"](s);
    }
    f["Close"]();
}
// Writing to a file
function WriteToFile(AFileName)
{
    var ForReading, ForWriting, ForAppending, TristateFalse;
    var s, fs, f;
    ForReading = 1;
    ForWriting = 2;
    ForAppending = 8;
    TristateFalse = 0;
    s = "Hello, world!";
    // Creates a new file object
    fs = Sys["OleObject"]("Scripting.FileSystemObject");
    if (! fs["FileExists"](AFileName))
       f = fs["CreateTextFile"](AFileName);
    else
       f = fs["OpenTextFile"](AFileName, ForAppending, TristateFalse);
    // Writes string to file
    f["Write"](s);
    // Closes the file
    f["Close"]();
}


© 2015 SmartBear Software. All rights reserved.
Email Send feedback on this document