Creating Self-Testing Applications in Visual Basic .NET

Applies to TestComplete 14.70, last modified on April 22, 2021
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.

Creation of a Self-Testing Application in Visual Basic .NET includes the following steps:

  • Adding modules to your Visual Basic .NET project
  • Writing test code in your Visual Basic .NET Self-Testing Application
  • (Optional) Writing code in your TestComplete project

The description of each step is below. As you will see, the creation of a Self-Testing Application is very similar to creating a Connected Application. The key difference is that the Self-Testing Application should run test code in a separate thread (see below).

Adding modules to your Visual Basic .NET project

  • Create a new Visual Basic .NET project or open an existing project in Visual Studio .NET (Visual Studio 2005).

  • In Visual Studio .NET 7.x. select Project | Add Reference from the main menu to display the Add Reference dialog. Go to the .NET tabbed page and click the Browse button to display the standard Open File dialog and open the AutomatedQA.TestComplete.VBConnectedApp.dll library (by default, it is located in the <TestComplete>\Connected Apps\.NET folder). After you have added this library to the component list, click OK to close the Add Reference dialog.

  • In Visual Studio 2005, do the following:

    • Right-click your project in the Solution Explorer and then select Add Reference from the context menu.

    • In the resulting Add Reference dialog, switch to the Browse tabbed page.

    • Open the AutomatedQA.TestComplete.VBConnectedApp.dll library (by default, it is located in the <TestComplete>\Connected Apps\.NET folder).

    • Click OK to close the Add Reference dialog.

Writing test code in your Visual Basic .NET Self-Testing Application

  • To create test code in a Self-Testing Application, write the test code into your Visual Basic .NET source, or copy the test procedure(s) from TestComplete scripts written in VBScript.

    To address TestComplete global programming objects, use their names. However, this functionality is only supported for TestComplete standard objects (Sys, Log, Regions and others). To address objects provided by third-party plugins, use the TC.Integration.GetObjectByName method of the Connect class:

    Visual Basic .NET

    Imports AutomatedQA.TestComplete.Connect
    ...
    MyObj = TC.Integration.GetObjectByName("MyObjectName")

  • Important: make sure your VB.NET units do not contain the Option Strict On statement. This statement forbids late binding which is needed to execute the imported script code.

  • Add the following string to the beginning of the source file, which will hold the test code:

    Visual Basic .NET

    Imports AutomatedQA.TestComplete

  • Create a separate thread for the procedure to run in, unless it does not call for onscreen inputs (e.g. button clicks, menu selections, etc.) serviced by the current thread. For example:

    Visual Basic .NET

    ' TestComplete namespace
    Imports AutomatedQA.TestComplete.Connect
    ' This namespace is necessary to address classes and
    ' functions that work with threads
    Imports System.Threading


    Public Class Form1
      ...
      Class MyThread
        ' Thread function
        Public Sub Test()
          ' Test code goes here ...
        End Sub
      End Class
     
      ' Creates a thread
      Private Sub CreateThread()
        ' Creates an object that holds the thread function
        Dim MyThreadRef As New MyThread()
        ' Specifies the thread function
        Dim MyThreadStart As New ThreadStart(AddressOf MyThreadRef.Test)
        
         ' Creates a thread
        Dim MyThreadObj As New Thread(MyThreadStart)
        
        ' Start the thread
        MyThreadObj.Start()
      End Sub
      ...
    End Class

  • In order for the Self-Testing Application to be able to address TestComplete objects and post messages to the test log, TestComplete must be in the script-running mode. So, to run the test code, you should either launch the Self-Testing Application from the TestComplete script, or call the Connect.RunTest routine (see Running Connected and Self-Testing Applications for more information).

    So, if you are going to use the Connect.RunTest routine, insert a call to it before the first statement of the test code in your Self-Testing Application. To stop the test run initiated with Connect.RunTest, use the Connect.StopTest routine:

    Visual Basic .NET

    Connect.RunTest("My Test", "MyProject", "C:\Work\MyProjectSuite.pjs")
    ...
    ' Test code
    ...
    Connect.StopTest()

    If you are going to launch your Self-Testing Application from TestComplete, call the test procedure from your own code. For instance, you can call the test procedure within a button’s OnClick event handler.

  • Compile your application.

Writing code in your TestComplete project

In order for the Self-Testing Application to be able to use TestComplete scripting objects and post messages to the test log, TestComplete must be in the script-running mode. The testing engine switches to this mode when you run a test from TestComplete, or when you call the Connect.RunTest routine (see Running Connected and Self-Testing Applications).

If you do not use the Connect.RunTest routine, you should launch your Self-Testing Application from TestComplete. To do this:

  • Open your project in TestComplete.

  • Add your Self-Testing Application to the project’s tested applications list. For more information on how to do this, see Defining Applications to Test.

  • Write script code that will launch your Self-Testing Application and wait while it performs the testing actions. For instance:

    JavaScript, JScript

    function Test()
    {
      var p;
      // Launches the Self-Testing Application and obtains the process
      // that corresponds to your Self-Testing Application
      p = TestedApps.Items[0].Run();
      // Delays script execution while 
      // the Self-Testing Application performs the testing actions
      while(p.Exists)
        aqUtils.Delay(500);
    }

    Python

    def Test():
      # Launches the Self-Testing Application and obtains the process
      # that corresponds to your Self-Testing Application
      p = TestedApps.Items[0].Run()
      # Delays script execution while 
      # the Self-Testing Application performs the testing actions
      while p.Exists:
        aqUtils.Delay(500)

    VBScript

    Sub Test
      ' Launches the Self-Testing Application and obtains the process
      ' that corresponds to your Self-Testing Application
      Set p = TestedApps.Items(0).Run
      ' Delays script execution while
      ' the Self-Testing Application performs the testing actions
      While p.Exists
        aqUtils.Delay 500
      WEnd
    End Sub

    DelphiScript

    procedure Test;
    var
      p : OleVariant;
    begin
      // Launches the Self-Testing Application and obtains the process
      // that corresponds to your Self-Testing Application
      p := TestedApps.Items[0].Run;
      // Delays script execution while
      // the Self-Testing Application performs the testing actions
      while p.Exists do
        aqUtils.Delay(500);
    end;

    C++Script, C#Script

    function Test()
    {
      var p;
      // Launches the Self-Testing Application and obtains the process
      // that corresponds to your Self-Testing Application
      p = TestedApps["Items"](0)["Run"]();
      // Delays script execution while 
      // the Self-Testing Application performs the testing actions
      while (p["Exists"])
        aqUtils["Delay"](500);
    }
  • Make sure this code is called when running the current TestComplete project.

  • As you can see, the script runs until the Self-Testing Application is closed. In order to stop the script run from the Self-Testing Application, use the Runner.Stop method. This method signals to TestComplete that the run is over and refreshes the Test Log so that it displays the latest test results.

You can now run the test by clicking the button whose OnClick event handler contains the test code.

See Also

Self-Testing Applications
Running Connected and Self-Testing Applications
Connected Applications - Overview
Creating Connected Applications in Visual Basic .NET

Highlight search results