Writing C++ Scripts
Applies to TestComplete 12.20, last modified on March 30, 2017
C++Script is a specific dialect of the C++ programming language supported by Tes
tComplete. It is based on JScript. It was specially designed to let C++ develope
rs easily port script routines to their C++ Self-Testing and Connected Applicati
ons.
Connected and self-testing applications are deprecated. We do not recomm
end 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 JScri
pt. 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 c
onstants are VARIANT. You must declare them and you should do this using the fol
lowing 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 Test
Complete. If you export scripts to a Connected Application, they will be differe
nt. 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 Test
Complete projects is to let developers, who are used to C++, write script code t
hat requires minimal changes when being imported into C++ applications. When wri
ting such code in C++Script, please keep in mind the following rules, which dist
inguish C++Script from JScript:
When calling methods, use only the following notation: method names are quoted a
nd 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++.
To address properties in C++Script, you should also use square brackets. For ins
tance:
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
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 up
date your code when importing it into a C++ application.
Also, these JScript objects are not currently supported in C++ applications: Arr
ay, Boolean, Date, Function, Global, Math, Number, Object, RegExp, Error and Str
ing.
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 ty
pe 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 uninitia
lize them when the scripts stop running. The easiest way to do this in your appl
ication is to use the IMPLEMENT_TESTCOMPLETE_GLOBAL_OBJECTS_MTA or IMPLEMENT_TES
TCOMPLETE_GLOBAL_OBJECTS macros declared in the <TestComplete>\Connected Apps\C+
+\scripts.h file. For more information on these macros, see Creating Self-Testin
g 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 i
t has been imported into your C++ application:
C++Script
// The function returns a value
function Func1()
{
return m;
}
C++
// The function returns a value
int Func1()
{
return m;
}
If your script function does not return values, you cannot modify its syntax, be
cause the script.h file, which is included in the C++ application, holds the fol
lowing declaration:
typedef void function
So, the function keyword is identical to void. For instance:
C++Script
// The function does not return a value
function Func1()
{
}
C++
// 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 serve
rs, e.g. with Microsoft Word, in the same way. Script.h simplifies syntax used i
n C++ applications to work with COM servers via late binding. Once you have obta
ined 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!");
}