Creating Self-Testing Applications in C#

Applies to TestComplete 14.50, 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 C# Self-Testing Application includes the following steps:

  • Adding assemblies to your C# project
  • Writing test code in your C# 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).

Requirements

You can create C# Self-Testing 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 Self-Testing 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 Self-Testing 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 .NET (Visual Studio 2005).

  2. In Visual Studio .NET 7.x, right-click 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.
  3. 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.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.

  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;

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# Self-Testing 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; 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. Create a separate thread for the text 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:

    C#

    // TestComplete namespaces
    using AutomatedQA.TestComplete;
    using AutomatedQA.script;
    using System.Threading;
    // This namespace is necessary to address classes and
    // functions that work with threads
    using System.Threading;
     
     ...
    namespace CSConnectedApp
    {
     public class Form1 : System.Windows.Forms.Form
      {
        // Creates a thread and starts the test
       private void TestProc()
        {
          TestClass tcls = new TestClass(); // Creates a test class instance
          // Runs the thread

          ThreadStart thrd_start = new ThreadStart(tcls.RunTest);
          Thread thrd = new Thread(thrd_start);
          thrd.Start();
        }
      ...
      }

     public class TestClass : Connect
      {
       public void RunTest()
        {
          // Test code goes here
        }
      }
    }

  4. TestComplete must be in the script-running mode to run the test code located in your Self-Testing Application. 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).

    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:

    C#

    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.

  5. Compile your application.

Writing Code in TestComplete Projects

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:

  1. Open your project in TestComplete.

  2. 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.

  3. Write script code that will launch your Self-Testing Application and wait until it performs the testing actions. For example:

    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);
    }
  4. Make sure this code is called when running the current TestComplete project.

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

Self-Testing Applications
Running Connected and Self-Testing Applications
Connected Applications - Overview
Creating Connected Applications in C#

Highlight search results