This topic describes VBScript language specifics and specifics of using VBScript in TestComplete.
Using Set for Object Assignments
If you assign an object to a variable or property, use the
Set statement, for example:
Variables that are declared with the
Dim statement within a routine are only visible within that routine. Variables that are used without declaring them with
Dim, become global variables. This behavior may cause hidden influence of routines onto other routines. For instance, the execution of the following code never ends:
For i = 0 To 5
For i = 0 To 10
This happens because after a call to
i variable is always 5 and never 10, so the loop within the
ParentSub procedure becomes an infinite loop.
Keyword Tests and Variable Assignments
In keyword tests, use the Set Variable Value operation to assign values to variables.
If you prefer Run Code Snippet, put the assignment code into the
Execute("KeywordTests.Test1.Variables.Var1 = KeywordTests.Test1.Variables.Var1 + 5")
This is needed because Run Code Snippet by default treats the equals sign (=) as a comparison operator, not as an assignment operator.
In TestComplete, script units can refer to each other, so you can call routines and variables defined in one unit from another unit. VBScript supports circular references between units, so two units can refer to each other.
VBScript does not support short-circuit logical operations. This means that all parts of a complex condition are evaluated, even if the result is determined by the first condition only. This specific complicates evaluating complex conditions where subsequent conditions rely on the success of the previous ones.
To illustrate this, suppose you have the following code:
Set w = Sys.Process("notepad").WaitWindow("Notepad", "Untitled - Notepad", 1, 1000)
If w.Exists And w.VisibleOnScreen Then
Log.Error("Cannot highlight the Notepad window.")
In case the Notepad window cannot be found (for example, its caption is different from "Untitled - Notepad"), the
w variable will be assigned with a stub object that only has the
Exists property equal to False. However, the
w.VisibleOnScreen property will still be tested, so you will get the following error in the test log:
The object does not exist.
You are trying to call the "VisibleOnScreen" method or property of the "Window("Notepad", "Untitled - Notepad", 1)" object that does not exist.
To workaround this limitation, you can evaluate conditions separately:
If w.Exists Then
If w.VisibleOnScreen Then
Log.Error("The Notepad window is not visible on screen.")
Log.Error("The Notepad window is not found.")
-- or --
Using Classes Defined in Other Units
VBScript allows you to define and use custom classes in your script code. However, in TestComplete VBScript projects, you can create class instances only in the same script unit where the class is defined. Creating a class instance in another unit causes an error. The following code demonstrates the issue:
' Creating class in the same unit
Set cls = New MyClass ' OK!
' Creating class in another unit
Set cls = New MyClass ' Error!
To avoid the problem, you can create a helper routine in the unit, in which the class is defined. This routine will create a class instance and return it. You can then call this routine from another unit and work with the created class instance in a usual manner:
' This routine creates and returns a new class instance
Set GetClassInstance = New MyClass
' Calling a routine defined in UnitA.
' Remember to include this unit with the USEUNIT statement
Set cls = GetClassInstance ' OK !
Private Variables and Routines
Private routines and variables are accessible only within the unit they are declared in. They cannot be accessed from other units. The following example demonstrates this:
Private Sub MyPrivateSub
Log.Message "Hello from private sub!"
Call MyPrivateSub ' OK, because MyPrivateSub is in the same unit
Call MyPrivateSub ' Error!
Call Wrapper ' OK, because Wrapper is public
Private routines are not executed by the Run This Routine command in the Code Editor. However, they are executed when called from other routines of the same unit.
Stop statement has no effect in TestComplete. To pause the script execution and activate the debugger, use the
Runner.Pause method instead.
Working With Extensions
The VBScript engine uses memory optimization. It parses the script before interpreting it and puts all the names to an internal case-insensitive storage. It will use the case of the first written word form.
If, in your script unit, you call a property or method of a script extension’s runtime object and call a routine or variable with the same name as the runtime object’s method or property has, an error may occur:
' Call the TESTVAR variable
TESTVAR = 4
' Call the TestVar property of the RTObj runtime object
RTObj.TestVar = 4
In the example above, the script unit calls the
TestVar property of the runtime object and the
TESTVAR local variable. The VBScript engine will treat both the calls as
TESTVAR as this form is used in the unit first. As a result, the engine will not be able to distinguish the variable name and the runtime object’s property and an error will occur.
To avoid any possible issues related to the mentioned VBScript limitation, do not use similar element names in script extensions and your own custom routines and variables.
If the arrays returned from COM objects (SAFEARRAY objects) contain many items, the debugger panels and dialogs group them by 100 for convenient presentation.