Adding Files to Stores

Applies to TestComplete 14.93, last modified on October 12, 2021

If your record-time or design-time action verifies objects, create a baseline copy of the objects with which the action will compare the actual objects. The easiest solution is to save the baseline objects in one of the Stores collections: Files, Objects, Regions, XML, and so on.

Before adding a new item to a particular Stores collection, make sure that the project contains the Stores item with the needed collection in it. To do this, use the Stores.Exists method. This method returns True if the currently active project contains the specified collection, and False if either the project does not have the Stores item or the specified collection does not exist in Stores.

If the needed collection does not exist yet, you can create it directly from the script extension code using the Stores.Create method. This method displays the Create Project Item dialog prompting the user to create the specified collection and returns the dialog result: True, if the user agreed to create the collection, and False if they canceled the action.

After the needed Stores collection has been created, you can add a new item to it. To add a new item to the Files, Regions or Objects collection, use the Add method of the corresponding object: Files, Regions or Objects. To add a new item to the XML collection, use the XML.CreateXML method.

The following example illustrates how to add a file to the Stores | Files collection:

JScript

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

  // Check if the Files collection exists and, if needed, create it
  if ( (! Stores.Exists("Files")) && (! Stores.Create("Files")) )
    return;

  // Verify that StoredItemName is not taken
  if (Files.Contains(StoredItemName))
  {
    aqDlg.ShowError(aqString.Format("The Files collection already contains an item with the name \"%s\".", StoredItemName));
    return;
  }

  // Add the file to the Files collection
  if (Files.Add(FileName, StoredItemName))
    aqDlg.ShowMessage( aqString.Format("\"%s\" has been successfully saved in Stores under the name \"%s\".", FileName, StoredItemName) );
  else
    aqDlg.ShowError(Files.LastError);
}

VBScript

Sub StoreFile
  Dim FileName, StoredItemName

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

  ' Check if the Files collection exists and, if needed, create it
  If Not Stores.Exists("Files") And Not Stores.Create("Files") Then
    Exit Sub
  End If

  ' Verify that StoredItemName is not taken
  If Files.Contains(StoredItemName) Then
    aqDlg.ShowError aqString.Format("The Files collection already contains an item with the name ""%s"".", StoredItemName)
    Exit Sub
  End If

  ' Add the file to the Files collection
  If Files.Add(FileName, StoredItemName) Then
    aqDlg.ShowMessage aqString.Format("""%s"" has been successfully saved in Stores under the name ""%s"".", FileName, StoredItemName)
  Else
    aqDlg.ShowError Files.LastError
  End If
End Sub

See Also

Script Extensions
Creating Script Extensions
Creating Custom Actions

Highlight search results