Add Method

Applies to TestComplete 14.93, last modified on September 27, 2021

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.

PDF files will be added to the Files collection as binary files. That is, their text content will not be recognized.

Declaration

ProgObj.Add(FileName, Name, ACopyFile)

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 under which the file will be added to the collection. If this parameter is omitted or set to an empty string, TestComplete will generate the item name itself.

This name will be used to access the added file in the collection.

ACopyFile

If this parameter is True, TestComplete will create a physical copy of the specified file and place it in the appropriate Stores subfolder.

Otherwise, TestComplete will add a reference to the original file. (This is the default behavior.)

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 existing element.

Example

The following example demonstrates how to add a file to the Stores > Files collection using the Files.Add method. The code checks whether the file is already in the 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