Creating Initialization and Finalization Routines

Applies to TestComplete 14.40, last modified on April 22, 2021

Individual scripts in script extensions can include initialization and finalization routines. The initialization routine of a script is executed when TestComplete loads the script extension in memory (this happens when you call the extension script for the first time after TestComplete starts or after you click Reload in the Install Script Extension dialog), the finalization routine - just before the extension is unloaded.

Note: Custom keyword test operations created with script extensions may have individual initialization routines. The operation’s initialization routine is executed when TestComplete creates a new instance of the operation and is usually used to specify default values of operation’s parameters and fields. For more information, see Creating Operation Initialization Routine.

In the initialization routine, you can perform some preliminary actions, for example:

  • Create instances of global helper objects, such as ActiveX objects and COM servers.
  • Initialize values of runtime object’s properties,
  • And so on.

The finalization routine, in its turn, can perform clean-up actions, for example:

  • Release references to objects,
  • Free resources allocated within the initialization routine,
  • And so on.

The initialization and finalization routines must be defined in the description.xml file. To do this, you specify the routine names in the InitRoutine and FinalRoutine attributes of the Script element that corresponds to the script file where the routines are implemented.

To illustrate how you can use initialization and finalization, suppose that your script extension provides a runtime object that automates Microsoft Excel via its COM server -- Excel.Application. Before performing any actions over Excel, you need to obtain a reference to its COM server. This is just what you can do in the initialization routine --

JScript

// script.js

var oExcelApp;

function Initialize()
{
  oExcelApp = new ActiveXObject ("Excel.Application");
}

VBScript

' script.vbs

Dim oExcelApp

Sub Initialize
  Set oExcelApp = CreateObject("Excel.Application")
End Sub

From now on, the script extension’s services can refer to Excel to perform the desired actions.

At the moment when the script extension is about to be unloaded, program access to Excel is no longer needed. So, it is time to quit Excel and release the stored reference to it. This code can be placed in the finalization routine:

JScript

// script.js

function Finalize()
{
  oExcelApp.Quit();
  oExcelApp = null;
  CollectGarbage();
}

VBScript

' script.vbs

Sub Finalize
  oExcelApp.Quit
  Set oExcelApp = Nothing
End Sub

In order for TestComplete to execute the initialization and finalization routines upon loading or unloading the extension, the routine names must be specified in the description.xml file. The example below shows how this can be done:

XML

<ScriptExtensionGroup>
    <Category Name="Runtime Objects">
        <ScriptExtension Name="Excel Object" Author="SmartBear Software" Version="1.0">
            <Script Name="script.vbs" InitRoutine="Initialize" FinalRoutine="Finalize">
                ...
            </Script>
        </ScriptExtension>
    </Category>
</ScriptExtensionGroup>

Remarks

These objects cannot be used in the initialization and finalization routines:

  • Indicator
  • Log
  • Runner

See Also

Script Extensions
Creating Script Extensions
Structure of the Description File
Script Element
Creating Operation Initialization Routine

Highlight search results