Creating Connected Applications in C#

Applies to TestComplete 15.47, last modified on January 20, 2023
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 Connected Application in Microsoft C# consists of three steps:

  • Adding assemblies to your C# project

  • Writing test code in your C# Connected Application

  • (Optional) Writing code in your TestComplete project

The description of each step is below.

Requirements

You can create C# Connected Applications in Visual Studio 2013 or earlier. Visual Studio 2015 and later are not supported for this.

Adding Assemblies

To make your C# application a Connected Application, you need to add the following two assemblies to your C# project:

Assembly Description
AutomatedQA.script.dll This assembly adds the late binding functionality to your C# application. In addition, it holds declarations of the AutomatedQA.script namespace and the var class used to write test code. You can use it for creating Connected Applications, and it simplifies the process of working with OLE servers. For more information on this, see Writing C# Scripts.
AutomatedQA.TestComplete.CSConnectedApp.dll This assembly provides access to TestComplete global programming objects such as Log, TestedApps, etc. In addition, the assembly includes a declaration of the AutomatedQA.TestComplete namespace and the Connect class.

By default both assemblies are located in the <TestComplete>\Connected Apps\.NET folder.

To add these assemblies to your project, perform the following operations:

  1. Create a new C# project or open an existing one in Visual Studio.

  2. In Visual Studio 2005 and later, do the following:

    • Rightlick 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.CSConnectedApp.dll and AutomatedQA.script.dll libraries (to select several files, press Ctrl and keep it pressed when clicking the desired files).

    • After you have added these libraries to the component list, click OK to close the Add Reference dialog.

  3. In Visual Studio .NET 7.x, rightlick your project in the Solution Explorer and select Add Reference from the context menu. In the resulting Add Reference dialog:

    • Switch to the .NET tabbed page.

    • Click the Browse button to call the standard Open File dialog and open the AutomatedQA.TestComplete.CSConnectedApp.dll library.

    • Click Browse again and add the AutomatedQA.script.dll library.

    • After you have added these libraries to the component list, click OK to close the Add Reference dialog.

  4. Insert the following strings at the beginning of the source file that will contain the test code:

    C#

    using AutomatedQA.script;
    using AutomatedQA.TestComplete;
When working with .NET applications, you can call routines that reside in .NET assemblies from your scripts. You should add the desired assembly to the list in the project's CLR Bridge options. Assemblies added to the CLR Bridge options become available in scripts as well as routines, types and type members defined in the assembly (see Calling Functions From .NET Assemblies).
If a routine of an assembly uses or returns the var type, you should add local copies of the AutomatedQA.script.dll and AutomatedQA.TestComplete.CSConnectedApp.dll files to the folder holding the used .NET assembly. Otherwise, these routines will be inaccessible from your scripts and will not be displayed in the Code Completion window.
You can add these files manually or recompile your .NET assembly in Microsoft Visual Studio with the Properties|Copy Local property of the AutomatedQA.script.dll and AutomatedQA.TestComplete.CSConnectedApp.dll items set to True.

Once you have added the assemblies and strings, you can write test code. For more information on this, see below.

Writing Test Code in C# Connected Applications

  1. Type the test code in your C# source, or copy the test procedure(s) from TestComplete scripts written in C#Script (though C#Script and C# are not fully compatible, copying will save the time needed for typing. If you copy the code, you will have to modify it in order to make it compatible with C#).

  2. Modify the copied code so that it can be executed in your C# application.

    • Use the Connect class to address global programming objects of TestComplete. To use this class, add the using AutomatedQA.TestComplete.Connect; line to the source file that contains the testing code. Note that you can skip Connect if the class, whose methods contain the testing code, is inherited from the Connect class. This class contains fields whose names coincide with the names of programming objects. Therefore, the methods of this class may address these properties without using Connect.

      Note that the Connect class contains fields for TestComplete standard objects (Sys, Log, Regions and others). It does not contain fields for objects provided by third-party plugins. To obtain these objects, use the GetObjectByName method of the Integration object:

      C#

      using AutomatedQA.TestComplete;
      ...
      var MyObject = Connect.Integration["GetObjectByName"]("MyObjectName");

    • Replace the function keyword with the function's result type, for instance, replace function with void.

    • Variables that receive results of TestComplete functions must have the var type.

    • Note for Visual Studio .NET 2003 users:

      The C#Script code imported into a C# application uses objects of the var type. These objects are analogues to Variant variables in Visual Basic or Delphi. If you assign a C# object to a variable or property of the var type, the variable or property will cause an error. For example, this may happen within an event handling routine:

      C#

      private void axEventControl1_OnLogError(object sender, AxEventControl.ItcControlEvents_OnLogErrorEvent e)
      {
        var lp = (var)e.logParams; // Incorrect
        lp["AdditionalText"] = "Message"; // <-- An error occurs
        …
      }

      The error happens because Visual Studio does not allow automatic construction of the base class (named object) to any other object:

      Compiler Error CS0553 'conversion routine' : user defined conversion to/from base class
      User-defined conversions to values of a base class are not allowed; you do not need such an operator.

      To solve the problem, do the following:

      C#

      private void axEventControl1_OnLogError(object sender, AxEventControl.ItcControlEvents_OnLogErrorEvent e)
      {
        var lp = new var(e.logParams); // Correct!
        lp["AdditionalText"] = "Message"; // Correct!
        …
      }

    For more information about these modifications, see Writing C# Scripts.

  3. TestComplete must be in the script-running mode to run the test code located in your Connected Application. To run the test code, you should either launch the Connected Application from the TestComplete script, or call the Connect.RunTest routine (see Running Connected and Self-Testing Applications for more information).

    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 Connected Application. To stop the test run initiated with Connect.RunTest, use the Connect.StopTest routine:

    C#

    Connect.RunTest("My Test", "MyProject", "C:\\Work\\MyProjectSuite.pjs");
    ...
    // Test code
    ...
    Connect.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.

  4. Compile your application.

Writing Code in TestComplete Projects

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 Connect.RunTest routine (see Running Connected and Self-Testing Applications).

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

  1. Open your project in TestComplete.

  2. Add your Connected Application to the project’s list of tested applications. For more information on how to do this, see 4. Define an Application to Test.

  3. Write script code that will launch your Connected Application and wait until it performs 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);
    }
  4. Make sure this code is called when running the current TestComplete project.

  5. As you can see, the script is running 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.

Now you can run the test by clicking the button whose OnClick event handler contains the test code.

See Also

Connected Applications - Overview
Connect Class
Using the var Type in C# Connected Applications
Creating Event Handlers in C# Applications
Running Connected and Self-Testing Applications
Self-Testing Applications
Creating Self-Testing Applications in C#

Highlight search results