Applies to TestComplete 14.50, last modified on August 05, 2020

Each keyword test has a collection of variables that can be used during the test run. These variables are called keyword test variables. Below is detailed information on working with these variables.

Overview

Each keyword test may use one or several variables. These variables are similar to variables that you use in script functions. They store various values during the test execution and pass data from one operation to another. Variables can also be used to store the result of the keyword test.

TestComplete provides a specific operation to assign values to a keyword test variable. After you assigned the value, you can then use the variable to specify values of operation parameters. You can also obtain and set values of keyword test variables from scripts.

The keyword test variables are accessible from any operation of the test, to which they belong. They are accessible from scripts, but they are not accessible from other keyword tests.

The concept of keyword test variables is similar to the concept of local variables of the project, project suite or network suite types. However, there is a difference: unlike these variables, keyword test variables do not store values between test runs. They are only used during one test run (like variables in script code). To store values between test runs, use the project, project suite or network suite variables.

Create keyword test variables

You can create variables of keyword tests using the Add Variable wizard or Variables page of the Keyword Test editor.

To add a new variable to a keyword test using the Add Variable wizard:

  • Click the Add Variable button on the toolbar of the Keyword Test editor or the Add Variable button in the Set Variable Value wizard or in the Edit Value and Edit Parameter dialog (the dialog will appear when specifying values to parameters of keyword test operations). The Add Variable wizard will appear.

  • On the first page of the wizard, specify where the variable will be added. It may be the current keyword test, project, project suite or network suite. Note that this page is only available if the wizard is called by clicking the Add Variable button in the Edit Value or Edit Parameter dialog. If you call the Add Variable wizard in the other way, the next page will be first.

  • On the next page of the wizard, specify the variable properties needed for variable creation:

    • In the Name edit box, specify the variable name.

      Note: The variable name is used to address the variable in scripts, so it must match the naming rules of the scripting language used in your project. The easiest way to follow this rule is to enter a name that only consists of alphanumeric or underscore characters and starts with a letter.
    • In the Type box, specify the variable’s type.

    • In the Description edit box, enter any descriptive text related to the variable.

  • Click Next to specify the variable's default value if needed.

  • Click Finish to close the dialog and add a new variable to a keyword test. Cancel will close the dialog without performing any actions.

After creating a variable using the wizard, it will appear on the Variables page of the Keyword Test editor. To display the page, open your keyword test for editing and select Variables at the bottom of the editor.

The Variables page is a table that contains information about existing variables. The table contains columns that provide information about the variables’ name, type, default value, category and descriptive text. For detailed information about the columns, see description of the page.

To add a new variable to a keyword test using the Variables page:

  • Right-click somewhere within the page and choose New Item from the context menu.

  • Then you can specify the variable’s name, type, default value, category and description.

To modify and delete keyword test variables, use the Variables page.

Data types of keyword test variables

A keyword test variable can have one of the following data types:

  • String - Use this type for variables that will store characters and strings (including multi-line text).

  • Integer - Use this type for variables that will store integer values.

  • Double - Use this type for variables that will store floating-point values and dates.

  • Boolean - Use this type for variables that will store True or False.

  • Password - Use this type for variables that will store encrypted strings (passwords or other sensitive data).

  • Object - Use this type for variables that will store object references (for instance, references to the process or window objects).

  • Table - Use this type for variables that will store a two-dimensional array of values. You can see the contents of such variables on the Test Steps page of the Keyword Test editor.

  • DB Table - Use this type for variables that will store tabular data from an external storage (a file or a database).

TestComplete checks the variable type when values are assigned to the variables. See the next section for details.

Set variable values

You can assign values to keyword test variables before the test run or during it.

  • Assign values before the test run

    To assign a value before the test run, specify the desired data in the Default Value column of the Variables page. TestComplete will assign this value to the variable when it starts executing the test to which this variable belongs.

  • Assign values during test execution

    To assign a value to a keyword test variable during the test execution, you can use any of the following:

    • Use the Set Variable Value operation (it belongs to the Test Actions category). In the Operation Parameters dialog you specify the name of the desired variable and its value. For detailed information, see Specifying Operation Parameters.

    • Call a script code that will change the variable. To access the variable from a script, you use the Variables property of the KeywordTest object that provides a scripting interface to your test. For detailed information about this, see the Working With Keyword Test Variables From Scripts section below.

      To call a script from your keyword test, you can use the Run Script Routine, Run Code Snippet or Run Test operations.

      Note:

      In VBScript projects, the Run Code Snippet operation treats the equals sign (=) as a comparison operator by default. To treat it as an assignment operator, put the assignment code into the Execute statement:

      VBScript

      Execute("KeywordTests.Test1.Variables.Var1 = 10")

    The script code variant lets you use all of the scripting languages features to check additional conditions and calculate the needed value. However, by using this operation you can also assign those values that cannot be assigned from scripts: values of test parameters and the result of the previous operation. The latter is useful if you need to check the result or store it for further processing (see Checking Operation Result).

