Keyword Test Variables

Applies to TestComplete 15.47, last modified on January 20, 2023

A keyword test can have a collection of variables that can be used during the test run. These variables are called keyword test variables.

About

Keyword test variables are like 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.

To assign values to variables in keyword tests, you use the Set Variable Value operation. 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.

Unlike project and project suite variables, keyword test variables do not store values between test runs. They are only used during the current test run (like variables in script code).

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 variables using the Add Variable wizard

  1. 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.

  2. 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.

  3. 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.

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

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

The variable will be added to the Variables page of the keyword test. To open the page, click 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 variables using the Variables page

  1. Click Add Variable on the page toolbar.

  2. Specify the variable’s name, type, default value, category and description.

Data types of keyword test variables

A keyword test variable can store values of the following 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 the test run

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

    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: 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

  1. Open the desired operation for editing.

  2. Select the desired parameter in the Operation Parameters dialog.

  3. Select Variable in the Mode column of the dialog.

  4. Select the variable name in the Value column.

  5. 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
Keyword Tests
Keyword Test Editor - Variables Page
Set Variable Value Operation
Variables Property
Variables Object

Highlight search results