Creating Scripting Objects From Visual Basic .NET Application Objects

Applies to TestComplete 15.63, last modified on April 10, 2024
Information in this topic applies to desktop applications only.
Connected and Self-Testing applications are deprecated. These technologies will be removed in one of the future releases of TestComplete.
To create test code that runs from within your tested apps, use TestLeft, a SmartBear functional testing tool for developers.

This topics explains how you can expose objects of Visual Basic .NET applications as scripting objects in TestComplete. To learn how you can create scripting objects from objects of Visual Basic 6.0 applications, see Creating Scripting Objects From Visual Basic 6.0 Application Objects.

Two notes before you proceed:

  • Objects of Visual Basic .NET applications can be exposed in this way only if .NET support is enabled in TestComplete. That is, the .NET Application Support plugin must be installed and enabled in File | Install Extensions. In addition, the application should not be launched without the NotOpenApp command-line argument.

  • TestComplete must be launched before your Visual Basic .NET application in order for the latter to be able to expose its objects.

Preparing Visual Basic .NET Project

In order to be able to expose objects of Visual Basic .NET applications as TestComplete scripting objects, you need to get access to the TestComplete COM server. The easiest way to manage TestComplete via COM is to transform your application into a Connected Application. Visual Basic .NET Connected Applications are created by linking the <TestComplete>\Connected Apps\.NET\AutomatedQA.TestComplete.VBConnectedApp.dll assembly to the project. This assembly provides objects and routines that free you from having to manually write the code needed to connect to TestComplete via COM.

For detailed information about creating Visual Basic .NET Connected Applications, see Creating Connected Applications in Visual Basic .NET.

Writing the Object Code

It is possible to create a scripting object from any existing object of a Visual Basic .NET application. So, you can either expose an application’s particular object, or define a special class that implements specific test actions and register its instance as a scripting object. In this example, we will do the latter. Let’s create a sample class that defines a scripting object. This class will contain one property, Text, and one method, ShowText. The property will store a text string, and the method will display this string in a message box:

Visual Basic

Public Class SampleRuntimeObject
    Private fText As String

    Public Sub New()
        fText = ""
    End Sub

    Public Property Text() As String
            Text = fText
        End Get
        Set(ByVal Value As String)
            fText = Value
        End Set
    End Property

    Public Sub ShowText()
    End Sub
End Class

Registering and Unregistering Custom Scripting Object

In order to add our custom object to the TestComplete object model, you need to register it in the TestComplete extensibility manager (see Creating Scripting Objects From Application Objects). In Visual Basic .NET Connected Applications, you can obtain this manager using the Manager function.

To register a custom scripting object, call the manager’s AddName method. The method call can be inserted in the application’s initialization routine, or in a routine that is called upon a specific event. In our example, we will add a button control to the application form and place the registration code in the button’s OnClick event handler. This way, we will be able to easily re-register our object in subsequent TestComplete sessions while the application is running.

The sample code is provided below. Please pay special attention to the following:

  • The code uses the special CreateWrapper function. It creates and returns a wrapper object that implements the IDispatch interface and provides access to the underlying .NET object. This function is provided by the TestComplete tcClrHook.dll library, so we need to write special code that refers to this function from the application code.

  • At the end of the Button1_Click routine, we call the Marshal.ReleaseCOMObject method to release the COM wrapper created for the application’s object to be exposed. Make sure that you call this method and release wrappers for all objects that you expose in your code. Otherwise, the work with these objects will not be finished properly and the next attempt to register them as TestComplete scripting objects will fail.

Visual Basic

Imports System.IO
Imports System.Runtime.InteropServices
Imports AutomatedQA.TestComplete.Connect

Public Class Form1


    Private RuntimeObj As SampleRuntimeObject
    Private Const RuntimeObjName As String = "SampleRuntimeObject"
    Private Registered As Boolean

    <DllImport("tcClrHook.dll", PreserveSig:=False)> _
    Public Shared Function CreateWrapper(<MarshalAsAttribute(UnmanagedType.Interface)> ByVal Obj As Object) As <MarshalAsAttribute(UnmanagedType.Interface)> Object
    End Function


    Public Sub New()

        'This call is required by the Windows Form Designer.

        RuntimeObj = New SampleRuntimeObject
        Registered = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Wrapper As Object = Nothing
        Dim ProjectPath As String = Path.GetFullPath("..\..\TCProject\SampleRuntimeObject.pjs")

        ' Open a project suite in TestComplete
        TC.Visible = True

            ' Create a wrapper for RuntimeObj and register it in TestComplete
            Wrapper = CreateWrapper(RuntimeObj)
            Manager.AddName(RuntimeObjName, True, Wrapper)
            Registered = True
        Catch ex As Exception
            Call MessageBox.Show(Me, "Could not register" & RuntimeObjName & ":" & vbNewLine & ex.Message, _
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If Not IsNothing(Wrapper) Then
            End If
        End Try
    End Sub
End Class

To unregister an object, use the RemoveName method of the TestComplete extensibility manager. In our example, we will place the unregistration code in the form’s Closing event handler, so that the object is unregistered when the user exits the application by closing its main form. Since the unregistration makes sense only if the object has been previously registered and if TestComplete is running, we check both these conditions before calling the RemoveName method:

Visual Basic

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
                  Handles MyBase.FormClosing
    Dim TCExists As Process() = Process.GetProcessesByName("TestComplete")
    If Registered And (TCExists.Length <> 0) Then
    End If
End Sub

After you have included all the needed code into your application, you can re-compile it.

Working With Custom Scripting Object

After a custom scripting object is registered in TestComplete, it is displayed in the Code Completion window and can be used in scripts:

The SampleRuntimeObject object

You can get and set the object’s property values, call its methods and check their return values the same way you would with standard scripting objects such as TestedApps, Log and others. The only difference is that the processing is performed by your application rather than by TestComplete.

The code snippet below demonstrates how you can work with the sample scripting object we have created earlier in this topic. This object is available in TestComplete from the time you launch the application and click the button on its main form and until the application is closed. Note that, despite that the object is a part of a Visual Basic .NET application, it can be used in scripts written in any scripting language, not only VBScript:

JavaScript, JScript

function Main()
  SampleRuntimeObject.Text = "Hello, world!";


def Main():
  SampleRuntimeObject.Text = "Hello, world!"


Sub Main
  SampleRuntimeObject.Text = "Hello, world!"
End Sub


procedure Main;
  SampleRuntimeObject.Text := 'Hello, world!';

C++Script, C#Script

function Main()
  SampleRuntimeObject["Text"] = "Hello, world!";

See Also

Creating Scripting Objects From Application Objects
Connected Applications - Overview
Creating Connected Applications in Visual Basic .NET
Creating Scripting Objects From Visual Basic 6.0 Application Objects

Highlight search results