Using global variables

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.

How to do it...

In order to use a global variable, we need to perform the following steps:

  1. Right-click on the name of the project and select the Edit | Variables menu item. The panel with two types of variable will open up: Persistent Variables and Temporary Variables.
  2. Right-click on the empty area of Persistent Variables and opt for the New Item menu. In the result there will appear a new variable by the name of Var1, and the type of String with other empty fields.
  3. Enter the default string value in the Default Value field, and the local string value in the Local Value field.
    How to do it...
  4. Open any module and create therein the following three functions:
    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);
    }
  5. If the testVariables function is called, we will obtain the following result:
    How to do it...

How it works...

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:

  • Creating a new variable dynamically in the course of script execution, and then changing its value.
  • Demonstrating a possibility for counting the value of the variable, previously changed by another function.
  • We show that in the course of handling variables, it is not necessary to use their full names (for example, 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.

There's more...

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.

Note

Using global variables is considered a bad style in programming, so use it only when there is no other way to achieve the result you need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.59