When you assign a value to the variable (with script code or with the operation), TestComplete checks whether this value has the same type as the variable. If not, TestComplete attempts to typecast the value to the variable’s type. If this attempt fails, an error occurs.

A typical example that illustrates this statement is assignment of string values to integer variables. Suppose, you have a variable of the integer type. If you assign the string "77" to it, the variable will hold the number 77 (the string "77" can be easily converted to the numerical value 77). However, if you attempt to assign the string "qwerty", the assignment will fail and an error will occur.

Note that when converting string values to the numerical format, TestComplete implies that the string contains the numbers in decimal format. Hexadecimal, octal and binary formats are not supported.

Use variables to specify operation parameters

Variables are used to specify operation parameters. To do this:

  • Open the desired operation for editing.

  • Select the desired parameter in the Operation Parameters dialog.

  • Select Variable in the Mode column of the dialog.

  • Select the variable name in the Value column.

  • Press Enter to confirm the change.

Now, when executing the operation, TestComplete will pass the value of the specified variable to the parameter.

Work with keyword test variables in scripts

To access keyword test variables from scripts, use the Variables property of the KeywordTest scripting object that corresponds to your test. This property provides a scripting interface to the test’s variable. To address a variable, you can use the following syntax:

KeywordTests.Test_Name.Variables.Variable_Name
 -- or --
KeywordTests.Test_Name.Variables.VariableByName(Variable_Name)

The following code snippet demonstrates how you can get and set the variable MsgVar of the test named KeywordTest1:

JavaScript

let s;
 
// Get the variable value
    s = KeywordTests.KeywordTest1.Variables.MsgVar;
// -- or --
    s = KeywordTests.KeywordTest1.Variables.VariableByName("MsgVar");
  
// Set the variable value
    KeywordTests.KeywordTest1.Variables.MsgVar = "New Value";
// -- or --
    KeywordTests.KeywordTest1.Variables.$set("VariableByName", "MsgVar", "New Value");

JScript

var s;
 
// Get the variable value
    s = KeywordTests.KeywordTest1.Variables.MsgVar;
// -- or --
    s = KeywordTests.KeywordTest1.Variables.VariableByName("MsgVar");
  
// Set the variable value
    KeywordTests.KeywordTest1.Variables.MsgVar = "New Value";
// -- or --
    KeywordTests.KeywordTest1.Variables.VariableByName("MsgVar") = "New Value";

Python

# Get the variable value 
s = KeywordTests.KeywordTest1.Variables.MsgVar
# -- or --
s = KeywordTests.KeywordTest1.Variables.VariableByName["MsgVar"]
  
# Set the variable value
KeywordTests.KeywordTest1.Variables.MsgVar = "New Value"
# -- or --
KeywordTests.KeywordTest1.Variables.VariableByName["MsgVar"] = "New Value"

VBScript

' Get the variable value
   s = KeywordTests.KeywordTest1.Variables.MsgVar
' -- or --
   s = KeywordTests.KeywordTest1.Variables.VariableByName("MsgVar")
  
' Set the variable value
   KeywordTests.KeywordTest1.Variables.MsgVar = "New Value"
' -- or --
   KeywordTests.KeywordTest1.Variables.VariableByName("MsgVar") = "New Value"

DelphiScript

var
  s : OleVariant;
begin
  // Get the variable value
      s := KeywordTests.KeywordTest1.Variables.MsgVar;
  // -- or --
      s := KeywordTests.KeywordTest1.Variables.VariableByName('MsgVar');
    
  // Set the variable value
      KeywordTests.KeywordTest1.Variables.MsgVar := 'New Value';
  // -- or --
      KeywordTests.KeywordTest1.Variables.VariableByName('MsgVar') := 'New Value';
end;

C++Script, C#Script

var s;
 
// Get the variable value
    s = KeywordTests["KeywordTest1"]["Variables"]["MsgVar"];
// -- or --
    s = KeywordTests["KeywordTest1"]["Variables"]["VariableByName"]("MsgVar");
  
// Set the variable value
    KeywordTests["KeywordTest1"]["Variables"]["MsgVar"] = "New Value";
// -- or --
    KeywordTests["KeywordTest1"]["Variables"]["VariableByName"]("MsgVar") = "New Value";

Note: Changing the variable before the script execution will not affect the test, because when the test begins, all of the test variables are assigned the values that are specified in the Initial Value column of the Variables page.

Use variables to output data from test

A keyword test can be a part of a larger test. The keyword test variables are not deleted when the keyword test is over, they exist until the larger test is finished. This feature lets you use keyword test variables to pass data out of the test. For instance, you can check the test result. To do this, in your test you assign the output value to a keyword test variable and write script code that will be executed after the test and that will retrieve and check the variable value. For detailed information about this, see Setting and Checking a Keyword Test Result.

See Also

Keyword Tests
About Keyword Testing
Keyword Test Editor - Variables Page
Set Variable Value Operation
Variables Property
Variables Object

Highlight search results