Creating Self-Testing Applications in Delphi

Applies to TestComplete 14.20, last modified on September 11, 2019
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 topic describes how to create Self-Testing Applications with the following compilers:

  • Borland Delphi 3.0 - 7.0
  • Borland Delphi 2005 and 2006 for Win32
  • CodeGear Delphi 2007 and 2009 for Win32
  • Embarcadero Delphi 2010 and XE

Creation of a Delphi Self-Testing Application includes the following steps:

  1. Adding units to your Delphi project
  2. Writing test code in your Delphi Self-Testing Application
  3. (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 units to Delphi project

  • Create a new Delphi project or open an existing one.

  • Include the <TestComplete>\Connected Apps\Delphi\TCConnect.pas file into the list of source file(s) where you want to put your test procedures. TCConnect.pas contains functions that allow you to use TestComplete objects (Sys, Log, Project, Files, Regions, etc.).

Writing test code in Delphi Self-Testing Application

  • To create test code in a Self-Testing Application, write the test code in your Delphi source, or copy the test procedure(s) from TestComplete scripts written in DelphiScript.

    To address TestComplete objects from a Self-Testing Application, use object names. This functionality is only implemented for standard TestComplete objects (Sys, Log, Regions, Files and others) and not supported for objects that are provided by third-party plugins.

    If you need to work with an object that is provided by a third-party plugin, you should obtain this object by using the GetObjectByName function that is defined in TCConnect.pas. To emulate addressing an object by its name, you can create a special function that will call the GetObjectByName function and return the desired object. The important thing is that the function’s name coincides with the object’s name:

    Delphi

    [YourUnit.pas]
     

    uses TCConnect;
    ...
    function MyObjectName : OleVariant;
    begin
      Result := TCConnect.GetObjectByName('MyObjectName');
    end;

    Since Delphi lets you skip braces when calling functions that do not have any parameters, the function will give an impression that you address an object directly.

  • 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. If you need an input thread, it has to begin with a call to CoInitializeEx(nil, COINIT_MULTITHREADED).

  • Note that 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 RunTest routine (see Running Connected and Self-Testing Applications for more information).

    So, if you are going to use the 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 RunTest, use the StopTest routine:

    Delphi

    RunTest('My Test', 'MyProject', 'C:\Work\MyProjectSuite.pjs');
    ...
    // Test code
    ...
    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 Self-Testing Applications 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 RunTest routine (see Running Connected and Self-Testing Applications).

If you do not use the 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 perform 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 notifies TestComplete that the test 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 Delphi

Highlight search results