VBScript - Specifics of Usage

Applies to TestComplete 15.69, last modified on November 13, 2024

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:

VBScript

Set p = Sys.Process("MyProcess")  ' Correct
p = Sys.Process("MyProcess")      ' Incorrect !!!

Variable Scope

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:

VBScript

Sub ChildSub
  For i = 0 To 5
    …
  Next
End Sub

Sub ParentSub
  For i = 0 To 10
    ChildSub
  Next
End Sub

This happens because after a call to ChildSub, the 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 statement:

VBScript

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.

Unit References

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.

Logical Operators

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:

VBScript

Sub Test
  Dim w
  Set w = Sys.Process("notepad").WaitWindow("Notepad", "Untitled - Notepad", 1, 1000)

  If w.Exists And w.VisibleOnScreen Then
    Sys.HighlightObject(w)
  Else
    Log.Error("Cannot highlight the Notepad window.")
  End If
End Sub

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:

VBScript

If w.Exists Then
  If w.VisibleOnScreen Then
    Sys.HighlightObject(w)
  Else
    Log.Error("The Notepad window is not visible on screen.")
  End If
Else
  Log.Error("The Notepad window is not found.")
End If

-- or --

VBScript

ok = w.Exists
If ok Then  ok = w.VisibleOnScreen

If ok Then
  Sys.HighlightObject(w)
Else
  Log.Error("Cannot highlight the Notepad window.")
End If

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:

VBScript

[UnitA]
Class MyClass
  ...
End Class
  
Sub TestRoutine
  ...
  ' Creating class in the same unit
  Set cls = New MyClass ' OK!
  ...
End Sub

[UnitB]
'USEUNIT UnitA
Sub AnotherRoutine
  ...
  ' Creating class in another unit
  Set cls = New MyClass ' Error!
  ...
End Sub

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:

VBScript

[UnitA]
Class MyClass
  ...
End Class
  
' This routine creates and returns a new class instance
Function GetClassInstance
  Set GetClassInstance = New MyClass
End Function


[UnitB]
'USEUNIT UnitA
Sub AnotherRoutine
  ...
  ' Calling a routine defined in UnitA.
  ' Remember to include this unit with the USEUNIT statement
  Set cls = GetClassInstance ' OK !

  ...
End Sub

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:

VBScript

[Unit1]
Private Sub MyPrivateSub
  Log.Message "Hello from private sub!"
End Sub

Sub Wrapper
  Call MyPrivateSub ' OK, because MyPrivateSub is in the same unit
End Sub

[MainUnit]
'USEUNIT Unit1
Sub Test
  Call MyPrivateSub ' Error!

  Call Wrapper      ' OK, because Wrapper is public
End Sub

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

VBScript’s 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:

VBScript

Sub Test
  …
  ' Call the TESTVAR variable
  TESTVAR = 4
  …
  ' Call the TestVar property of the RTObj runtime object
  RTObj.TestVar = 4
  …
End Sub

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.

Debugging

If the arrays returned from COM objects (SAFEARRAY objects) contain many items, the debugger panels and dialogs group them by 100 for convenient presentation.

See Also

Specifics of Usage Common for All Languages
Script Tests
About Script Tests
Selecting the Scripting Language

Highlight search results