How to Read a Text File and Make an Object in Javascript

Migrating to Jira Cloud? Join Dec ist webinar for strategies & tips. Learn more

Become a Zephyr Calibration All-Star, contribute to production development, and get exclusive benefits! Learn more

In that location are at to the lowest degree three different ways to read data from a text file. First of all, you can use the special aqFile and aqTextFile objects that are provided by TestComplete. Second, you tin can obtain the Scripting.FileSystemObject object and use its methods to piece of work with any file system object, including text files. For a complete description of the object and its methods, see the documentation on the FileSystemObject object in the MSDN library. Finally, you lot tin use native routines of DelphiScript (a list of the routines is given in the File and Binder Routines topic). The offset two techniques are more versatile, since the corresponding objects are bachelor for all the supported scripting languages.

The sections below demonstrate each of the above-mentioned techniques:

  • Reading a text file using the aqFile and aqTextFile objects

    The ReadWholeFile routine reads text from a file as a single string and posts it to the examination log. The ReadFileLines routine reads text line by line and posts the retrieved lines to the log.

    JavaScript, JScript

    office ReadWholeFile(AFileName)
    {
    var southward = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI);
      Log.Bulletin("File entire contents:");
      Log.Message(south);
    }

    function ReadFileLines(AFileName)
    {
    var F, s;

         F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI);
      F.Cursor = 0;
      Log.Message("File by lines:");
    while(! F.IsEndOfFile()){
        southward = F.ReadLine();
        Log.Message(s);
      }
      F.Shut();
    }

    Python

                          def ReadWholeFile(AFileName):   due south = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI)   Log.Message("File entire contents:")   Log.Message(s)  def ReadFileLines(AFileName):   F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI)   F.Cursor = 0   Log.Message("File by lines:")   while not F.IsEndOfFile():     s = F.ReadLine()     Log.Message(south)   F.Close()                    

    VBScript

    Sub ReadWholeFile(AFileName)
      south = aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI)
      Log.Message("File entire contents:")
      Log.Bulletin(s)
    EndSub

    Sub ReadFileLines(AFileName)
    Ready F = aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI)
      F.Cursor = 0
      Log.Message("File by lines:")
    WhileNot F.IsEndOfFile()
        s = F.ReadLine()
        Log.Message(southward)
    WEnd
      F.Close()
    EndSub

    DelphiScript

    process ReadWholeFile(AFileName);
    var due south: Cord;
    begin
      south := aqFile.ReadWholeTextFile(AFileName, aqFile.ctANSI);
      Log.Bulletin('File unabridged contents:');
      Log.Message(southward);
    end;

    procedure ReadFileLines(AFileName);
    var F: OleVariant;
        south: String;
    begin
      F := aqFile.OpenTextFile(AFileName, aqFile.faRead, aqFile.ctANSI);
      F.Cursor := 0;
      Log.Message('File by lines:');
    whilenot F.IsEndOfFile() do
    begin
          s := F.ReadLine();
          Log.Bulletin(due south);
    end;
      F.Close();
    end;

    C++Script, C#Script

    function ReadWholeFile(AFileName)
    {
    var s;
      s = aqFile["ReadWholeTextFile"](AFileName, aqFile["ctANSI"]);
      Log["Message"]("File entire contents:");
      Log["Message"](s);
    }

    function ReadFileLines(AFileName)
    {
    var F, south;

         F = aqFile["OpenTextFile"](AFileName, aqFile["faRead"], aqFile["ctANSI"]);
      F.Cursor = 0;
      Log["Bulletin"]("File by lines:");
    while(! F["IsEndOfFile"]()){
        s = F["ReadLine"]();
        Log["Bulletin"](s);
      }
      F["Close"]();
    }

  • Reading a text file using FileSystemObject

    The following routine reads text lines from a file and posts them to the examination log.

    JavaScript

    function ReadFile(AFileName)
    {
    const ForReading = 1;
    const ForWriting = two;
    const ForAppending = 8;
    let FS = getActiveXObject("Scripting.FileSystemObject");

    allow F = FS.OpenTextFile(AFileName, ForReading);
    while(! F.AtEndOfStream){
    let s = F.ReadLine();
        Log.Message(s);
      }
      F.Close();
    }

    JScript

    office ReadFile(AFileName)
    {
    var ForReading = 1;
    var ForWriting = 2;
    var ForAppending = eight;
    var FS = Sys.OleObject("Scripting.FileSystemObject");
    // Note that y'all can create FileSystemObject
    // using the post-obit code:
    // var FS = new ActiveXObject("Scripting.FileSystemObject")
    var F = FS.OpenTextFile(AFileName, ForReading);
    var s;
    while(! F.AtEndOfStream){
        s = F.ReadLine();
        Log.Message(due south);
      }
      F.Close();
    }

    Python

                          def ReadFile(AFileName):   ForReading = 1   ForWriting = two   ForAppending = eight   FS = Sys.OleObject["Scripting.FileSystemObject"]   F = FS.OpenTextFile(AFileName, ForReading)   while non F.AtEndOfStream:     south = F.ReadLine()     Log.Message(s)    F.Close()                    

    VBScript

    Sub ReadFile(AFileName)
      ForReading = i
      ForWriting = 2
      ForAppending = viii
    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

    DelphiScript

    process ReadFile(AFileName : Cord);
    const
      ForReading = 1;
      ForWriting = 2;
      ForAppending = 8;
    var
      FS, f, s : OleVariant;
    begin
      FS := Sys.OleObject('Scripting.FileSystemObject');
      F := FS.OpenTextFile(AFileName, ForReading);
    while not F.AtEndOfStream do
    brainstorm
        s := F.ReadLine;
        Log.Message(due south);
    end;
      F.Close;
    end;

    C++Script, C#Script

    office ReadFile(AFileName)
    {
    var FS, F, s;
    var ForReading = 1;
    var ForWriting = two;
    var ForAppending = viii;
      FS = Sys["OleObject"]("Scripting.FileSystemObject");
    // Note that y'all can create FileSystemObject
    // using the following lawmaking:
    // FS = new ActiveXObject("Scripting.FileSystemObject")
      F = FS["OpenTextFile"](AFileName, ForReading);
    while(! F["AtEndOfStream"])
      {
        s = F["ReadLine"]();
        Log["Message"](s);
      }
      F["Close"]();
    }

  • Reading a text file using DelphiScript routines

    The following script routine reads text lines from a file and posts them to the test log.

    DelphiScript

    procedure ReadFile(AFileName : String);
    var
      FileVar, s : OleVariant;
    begin
      AssignFile(FileVar, AFileName);
      Reset(FileVar);
    while not Eof(FileVar) practice
    begin
        Readln(FileVar, southward);
        Log.Bulletin(s);
    end;
      CloseFile(FileVar);
    cease;

See As well

Working With External Data Sources
Processing Files in a Folder
Getting File Size
About File Checkpoints

mclinthimandind.blogspot.com

Source: https://support.smartbear.com/testcomplete/docs/testing-with/working-with-external-data-sources/text.html

0 Response to "How to Read a Text File and Make an Object in Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel