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 --
oExcelApp = new ActiveXObject ("Excel.Application");
Set oExcelApp = CreateObject("Excel.Application")
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:
oExcelApp = null;
Set oExcelApp = Nothing
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:
<Category Name="Runtime Objects">
<ScriptExtension Name="Excel Object" Author="SmartBear Software" Version="1.0">
<Script Name="script.vbs" InitRoutine="Initialize" FinalRoutine="Finalize">
These objects cannot be used in the initialization and finalization routines: