5. Creating Initialization Code

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

Custom scripting objects that are created with the script extension technology do not have constructors and destructors. The initialization and finalization code is included into the initialization and finalization routines that are executed when the script unit is loaded to and unloaded from memory. The script functions, which will be used as the initialization and finalization routines, are specified by the InitRoutine and FinalRoutine attributes of the ScriptExtension element in the description.xml file.

So, the creation of the initialization and finalization routines includes two steps:

  1. Writing the routine’s code.

  2. Specifying the routine as the initialization or finalization routine in the description file.

In this topic we will only describe the creation of the routine’s code. As for the editing of the description file, we will explain this in the next topic.

Our WMI object requires the following initialization actions:

  • Since the WMI object is a wrapper over the operating system’s SWbemServices object, we need to obtain a reference to this object.

  • We need to specify the default value for the ComputerName and MaxEventCount methods of the WMI object.

Let’s write the routine that will perform these actions. We will call this routine Initialize. Open the wmiCode unit for editing and add the following code to it:

VBScript

[wmiCode.vbs]

Const CLocalComputer = "."
Const CDefaultEventCount = 50

...

Sub Initialize
  MaxEventCount = CDefaultEventCount
  ComputerName = CLocalComputer
  ConnectToComputerInternal Null, Null
End Sub

As you can see, the Initialize routine stores the default values of the WMI object’s ComputerName and MaxEventCount properties in the global script variables of the same name. The default values are specified by script constants defined at the beginning of the script. The default value assigned to the ComputerName variable - a dot (".") - means the local computer.

The Initialize routine also calls the helper ConnectToComputerInternal routine that we created in the previous step. This routine connects to the WMI services on the specified computer using the provided user name and password, obtains the SWbemServices object that provides access to the WMI services and stores a reference to this object in the WMIService variable.

We have now created the initialization code, but now we need to create the description file that will describe our object and extension and specify the Initialize routine as the initialization routine of the wmiCode unit.

One note before we proceed: in this tutorial, we will only create the initialization code. The finalization code is not needed, because the SWbemServices object reference will be released automatically when the WMIService variable is destroyed. However, when creating your own script extensions, you may need to write finalization code to release some resources or close files that are not released or closed automatically.

Prev     Next

See Also

Creating Runtime Objects
Creating Runtime Objects - Basic Concepts

Highlight search results