Creating Event Handlers in Delphi and C++Builder Applications

Applies to TestComplete 15.0, last modified on November 17, 2021
Information in this topic applies to generic desktop applications (.NET, WPF, Delphi and others).
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.

To handle events in TestComplete, you use Event controls. These controls are ActiveX objects registered in the operating system. You can use them to create event handlers in TestComplete, as well as in any other application that supports working with ActiveX controls. This topic explains how you create handling code for TestComplete events in Delphi and C++Builder Connected Applications.

1. Preparing the Event Control

Before you start creating event handlers, you need to prepare the Event control that you will use for event handling.

  1. Open your test project in TestComplete, and open your Event control for editing. To do this, right-click the Event control in the Project Explorer and choose Edit from the context menu.

  2. In the Event Control editor, add the desired events to the Event control. The steps you need to perform depend on the events to be handled. See Adding Events to Event Controls for complete information.

  3. In the TLB Description edit box at the top of the Event Control editor, specify some descriptive text for your Event control. This text will identify the control among other ActiveX objects in the system.

    Description of the Event Control
  4. Select File | Save from the TestComplete main menu to save the changes.

2. Adding the Event Control to the Toolbox Panel

In order to handle events in Delphi or C++Builder applications, you need to add the Event control to Delphi (C++Builder). The installation of the control is similar to the installation of any other ActiveX component:

  1. Select Component | Import ActiveX Control from Delphi’s (C++Builder’s) main menu. This will open the Import ActiveX dialog.

  2. In the dialog --

    • Select the .ocx library along with the Event control from the list of registered libraries. This list is displayed at the top of the dialog. It holds descriptions of ActiveX libraries registered in the operating system. To search for the desired library you can use the description you have specified in the TLB description field of the Event Control editor.

    • Press Create Unit. This will update the .pas file that implements the wrapper class for the Event control. If this .pas file already exists, Delphi (C++Builder) will ask if you want to replace the file. Press Yes.

  3. Pressing Create Unit closes the Import ActiveX dialog. Open the Import ActiveX dialog once again and select the desired .ocx library. Then,

    • (Optional step) From the Palette Page dropdown list select the Component Palette’s page which will hold the Event control after it is installed in Delphi (C++Builder). By default, the control will be installed into the ActiveX page.

    • Press Install. Delphi (C++Builder) will display the Install dialog where you should specify the package which will hold the OCX library. You can either install it into an existing package or into a new one. When you have specified the package, press OK. Delphi (C++Builder) will add the component to the specified page of the Component Palette. The component has the icon.

3. Creating the Event Handler

3.1. Creating the Event Hander Function

To create an event handler:

  • Drop the Event control on a form.

  • Select the control on the form and switch to the Object Inspector.

  • Double-click on the desired event in the Events page.

Delphi (C++Builder) will create the event handler procedure and display it in the editor.

3.2. Attaching Event Controls to Event Sources

To make your Event control respond to events of TestComplete or ActiveX objects, you should establish a link between the control and TestComplete (or between the control and the desired object). We call this attaching an Event control to the event source. You should attach the control to every object, whose events you are going to handle. The Event control can only process the TestComplete engine’s events or events of an ActiveX object if it has been attached to them.

To attach an Event control to TestComplete or desired object, use the following methods of the Event control:

  • EventControl.AttachToTestComplete() - Establishes the link to the TestComplete engine. Call this method if you need to handle TestComplete events.

  • EventControl.AttachTo(Obj) - Attaches the Event control to any object specified by the Obj parameter. This object must implement the IDispatch interface. Use this methods to handle events of ActiveX objects that reside outside of TestComplete.

    Note: The AttachTo method is available if your Event control contains events of external ActiveX objects. If the control only contains TestComplete events, the method is unavailable.

It may be convenient  to attach an Event control to the desired event source after your C# Connected Application has been started. You can call the AttachToTestComplete and AttachTo methods within the OnLoad event handler of the application’s main form. For instance, to handle events of the Document object of Microsoft Word, follow these steps:

  • Add Microsoft Word’s events to the list of project events. See Adding Events to Event Controls for more information on this.

  • Attach the Event control to TestComplete by calling the EventControl.AttachToTestComplete method.

  • Attach the Event control to each Microsoft Word’s Document object, whose events you would like to handle. That is, call EventControl.AttachTo(DocumentObject) for each Document object:

    Delphi

    var
      WordAppObject, DocObject1, DocObject2 : OleVariant;
    begin
      // Attaching Event control to TestComplete
      EventControl1.AttachToTestComplete();
      
      // Getting references to Microsoft Word and Document objects
      WordAppObject := Sys.OleObject('Word.Application');
      DocObject1 := WordAppObject.Documents.Item(1);
      DocObject2 := WordAppObject.Documents.Item(2);
      
      // Attaching to Microsoft Word and Document objects
      EventControl1.AttachTo(DocObject1);
      EventControl1.AttachTo(DocObject2);
      EventControl1.AttachTo(WordAppObject);
      ...
    end;

    C++Builder

    // Attaching Event control to TestComplete
    EventControl1.AttachToTestComplete();
     
    // Getting references to Microsoft Word and Document objects
    varWordAppObject = Sys["OleObject"]("Word.Application");
    var DocObject1 = WordAppObject["Documents"]["Item"](1);
    var DocObject2 = WordAppObject["Documents"]["Item"](2);
     
    // Attaching to Microsoft Word and Document objects
    EventControl1->AttachTo(DocObject1);
    EventControl1->AttachTo(DocObject2);
    EventControl1->AttachTo(WordAppObject);

3.4. Detaching Event Controls From Event Sources

When the testing is over, it is recommended to detach from the objects whose events were processed. This will prevent you from possible malfunction in the application under test and in the system.

To detach from TestComplete, call the EventControl.DetachFromTestComplete method. To detach from other objects, call EventControl.DetachFrom(Obj):

Delphi

// Attaching Event control to event sources
EventControl1.AttachToTestComplete();
EventControl1.AttachTo(WordAppObject);
EventControl1.AttachTo(DocObject1);
EventControl1.AttachTo(DocObject2);
...
// Test code
...
' Detaching Event control from event sources
EventControl1.DetachFrom(WordAppObject);
EventControl1.DetachFrom(DocObject1);
EventControl1.DetachFrom(DocObject2);
EventControl1.DetachFromTestComplete();

C++Builder

// Attaching Event control to event sources
EventControl1->AttachToTestComplete();
EventControl1->AttachTo(WordAppObject);
EventControl1->AttachTo(DocObject1);
EventControl1->AttachTo(DocObject2);
...
// Test code
...
' Detaching Event control from event sources
EventControl1->DetachFrom(WordAppObject);
EventControl1->DetachFrom(DocObject1);
EventControl1->DetachFrom(DocObject2);
EventControl1->DetachFromTestComplete();

The DetachFrom method is available only if your Event control contains some events of external ActiveX objects. It is not available if the control contains only TestComplete events.

After the Event control has been detached from TestComplete or an object, it will not receive notifications about events that occur in TestComplete or this object, thus you will not be able to handle these events.

See Also

Handling Events - Overview
Creating Event Handlers for External COM Objects
Creating Connected Applications in Delphi
Creating Connected Applications in C++
Creating Event Handlers in Connected Applications

Highlight search results