Working With Project and Project Suite Variables in Scripts

Applies to TestComplete 15.62, last modified on March 19, 2024

Project and project suite variables can be used in scripts and can be created, changed and removed from scripts. You can obtain the Variables program object using the Project.Variables and ProjectSuite.Variables properties. Each of these properties returns the Variables object that provides access to the appropriate collection of variables (project or project suite).

The following sections provide detailed information about how to work with project and project suite variables from TestComplete scripts.

Adding Project and Project Suite Variables

Instead of using the Variables Page, you can add variables of project or project suite types programmatically.

In order to create a project or project suite variable, use the AddVariable method of the appropriate collection (Project.Variables or ProjectSuite.Variables ) and specify the variable name and type:

JavaScript, JScript

Project.Variables.AddVariable("MyExportFilePath", "String");

Python

Project.Variables.AddVariable("MyExportFilePath", "String")

VBScript

Project.Variables.AddVariable "MyExportFilePath", "String"

DelphiScript

Project.Variables.AddVariable('MyFileExportPath', 'String');

C++Script, C#Script

Project["Variables"]["AddVariable"]("MyFileExportPath", "String");

Default values for variables created in scripts are:
  • An empty string for string and password variables.
  • 0 for integer and double variables.
  • False for boolean variables.
  • Empty value (null in JavaScript, JScript, C#Script and C++Script, None in Python, Nothing in VBScript and nil in DelphiScript) for variables holding object references.

After creating a variable, you can set its value and use it in scripts.

After running a script with this line, you can see the new variable in the Variable Page.

Modifying Project and Project Suite Variables

Once a variable has been created, it appears in the Code Completion window under the Project | Variables or ProjectSuite | Variables node.

To address variables in a script, use the object_name.Variables.variable_name syntax. For instance:

JavaScript, JScript

Project.Variables.MyExportFilePath = "C:\\MyFolder\\MyProject.mds";
ExportFileTo(Project.Variables.MyExportFilePath);

Python

Project.Variables.MyExportFilePath = "C:\\MyFolder\\MyProject.mds"
ExportFileTo(Project.Variables.MyExportFilePath)

VBScript

Project.Variables.MyExportFilePath = "C:\MyFolder\MyProject.mds"
ExportFileTo(Project.Variables.MyExportFilePath)

DelphiScript

Project.Variables.MyExportFilePath = 'C:\MyFolder\MyProject.mds';
ExportFileTo(Project.Variables.MyExportFilePath);

C++Script, C#Script

Project["Variables"]["MyExportFilePath"] = "C:\\MyFolder\\MyProject.mds";
ExportFileTo(Project["Variables"]["MyExportFilePath"]);

The script syntax does not depend on the variable type. That is, variables of both types are addressed using the object_name.Variables.variable_name statement, as shown in the example above.

You can also address the variables using the VariableByName property of the Variables object. This feature lets you form the string holding the variable name during the script run and then use this string to obtain the variable:

JavaScript

ProjectSuite.Variables.$set("VariableByName", "MyExportFilePath", "C:\\MyFolder\\MyProject.mds");

JScript

ProjectSuite.Variables.VariableByName("MyExportFilePath") = "C:\\MyFolder\\MyProject.mds";

Python

ProjectSuite.Variables.VariableByName["MyExportFilePath"] = "C:\\MyFolder\\MyProject.mds"

VBScript

ProjectSuite.Variables.VariableByName("MyExportFilePath") = "C:\MyFolder\MyProject.mds"

DelphiScript

ProjectSuite.Variables.VariableByName['MyExportFilePath'] = 'C:\MyFolder\MyProject.mds';

C++Script, C#Script

ProjectSuite["Variables"]["VariableByName"]("MyExportFilePath") = "C:\\MyFolder\\MyProject.mds";

We would like to remind you that project and project suite variables are Read/Write, so you can modify them from script code.

Note also that TestComplete does not restore the initial values of the project and project suite variables when the script has finished. Therefore, if your script modifies the project or project suite variable, the variable will not have the initial value the next time you run the script. In this case, the variables of simple types (integer, double, string, Boolean) would have the modified value, while the object type variables would have an empty value.

Since project and project suite variables of simple data types are set up before running a script, they can be used to specify parameters for the project’s test run. See How to Specify Parameters for the Test Run.

Obtaining Values of Project and Project Suite Variables

The Variables object also contains methods that let you get information about project and project suite variables, such as the variable name, type, category and description. These are the GetVariableName, GetVariableType, GetVariableCategory and GetVariableDescription methods. The GetVariableName method takes the variable’s index within the collection as a parameter. GetVariableType, GetVariableCategory and GetVariableDescription can take the variable’s index or its name as a parameter. All of these methods return string values.

The total number of the project or project suite variables is returned by the VariableCount property of the Project.Variables and ProjectSuite.Variables collections respectively.

The following example posts the list of project variables and their properties to the log:

JavaScript, JScript

function Main ()
{
  var i, id, name;
  for (i = 0; i < Project.Variables.VariableCount; i++)
  {
    name = Project.Variables.GetVariableName(i);
    id = Log.CreateFolder(name);
    Log.PushLogFolder(id);
    Log.Message("Type: " + Project.Variables.GetVariableType(i));
    Log.Message("Value: " + Project.Variables.VariableByName(name));
    Log.Message("Category: " + Project.Variables.GetVariableCategory(i));
    Log.Message("Description: " + Project.Variables.GetVariableDescription(i));
    Log.PopLogFolder();
  }
}

Python

def Main():
  for i in range(Project.Variables.VariableCount):
    name = Project.Variables.GetVariableName(i)
    id = Log.CreateFolder(name)
    Log.PushLogFolder(id)
    Log.Message("Type: " + Project.Variables.GetVariableType(i))
    Log.Message("Value: " + str(Project.Variables.VariableByName[name]))
    Log.Message("Category: " + Project.Variables.GetVariableCategory(i))
    Log.Message("Description: " + Project.Variables.GetVariableDescription(i))
    Log.PopLogFolder()

VBScript

Sub Main
  For i = 0 To Project.Variables.VariableCount - 1
    name = Project.Variables.GetVariableName(i)
    id = Log.CreateFolder(name)
    Log.PushLogFolder(id)
    Log.Message "Type: " + Project.Variables.GetVariableType(i)
    Log.Message "Value: " + CStr(Project.Variables.VariableByName(name))
    Log.Message "Category: " + Project.Variables.GetVariableCategory(i)
    Log.Message "Description: " + Project.Variables.GetVariableDescription(i)
    Log.PopLogFolder
  Next
End Sub

DelphiScript

procedure Main;
var
  i, id : integer;
  name : string;
begin
  for i:=0 to Project.Variables.VariableCount-1 do
  begin
    name := Project.Variables.GetVariableName(i);
    id := Log.CreateFolder(name);
    Log.PushLogFolder(id);
    Log.Message('Type: ' + Project.Variables.GetVariableType(i));
    Log.Message('Value: ' + aqConvert.VarToStr (Project.Variables.VariableByName[name]));
    Log.Message('Category: ' + Project.Variables.GetVariableCategory(i));
    Log.Message('Description: ' + Project.Variables.GetVariableDescription(i));
    Log.PopLogFolder();
  end;
end;

C++Script, C#Script

function Main () {
  var i, id, name;
  for (i=0; i<Project["Variables"]["VariableCount"]; i++)
  {
    name = Project["Variables"]["GetVariableName"](i);
    id = Log["CreateFolder"](name);
    Log["PushLogFolder"](id);
    Log["Message"]("Type: " + Project["Variables"]["GetVariableType"](i));
    Log["Message"]("Value: " + Project["Variables"]["VariableByName"](name));
    Log["Message"]("Category: " + Project["Variables"]["GetVariableCategory"](i));
    Log["Message"]("Description: " + Project["Variables"]["GetVariableDescription"](i));
    Log["PopLogFolder"]();
  }
}

Removing Project and Project Suite Variables

To delete a variable from the project or project suite variables collection, use the RemoveVariable method of the appropriate collection (Project.Variables or ProjectSuite.Variables) with the variable name as a parameter:

JavaScript, JScript

Project.Variables.RemoveVariable("MyExportFilePath");

Python

Project.Variables.RemoveVariable("MyExportFilePath")

VBScript

Project.Variables.RemoveVariable "MyExportFilePath"

DelphiScript

Project.Variables.RemoveVariable('MyFileExportPath');

C++Script, C#Script

Project["Variables"]["RemoveVariable"]("MyFileExportPath");

See Also

Project And Project Suite Variables
About Project And Project Suite Variables
Variables Page
Working With Project and Project Suite Variables in Keyword Tests

Highlight search results