Usually, to pass data between functions, parameters are used. However, this isn't always possible. In such instances, global variables come in mighty handy. These go by the names of project variables and project suite variables in TestComplete.
In order to use a global variable, we need to perform the following steps:
default string
value in the Default Value field, and the local string
value in the Local Value field.function testVariables() { testExistingVars(); testNewVars(); } function testExistingVars() { Log.Message(Project.Variables.Var1); Log.Message(Project.Variables.GetVariableDefaultValue("Var1")); Project.Variables.Var1 = "new string"; Log.Message(Project.Variables.Var1); } function testNewVars() { Project.Variables.AddVariable("Var2", "Integer"); Project.Variables.Var2 = 123; var variable1 = Project.Variables.Var1; var variable2 = Project.Variables.Var2; Log.Message(variable1); Log.Message(variable2); }
testVariables
function is called, we will obtain the following result:In TestComplete there are two types of variables: persistent and temporary. Values of the persistent variable are saved even if the TestComplete is closed and re-opened anew. The values of the temporary variables exist only at the point of working of the scripts. On the next script run their values will be set to default.
Persistent variables can be of four types: Integer
, Boolean
, String
, and Double
. For temporary variables two additional types are available: Table
and Object
.
First, we have created the persistent variable Var1
with the help of the TestComplete editor, assigning it with the default value and the current (local) value. The default value is needed in case several people are working with the same project, the ad hoc value in view will be the same across all the computers.
The function testVariables
is needed only for the purpose of demonstrating of the fact that values of the variable may differ in one function, and then correctly show up in another.
In the testExistingVars
function, we are working with the created variable Var1
, first outputting its value and the default value to the log, and then changing its value and outputting the same to the log again.
In the testNewVars
function, we consider several possibilities of handling the variable at once:
Project.Variables.Var1
). And for brevity's sake, just assign the value of the newly created variable to the global variable and work with the latter, in turn. Meanwhile, changes of the new variable in no way affect the global variable.Apart from the project variables, there exist project suite level variables. Working with them is no different than working with the variables of the project, with the only reservation for accessing. To access project suite level variables from scripts, one should use the code ProjectSuite.Variables
instead of Project.Variables
.
Similarly, to work with these variables in TestComplete itself, it is necessary to select the Edit | Variables menu from the Project Suite context menu, and not from the project. The advantage of using the variables of this type lies in the possibility of handling one and the same variable from different projects (even those using different programming languages!), and so passing data in-between.
3.15.137.59