2. Creating the Script Unit

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

Every script extension includes one or more JScript or VBScript units that contain the script code implementing the extensions functionality. To create script units, you can use any tool or editor. However, it is convenient to use TestComplete to create them, because TestComplete offers a full-featured Code Editor with the Code Completion window that lets you quickly find the objects, methods and properties to be used in scripts. So, in this tutorial we will create script units with TestComplete.

Script extensions can use the JScript or VBScript language, or both. The language of the script extensions does not depend on the scripting language of the test projects, in which the extension will work. The extensions work well in any project. In this tutorial, we will use VBScript.

Creating a Source Folder

First of all, we need to create a folder that will contain the script extension’s files. To do this:

  1. Open Windows Explorer or any other file manager and create the folder that will contain the files. We will create the C:\My Extension Files\Extension1 folder.

    Placing the script extensions file into one folder will help you debug script code and pack the files into one .tcx module.

  2. In the folder, create a new file and call it wmiCode.vbs. This file will contain the implementation of our script object.

    It is important that the script file has the .vbs extension, since TestComplete uses the extension to determine the script language of the script unit. For VBScript units, the extension should be .vbs, for JScript - .js.
Creating a TestComplete Project

Now let’s create a TestComplete project where we will keep and edit the source files of the script extension.

First, we will create a project suite:

  1. Select File | New | New Project Suite from the TestComplete main menu. The Create Project Suite dialog will open.

  2. In the dialog, specify a project suite name and location and clickOK.

TestComplete will create an empty project suite in the specified location.

Now let’s add a project to this suite:

  1. Select File | New | New Item from the TestComplete main menu. The Create Project dialog will open.

  2. Specify the desired project name and path in the Project name and Location text boxes.

  3. Select VBScript in the Language box.

  4. Clear the Generate the default content check box.

  5. Click OK.

TestComplete will create an empty project.

A blank project in the Project Explorer

Adding Scripts

Earlier, we have created the wmiCode.vbs file that will contain the source code of our custom keyword test operation. Let’s import this file to the project so that we can write the code in the TestComplete Code Editor. But before we do that, we need to add the Scripts collection to our project:

  1. Right-click the project in the Project Explorer and select Add | New Item from the context menu. The Create Project Item dialog will open.

  2. In the dialog, select the Script item.

  3. Select Generate the default content.

  4. Click OK.

TestComplete will add the Scripts collection with the Unit1 unit to the project. Do not delete this unit, as we will use it later to test our WMI object.

Now we can import the wmiCode.vbs file to the project:

  1. Right-click the Script project item and choose Add | Existing Item from the context menu. This will invoke the standard Open File dialog.

  2. In the dialog, navigate to the C:\My Extension Files\Extension1 folder.

  3. Select All Files (*.*) in the Files of type box.

  4. Choose the wmiCode.vbs unit and click Open.

The resulting project looks like the image below.

Script Extension Project in the Project Explorer

Make sure the wmiCode unit is clean. Do not use the 'USEUNIT statement in it, because units of script extensions cannot refer to each other.

Script files in script extensions must use the ANSI or UTF-8 character encoding. So, we need to specify the desired encoding for script units included in the project:

  1. Right-click the project node in the Project Explorer and select Edit | Properties from the context menu.

    TestComplete will display the project editor in the Workspace panel and will activate the Properties page of the editor.

  2. Select the General category in the tree on the left, and then specify the desired format -- ANSI or UTF-8 -- in the Units encoding box.

  3. Close the project editor.

Prev     Next

See Also

Creating Projects and Project Suites
Adding and Removing Project Items and Their Child Elements

Highlight search results