Add Method

Applies to TestComplete 14.10, last modified on June 5, 2019

Description

The Add method lets you add the specified file to the Files, Objects or Regions collection and saves it there as an item with the given name.

Declaration

ProgObj.Add(FileNameNameACopyFile)

ProgObj An expression, variable or parameter that specifies a reference to one of the objects listed in the Applies To section
FileName [in]    Required    String    
Name [in]    Optional    String Default value: Empty string   
ACopyFile [in]    Optional    String Default value: False   
Result Boolean

Applies To

The method is applied to the following objects:

Parameters

The method has the following parameters:

FileName

The full or relative path to the file to be added to the collection.

Name

Unique name of the collection item under which the file will be saved in the collection. If this parameter is omitted or set to an empty string, TestComplete will generate the item name itself.

ACopyFile

If this parameter is True, TestComplete will physically copy the specified file to the appropriate Stores subfolder. Otherwise, if this parameter is False (default), TestComplete will add a reference to the original file instead of copying it.

Result Value

If the file was added successfully, the Add method returns True. Otherwise, it returns False and generates an error string, which you can retrieve from the LastError property (and post to the test log if you wish).

If the collection already contains an element with the specified name, the method returns False and does not overwrite the file reference in the project.

Example

The following example demonstrates how to add a file to the Stores | Files collection using the methods of the Files object. This example verifies whether the desired file is already in the Files collection, and if it is not there, adds the file to it.

JavaScript, JScript

function StoreFile()
{
  var FileName = "C:\\MyFile.txt";
  var StoredItemName = "MyFile";

  // Verify that StoredItemName is not taken
  if (Files.Contains(StoredItemName))
  {
    Log.Message ("The Files collection already contains this file.");
    return;
  }

  // Add the file to the Files collection
  if (Files.Add(FileName, StoredItemName))
    Log.Message("The file has been successfully saved in Stores.");
  else
    Log.Message (Files.LastError);
}

Python

def StoreFile():
  FileName = "C:\\MyFile.txt"
  StoredItemName = "MyFile"
  # Verify that StoredItemName is not taken
  if Files.Contains(StoredItemName):
    Log.Message ("The Files collection already contains this file.");
    return
  # Add the file to the Files collection
  if Files.Add(FileName, StoredItemName):
    Log.Message("The file has been successfully saved in Stores.")
  else:
     Log.Message (Files.LastError)

VBScript

Sub StoreFile
  Dim FileName, StoredItemName

  FileName = "C:\MyFile.txt"
  StoredItemName = "MyFile"

  ' Verify that StoredItemName is not taken
  If Files.Contains(StoredItemName) Then
    Log.Message ("The Files collection already contains this file.")
  End If

  ' Add the file to the Files collection
  If Files.Add(FileName, StoredItemName) Then
    Log.Message ("The file has been successfully saved in Stores.")
  Else
    Log.Message (Files.LastError)
  End If
End Sub

DelphiScript

procedure StoreFile;
var FileName, StoredItemName;
begin
  FileName := 'C:\MyFile.txt';
  StoredItemName := 'MyFile';

  // Verify that StoredItemName is not taken
  if Files.Contains(StoredItemName) then
    Log.Message ('The Files collection already contains this file.');

  // Add the file to the Files collection
  if Files.Add(FileName, StoredItemName) then
    Log.Message('The file has been successfully saved in Stores.')
  else
    Log.Message (Files.LastError);
end;

C++Script, C#Script

function StoreFile()
{
  var FileName = "C:\\MyFile.txt";
  var StoredItemName = "MyFile";

  // Verify that StoredItemName is not taken
  if (Files["Contains"](StoredItemName))
  {
    Log["Message"]("The Files collection already contains this file");
    return;
  }

  // Add the file to the Files collection
  if (Files["Add"](FileName, StoredItemName))
    Log["Message"]("The file has been successfully saved in Stores.");
  else
    Log["Message"](Files["LastError"]);
}

See Also

Remove Method
LastError Method

Highlight search results