You can use TestComplete to work with COM and ActiveX objects registered in the operating system.
The way you obtain the desired object depends on the object type: out-of-process or in-process. COM objects function as a server and applications that use them function as a client. If a COM object functions within the process of its client application, it is an in-process object. Otherwise, if a COM object functions within a separate process, it is an out-of-process object.
This topic explains how to work with out-of-process and in-process COM objects in TestComplete. For common recommendations on calling methods and properties of COM objects and passing parameters to COM object methods, see the section at the end of the topic.
Working With Out-of-Process COM Objects
Out-of-process COM objects function in a process separate from the process of the COM client. To get a reference to an out-of-process object, you can use the Sys.OleObject
property or the getActiveXObject
method (JavaScript only). For instance:
JavaScript
// Returns a reference to the Application object of Microsoft Word
Obj = getActiveXObject("Word.Application");
JScript
// Returns a reference to the Application object of Microsoft Word
Obj = Sys.OleObject("Word.Application");
Python
# Returns a reference to the Application object of Microsoft Word
Obj = Sys.OleObject["Word.Application"]
VBScript
' Returns a reference to the Application object of Microsoft Word
Set Obj = Sys.OleObject("Word.Application")
DelphiScript
var
Obj : OleVariant;
begin
// Returns a reference to the Application object of Microsoft Word
Obj := Sys.OleObject('Word.Application');
end;
C++Script, C#Script
var Obj;
// Returns a reference to the Application object of Microsoft Word
Obj = Sys["OleObject"]("Word.Application");
To get the desired object, you can also use native methods of a scripting language. For instance, VBScript users can call the CreateObject
method of VBScript to obtain the desired COM object:
VBScript
' Returns a reference to the Application object of Microsoft Word
Set Obj = CreateObject("Word.Application")
If you use JScript, C#Script or C++Script, you can use the ActiveXObject
statement:
JScript, C#Script, C++Script
var Obj;
// Returns a reference to the Application object of Microsoft Word
Obj = new ActiveXObject("Word.Application");
After you obtain the object, you can call its methods in your scripts, work with properties, and so on. For instance, you can call methods and properties of COM objects using the simple “dot” notation:
MyCOMObject.PropertyName = Value
MyCOMObject.MethodName(Param1, Param2)
You can also create a script routine handling the object events. For an example of how to do this, see Creating Event Handlers for External COM Objects.
In addition, you can also explore the available methods, properties and events of the COM object in the TestComplete Object Browser. For information on how to do this, see Viewing COM Object Properties, Methods and Events.
Working With In-Process COM Objects
In-process COM objects and ActiveX components function within the process of their client application that uses the object or component. To access these objects, you need to have access to the internals of their client applications:
-
If an object is in an external application, to access the object, you may need to compile this application as an Open Application (we say “may” because Visual Basic, .NET and Java applications are always “Open” to TestComplete, so re-compilation is not needed in this case). Then, to access the desired COM object, use the methods and properties of its client application (to find out what method or property to use, explore the client application's process in the Object Browser):
JavaScript
// Returns a reference to the Adobe PDF Reader component residing on the application form
var obj = getActiveXObject("SampleApp").WinFormsObject("MainForm").WinFormsObject("acAcroPDF");JScript
// Returns a reference to the Adobe PDF Reader component residing on the application form
var obj = Sys.OleObject("SampleApp").WinFormsObject("MainForm").WinFormsObject("acAcroPDF");Python
# Returns a reference to the Adobe PDF Reader component residing on the application form obj = Sys.Process("SampleApp").WinFormsObject("MainForm").WinFormsObject("acAcroPDF")
VBScript
' Returns a reference to the Adobe PDF Reader component residing on the application form
Set obj = Sys.Process("SampleApp").WinFormsObject("MainForm").WinFormsObject("acAcroPDF")DelphiScript
var obj : OleVariant;
begin
// Returns a reference to the Adobe PDF Reader component residing on the application form
obj := Sys.Process('SampleApp').WinFormsObject('MainForm').WinFormsObject('acAcroPDF');C#Script
// Returns a reference to the Adobe PDF Reader component residing on the application form
var obj = Sys["Process"]("SampleApp")["WinFormsObject"]("MainForm")["WinFormsObject"]("acAcroPDF"); -
If an ActiveX control is located on a Web page, its client process is iexplore.exe (the Internet Explorer process), and you can obtain the desired object as an element of the tested Web page (for more information on testing Web pages, see Web Testing).
JavaScript, JScript
// Returns a reference to the Adobe PDF Reader component residing on a Web page
var obj = Sys.Browser("iexplore").Page("*").Object(0).contentDocument;Python
# Returns a reference to the Adobe PDF Reader component residing on a Web page obj = Sys.Browser("iexplore").Page("*").Object(0).contentDocument
VBScript
' Returns a reference to the Adobe PDF Reader component residing on a Web page
Set obj = Sys.Browser("iexplore").Page("*").Object(0).contentDocumentDelphiScript
var obj : OleVariant;
begin
// Returns a reference to the Adobe PDF Reader component residing on a Web page
obj := Sys.Browser('iexplore').Page('*').Object(0).contentDocument;C#Script
// Returns a reference to the Adobe PDF Reader component residing on a Web page
var obj = Sys["Browser"]("iexplore")["Page"]("*")["Object"](0)["contentDocument"];
You can also use TestComplete as a client application for your in-process object. In this case, the object is loaded to the TestComplete process:
You cannot use TestComplete as a client application for in-process OLE objects, the bitness of which is different than the bitness of TestComplete. |
Obtain an in-process COM object by using the Sys.OleObject
property or the getActiveXObject
method:
JavaScript
var obj = getActiveXObject("AcroPDF.PDF.1");
JScript
var obj = Sys.OleObject("AcroPDF.PDF.1");
Python
# Returns a reference to the object specified by its ProgID
obj = Sys.OleObject["AcroPDF.PDF.1"]
VBScript
Set obj = Sys.OleObject("AcroPDF.PDF.1")
DelphiScript
begin
// Returns a reference to the object specified by its ProgID
obj := Sys.OleObject('AcroPDF.PDF.1');
C#Script
var obj = Sys["OleObject"]("AcroPDF.PDF.1");
Notes on Calling Methods and Properties of COM Objects
-
If you are going to call methods that use parameters passed by reference, keep in mind the following:
-
JavaScript, JScript, C#Script and C++Script do not support parameters passed by reference (C#Script and C++Script are based on JScript) . When you call a COM server method that has a parameter passed by reference, the method will change the parameter, but the JavaScript and JScript engines will not return the changed value to the script.
-
When you call a COM server method in VBScript or DelphiScript code, a Type mismatch error can occur. The reason for this is that VBScript and DelphiScript use only Variant data types, while method parameters can be of any COM-compatible type. For example, a method can have the following declaration:
IDL
// Output parameters are passed by reference
HRESULT MyMethod([in] int InputParam, [out] BSTR * OutputParam1, [out] int * OutputParam2);When you call such a method from VBScript or DelphiScript code, the script engine passes variables of the VARIANT type to the method, and this causes the Type mismatch error.
To be compatible with VBScript and DelphiScript, the parameters must be of the VARIANT data type. You can call this sample method only if the parameters are declared in the following way:
-
-
DelphiScript does not support calling COM object methods that take
nil
as one of the parameters. To call such methods, use theaqObject.EmptyObject
property that returns an empty object. For example:DelphiScript
obj.DoSomething('Hello', aqObject.EmptyObject); -
Some methods may have parameters of the enumeration type or some methods or properties may return a value of the enumeration type. When you are working with COM objects from scripts, the scripting engine does not have access to the type library of this object. So, it does not “know” about various enumeration constants defined in this library type and, therefore, you cannot use these values in your scripts. You have to use the integer equivalents of the desired enumeration values.
-
In JavaScript and Python, to get an individual item of a collection object obtained via COM, you need to use the appropriate property (for example,
Item
orItems
):JavaScript
let value = Excel.Cells.Items(2,3);
Python
value = Excel.Cells.Items(2,3)
-
In JavaScript, you may get wrong results when comparing values obtained via COM with the
null
orundefined
value. To avoid errors, use theequal
andstrictEqual
methods.
See Also
Exploring Applications
Viewing COM Object Properties, Methods and Events
Creating Event Handlers for External COM Objects
OleObject Property