The object-driven testing (ODT) functionality is deprecated. Do not use it to create new tests. It will be removed from the product in one of the future releases. As an alternative, you can create custom classes in your scripts. For more information, see Alternatives to the ODT functionality.
Once we have created the classes, we can create objects based on these classes. We will start creating the object hierarchy with the topmost object based on the ClsNotepadTest
class and then we will create other objects down in the hierarchy.
Creating Objects
Creating ObjectsCreating ObjectsTo create an object, use the following code:
JavaScript, JScript
TestObj = ODT.Classes.New("ClsNotepadTest");
VBScript
Set TestObj = ODT.Classes.New("ClsNotepadTest")
DelphiScript
TestObj := ODT.Classes.New('ClsNotepadTest');
C++Script, C#Script
TestObj = ODT["Classes"]["New"]("ClsNotepadTest");
|
The objects created programmatically are deleted after a test run. In order to save an object to the ODT or Data editor, create a variable in one of data groups and save a reference to the object to this variable.
JavaScript, JScript
// Obtains a group
group = ODT.Data.Groups("TestSample");
// Creates a variable that stores a reference to the object
group.AddVariable("ReleaseTest", TestObj);
VBScript
' Obtains a group Set group = ODT.Data.Groups("TestSample")
' Creates a variable that stores a reference to the object
group.AddVariable "ReleaseTest", TestObj
DelphiScript
// Obtains a group
group := ODT.Data.Groups('TestSample');
// Creates a variable that stores a reference to the object
group.AddVariable('ReleaseTest', TestObj);
C++Script, C#Script
// Obtains a group
group = ODT["Data"]["Groups"]("TestSample");
// Creates a variable that stores a reference to the object
group["AddVariable"]("ReleaseTest", TestObj);
|
There is another way to create an object: You can do this by calling the AddVarOfClassType
method of the Group
object:
JavaScript, JScript
TestObj = group.AddVarOfClassType("ReleaseTest", "ClsNotepadTest");
VBScript
Set TestObj = group.AddVarOfClassType("ReleaseTest", "ClsNotepadTest")
DelphiScript
TestObj := group.AddVarOfClassType('ReleaseTest', 'ClsNotepadTest');
C++Script, C#Script
TestObj = group["AddVarOfClassType"]("ReleaseTest", "ClsNotepadTest");
Note: |
If an object contains “class” properties, TestComplete automatically creates new class instances upon object creation. There is no need to create instances of these classes manually.
|
Specifying Property Values
Specifying Property ValuesSpecifying Property ValuesOnce we have created the object, we can call its methods and modify its properties. To access the object’s property or method, use the following syntax: object.property_name
or object.method_name
. For instance, the following code modifies the ID
property of the ClsNotepadTest
object:
JavaScript, JScript
TestObj.ID = "ReleaseTest";
VBScript
TestObj.ID = "ReleaseTest"
DelphiScript
TestObj.ID := 'ReleaseTest';
C++Script, C#Script
TestObj["ID"] = "ReleaseTest";
The Files
property of the ClsNotepadTest
object holds an array of ClsFile
objects that are used to test individual files to be processed via Notepad. Below is the code that adds one item to the array.
JavaScript, JScript
FileObj = TestObj.Files.AddItemOfClassType("ClsFile");
VBScript
Set FileObj = TestObj.Files.AddItemOfClassType("ClsFile")
DelphiScript
FileObj := TestObj.Files.AddItemOfClassType('ClsFile');
C++Script, C#Script
FileObj = TestObj["Files"]["AddItemOfClassType"]("ClsFile");
Note that when you are adding a "class" item to an array using the AddItemOfClassType
method, TestComplete automatically creates a new instance of the class specified in the AddItemOfClassType
call.
Once you have created the ClsFile
objects, you can specify their properties in the same manner as you specified properties of the ClsNotepadTest
object. For instance, the code below specifies the path to the desired file (if only the file name is given, this means the file is in the current project’s folder).
JavaScript, JScript
FileObj.Path = "C:\\TestFiles\\file1.txt";
VBScript
FileObj.Path = "C:\TestFiles\file1.txt"
DelphiScript
FileObj.Path := 'C:\TestFiles\file1.txt';
C++Script, C#Script
FileObj["Path"] = "C:\\TestFiles\\file1.txt";
To specify what operations will be used to test the given file, we will use the form’s Operations
property. You can add the ClsOperation
objects to the Operations
array in the same manner as you added the ClsFile
objects to the ClsNotepadTest.Files
array, for example,
JavaScript, JScript
OperationObj = FileObj.Operations.AddItemOfClassType("ClsOperation");
OperationObj.ID = "Find";
OperationObj.MenuPath = "Edit|Find...";
VBScript
Set OperationObj = FileObj.Operations.AddItemOfClassType("ClsOperation")
OperationObj.ID = "Find"
OperationObj.MenuPath = "Edit|Find..."
DelphiScript
OperationObj := FileObj.Operations.AddItemOfClassType('ClsOperation');
OperationObj.ID := 'Find';
OperationObj.MenuPath := 'Edit|Find...';
C++Script, C#Script
OperationObj = FileObj["Operations"]["AddItemOfClassType"]("ClsOperation");
OperationObj["ID"] = "Find";
OperationObj["MenuPath"] = "Edit|Find...";
To specify which values will be input for each operation, we will use the Values
property of the ClsOperation
object. This property holds an array of testing values. Each item of this array will be an object. This is made to have sets of data specific for the chosen operation:
JavaScript, JScript
// Adds an item to the Values array
ValueObj = OperationObj.Values.AddItemOfClassType("ClsFindData");
ValueObj.FindText = "aaa";
VBScript
' Adds an item to the Values array
Set ValueObj = OperationObj.Values.AddItemOfClassType("ClsFindData")
ValueObj.FindText = "aaa"
DelphiScript
// Adds an item to the Values array
ValueObj := OperationObj.Values.AddItemOfClassType('ClsFindData');
ValueObj.FindText := 'aaa';
C++Script, C#Script
// Adds an item to the Values array
ValueObj = OperationObj["Values"]["AddItemOfClassType"]("ClsFindData");
ValueObj["FindText"] = "aaa";
Working With Objects in Methods
Working With Objects in MethodsWorking With Objects in MethodsTo address the object to which a method belongs, within this method, use the keyword This
in VBScript, JScript, C++Script and C#Script projects. Use the keyword Self
in DelphiScript projects. You can access an object's properties and methods by using these keywords. For instance, the Init
method of the ClsNotepadTest
object can include the following code:
JavaScript, JScript
function Init()
{
for( i = 0; i < This.Files.Count; i++)
{
FileObj = This.Files(i);
// Perform some actions over the ClsFile object ...
}
}
VBScript
Sub Init
For i = 0 To This.Files.Count - 1
Set FileObj = This.Files(i)
' Perform some actions over the ClsFile object ...
Next
End Sub
DelphiScript
procedure Init;
var
i, FileObj : OleVariant;
begin
for i := 0 to Self.Files.Count - 1 do
begin
FileObj := Self.Files[i];
// Perform some actions over the ClsFile object ...
end;
end;
C++Script, C#Script
function Init()
{
var i, FileObj;
for( i = 0; i < This["Files"]["Count"]; i++)
{
FileObj = This["Files"](i);
// Perform some actions over the ClsFile object ...
}
}
Note: |
Note that the This and Self keywords can only be used within script routines that represent object methods. Outside object methods, the values of these keywords are undefined. |
Suppose that within a method that belongs to an object instance you need to access the object that holds a reference to the method’s object. To accomplish this, use the property. Below is the illustration of how to do this, taken from our example. The MainUnit.ClsReplaceData_FindReplace
script routine plays the role of the ClsReplaceData.Replace
method; the first owner of the instance of the ClsFindReplace
class is the Values
array, whose owner is an instance of the ClsOperation
class.
JavaScript, JScript
function ClsReplaceData_FindReplace()
{
...
This.Owner.Owner.Execute();
...
}
VBScript
Sub ClsReplaceData_FindReplace
...
Call This.Owner.Owner.Execute
...
End Sub
DelphiScript
procedure ClsReplaceData_FindReplace;
begin
...
Self.Owner.Owner.Execute;
...
end;
C++Script, C#Script
function ClsReplaceData_FindReplace()
{
...
This["Owner"]["Owner"]["Execute"]();
...
}
The full code for this tutorial is available in the <TestComplete Samples>\Common\Object-Driven Testing folder. If you do not have it, download the TestComplete Samples installation package from the https://support.smartbear.com/downloads/testcomplete/samples/ page of our website and run it.
We would like to note that a custom object can only be referred to by one property, array item or a data-group variable. That is, two or more properties, array items or data-group variables referring to the same object is not allowed. For instance, if you need to exchange values of two properties that store objects, you need to assign an empty value (Nothing
in VBScript, null
in JScript, C++Script and C#Script and nil
in DelphiScript) to one of these properties before exchanging. See 4. Working With Custom Objects in Scripts for sample code.
Prev
See Also
Creating Custom Objects Programmatically