C++Script is a specific dialect of the C++ programming language supported by TestComplete. It is based on JScript. It was specially designed to let C++ developers easily port script routines to their C++ Self-Testing and Connected Applications.
|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:
// This code was imported to a C++ Connected Application:
#include "c:\TestComplete\Connected Apps\C++\script.h"
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:
When calling methods, use only the following notation: method names are quoted and placed in brackets, and their parameters are placed in parentheses:
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:
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++.
To address properties in C++Script, you should also use square brackets. For instance:
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:
var p = TestedApps["Items"](0); // Getting property value
TestedApps["Items"](0) = "notepad.exe"; // Setting property value
Do not use JScript statements
for...inand JScript operators
>>>=. They are not supported in C++. If you use them, you will have to update your code when importing it into a C++ application.
Also, these JScript objects are not currently supported in C++ applications:
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
VARIANTdata type, so there is no need to specify the type of exceptions in the
try... catchblocks. 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_OBJECTSmacros 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). For example:
C++ScriptTestedApps[Put("Items")](0) = "notepad.exe";
orTestedApps[(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++Script// The function returns a value
C++// The function returns a value
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++Script// The function does not return a value
C++// The function does not return a value
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
Invoke and other methods:
#include "c:\TestComplete\Connected Apps\C++\script.h"
using namespace TestComplete;
/* We used the COleInit variable rather than
the CoInitializeEx and CoUninitialize API functions. */
/* The GetObject method returns a reference to the desired COM server.
It performs the actions, similar to those below:
IDispatch * wrdVar;
CoCreateInstance(WrdClass, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&wrdVar);
wrd = wrdVar; */
/* The following code performs some operations with the COM Server */
wrd["Visible"] = true;