Writing C++ Scripts

Applies to TestComplete 15.46, last modified on January 09, 2023

C++Script is a specific dialect of the C++ programming language supported by TestComplete. It is based on JScript.

Note: C++Script is a legacy language. We do not recommend that you use it to create new tests. Instead, we recommend that you use JavaScript or Python. See Selecting Scripting Language.

C++Script was designed to let C++ developers easily port script routines to their C++ Self-Testing and Connected Applications.

Note: Connected and self-testing applications are deprecated. We do not recommend using them.

TestComplete can both record and play back C++Script routines. That is, you can record a script in TestComplete, debug it there, then import this script to your C++ application and make it work by adding just a few lines. For instance:

// This code was recorded by TestComplete:
 

function Test()
{
  var p, w;
  p = Sys["Process"]("notepad");
  w = p["Window"]("Notepad", "*");
  w["Activate"]();
  w["Window"]("Edit")["VScroll"]["Pos"] = 0;
  w["Window"]("Edit")["Click"](9, 9);
  Sys["Keys"]("Test");
  w["MainMenu"]["Click"]("File|Exit");
}

// This code was imported to a C++ Connected Application:
 

#include "c:\TestComplete\Connected Apps\C++\script.h"
using namespace TestComplete;
IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS
 
function Test()
{
  var p, w;
  p = Sys["Process"]("notepad");
  w = p["Window"]("Notepad", "*");
  w["Activate"]();
  w["Window"]("Edit")["VScroll"]["Pos"] = 0;
  w["Window"]("Edit")["Click"](9, 9);
  Sys["Keys"]("Test");
  w["MainMenu"]["Click"]("File|Exit");
}

The language of the code recorded by TestComplete for your C++ projects is JScript. So, you can simply write anything that is allowed in JScript and this script will be operational. For instance, in JScript and C++Script all variables and constants are VARIANT. You must declare them and you should do this using the following format:

var p, w;

Syntax of C++ scripts is the same as the syntax of C# scripts (since C#Script is also based on JScript). They are the same as long as the scripts reside in TestComplete. If you export scripts to a Connected Application, they will be different. That is, the syntax of C++Script and C#Script routines exported to C++ and C# Connected and Self-Testing Applications will differ.

Once again, the idea of having C++Scripts as a separate script language for TestComplete projects is to let developers, who are used to C++, write script code that requires minimal changes when being imported into C++ applications. When writing such code in C++Script, please keep in mind the following rules, which distinguish C++Script from JScript:

  1. When calling methods, use only the following notation: method names are quoted and placed in brackets, and their parameters are placed in parentheses:

    C++Script

    Log["Message"]("My Message", "My Message Description", 0, 1);

    TestComplete uses this notation when recording C++Script routines. JScript uses another notation for method calls:

    JScript

    Log.Message("My Message", "My Message Description", 0, 1);

    You should avoid this notation, since it is not supported in C++. Otherwise, you will have to rewrite code when importing it into C++.

  2. To address properties in C++Script, you should also use square brackets. For instance:

    C++Script

    p = w["Parent"];// Saving property value to a variable
    Sys["MouseX"] = 100; // Assigning property value

    To access an indexed property, use the following syntax: object["property_name"](index). For example:

    C++Script

    var p = TestedApps["Items"](0); // Getting property value
    TestedApps["Items"](0) = "notepad.exe";  // Setting property value

  3. Do not use JScript statements with and for...in and JScript operators ===, !==, >>> or >>>=. They are not supported in C++. If you use them, you will have to update your code when importing it into a C++ application.

  4. Also, these JScript objects are not currently supported in C++ applications: Array, Boolean, Date, Function, Global, Math, Number, Object, RegExp, Error and String.

  5. Use the semicolon at the end of each statement.

When you have inserted your C++Script routines into C++ code, try to compile the code. This will tell you whether it contains errors and where you can find them. Below are the most common changes that you may need to make:

  • C++Script uses only the VARIANT data type, so there is no need to specify the type of exceptions in the try... catch blocks. In C++ you must always specify the exception types.

  • You should initialize COM libraries before running imported scripts and uninitialize them when the scripts stop running. The easiest way to do this in your application is to use the IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTA or IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS macros declared in the <TestComplete>\Connected Apps\C++\scripts.h file. For more information on these macros, see Creating Self-Testing Applications in C++ or Creating Connected Applications in C++. Even if COM has already been initialized somehow, these macros will not cause harm to anything, so we recommend that you always use them.

  • Change instructions that assign values to indexed properties. To set an indexed property, use the following syntax: object[Put("property_name")](index)or object[(Put)"property_name"](index). For example:

    C++Script

    TestedApps[Put("Items")](0) = "notepad.exe";

      or

    TestedApps[(Put)"Items"](0) = "notepad.exe";
  • If your C++Script function returns a value, you should modify its syntax after it has been imported into your C++ application:

    C++ScriptCopy Code

    // The function returns a value
    function Func1()
    {
     return m;
    }

    C++Copy Code

    // The function returns a value
    int Func1()
    {
      return m;
    }

    If your script function does not return values, you cannot modify its syntax, because the script.h file, which is included in the C++ application, holds the following declaration:

    typedef void function

    So, the function keyword is identical to void. For instance:

    C++ScriptCopy Code

    // The function does not return a value 
    function Func1()
    {
    }

    C++Copy Code

    // The function does not return a value
    void Func1()
    {
    }
    // or
    function Func1()
    {
    }

Note that besides TestComplete, Script.h allows you to work with other COM servers, e.g. with Microsoft Word, in the same way. Script.h simplifies syntax used in C++ applications to work with COM servers via late binding. Once you have obtained the IDispatch interface of the desired server, you can address its methods and properties using the C++Script syntax rather than calling the GetIDsOfName, Invoke and other methods:

C++Script

#include "c:\TestComplete\Connected Apps\C++\script.h"
...
using namespace TestComplete;
void Test()
{
 /* We used the COleInit variable rather than
      the CoInitializeEx and CoUninitialize API functions. */

  COleInit g;

  var wrd;
  /* The GetObject method returns a reference to the desired COM server.
     It performs the actions, similar to those below:
      IDispatch * wrdVar;
      GUID WrdClass;
      CLSIDFromProgID(L"Word.Application", &WrdClass);
      CoCreateInstance(WrdClass, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&wrdVar);
      wrd = wrdVar; */
  wrd.GetObject(L"Word.Application");

 /* The following code performs some operations with the COM Server */
  wrd["Visible"] = true;
  wrd["Documents"]["Add"]();
  wrd["Selection"]["InsertAfter"]("Hello, world!");
}

See Also

Script Tests
JScript, C#Script and C++Script - Specifics of Usage
Handling Exceptions in Scripts
Creating Connected Applications in C++
Creating Self-Testing Applications in C++

Highlight search results