Articles

[Collapse]TestComplete FAQ
 [Expand]Writing Automated Test Script Code
 [Collapse]Working With Files, Databases and Excel Sheets
   Can I work with database fields from tests?/viewarticle/17611/
   Can I connect to any ODBC database to check results?/viewarticle/8917/
   How can I read and write data to and from a file in script?/viewarticle/8918/
   Is it possible to read data from an Excel sheet?/viewarticle/8919/
   How can I execute an expression specified in a cell of the data-driven storage?/viewarticle/23789/
    I am getting the “Parameter object is improperly defined...” error. What should I do?/viewarticle/63537/
 [Expand]Web Testing - Functional Testing of Web Pages
 [Expand]TestComplete Trial Version Questions
 [Expand]Test Log and Results
 [Expand]Supported Applications, Operating Systems and Development Tools
 [Expand]Recording, Running and Debugging Automated Tests
 [Expand]Objects, Files and Images Comparison
 [Expand]Miscellaneous
 [Expand]Licensing Questions/viewarticle/18490/
 [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
Updated: 3/13/2014 Applies To: TestComplete Rating: PoorPoorPoorPoorPoor 13 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 the 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.

  • If you need to get access to the data stored in the XML file, 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 either use 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)
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);
}

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);
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"]);
}

In DelphiScript, file operations are supported by special file functions similar to those of Pascal (see the "DelphiScript Routines" help topic). 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 the 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();
}

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"]();
}


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