Creating Connected Applications in Delphi

Applies to TestComplete 15.20, last modified on January 19, 2022
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.

A Connected Application simulates mouse or keyboard input into itself. Before trying this, please see the Creating Self-Testing Applications in Delphi topic. This topic explains how to build a “generic” Connected Application with one of the following compilers:

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

The examples described in this topic are not self-testing as they explain how to “test” MS Paint. Although, you may want to read this topic first before going to the self-testing topic.

Creation of a Delphi Connected Application includes the following steps:

  • Adding units to your Delphi project
  • Writing test code in your Delphi Connected Application
  • (Optional) Writing code in your TestComplete project

The description of each step is 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 Connected Application

To create test code in a Connected 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 Connected 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.

Note that in order for the Connected Application to be able to address TestComplete objects and post messages to the test log, TestComplete must be in the script-running mode. To run the test code, you should either launch the Connected 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 Connected 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 Connected 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 Connected 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 RunTest routine (see Running Connected and Self-Testing Applications).

If you do not use the RunTest routine, you should launch your Connected Application from TestComplete. To do this:

  • Open your project in TestComplete.

  • Add your Connected 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 Connected Application and wait while it performs the testing actions. For instance:

    JavaScript, JScript

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

    Python

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

    VBScript

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

    DelphiScript

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

    C++Script, C#Script

    function Test()
    {
      var p;
      // Launches the Connected Application and obtains the process
      // that corresponds to your Connected Application
      p = TestedApps["Items"](0)["Run"]();
      // Delays script execution while 
      // the Connected 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 Connected Application is closed. In order to stop the script run from the Connected Application, use the Runner.Stop method. This method notifies 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

Connected Applications - Overview
Creating Event Handlers in Delphi and C++Builder Applications
Running Connected and Self-Testing Applications
Self-Testing Applications
Creating Self-Testing Applications in Delphi

Highlight search results