Generating Variable Declarations

Applies to TestComplete 15.70, last modified on December 17, 2024

Custom actions created with script extensions can generate script code that uses variables. It is recommended that all variables be declared before they are used. To generate a statement containing a variable declaration, call the Syntax.CreateVarDef method and pass the variable name as the method’s parameter. This method creates and returns a VarDefSyntax object that corresponds to the declaration of the specified variable name. You can then pass this syntax object to the Syntax.GenerateSource method to get the appropriate scripting code as a text.

Note that variable names must meet certain rules:

  • The variable name must be a valid script identifier in all scripting languages supported by TestComplete. That is, it must consist only of letters (A..Z, a..z), digits (0..9) and underscores (_) and must start with a letter.
  • The variable name must not coincide with the names of standard TestComplete objects such as (Sys, Log and others) and objects provided by third-party plugins and script extensions. Also, the variable name must not coincide with the names of intrinsic objects, routines and reserved words of scripting languages supported by TestComplete.

The sample routine below demonstrates how to generate a statement that declares the MyVar variable. The generated code is displayed in a message box.

JScript

function ShowVariableDeclaration()
{
  var oVarDef = Syntax.CreateVarDef("MyVar");

  var strCode = Syntax.GenerateSource(oVarDef);
  aqDlg.ShowMessage(strCode)
}

VBScript

Sub ShowVariableDeclaration
  Dim oVarDef, strCode

  Set oVarDef = Syntax.CreateVarDef("MyVar")

  strCode = Syntax.GenerateSource(oVarDef)
  aqDlg.ShowMessage strCode
End Sub

The generated code is as follows:

JavaScript, JScript

var MyVar;

Python

# returns "pass", because there is no declaration statement in Python

VBScript

Dim MyVar

DelphiScript

var MyVar;

C++Script, C#Script

var MyVar;

See Also

Generating Script Code
Generating Variable Expressions
VarDefSyntax Object
CreateVarDef Method

Highlight search results