4. Working With Custom Objects in Scripts

Applies to TestComplete 15.70, last modified on December 17, 2024

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.

In the previous steps, we created classes and objects via the ODT, Classes and Data, now we can use these objects in scripts.

How to Obtain a Reference to an Object

To get a reference to an object, you need to obtain a variable that holds this reference. It is very easy to do this. The code below illustrates how you can obtain the ReleaseTest object.

Show code

Calling Object’s Methods and Properties

Once we have created an object, we can call its methods and properties. To do this, use the following syntax: object.property_name or object.method_name. For instance, the following code snippet sets the MenuPath property and calls the Init method of the instance of the ClsOperation class.

JavaScript, JScript

// Obtains the object
AppVar = ODT.Classes.New("ClsOperation");
// Accesses the property
AppVar.MenuPath = "File|Open...";
// Calls the method
AppVar.Execute();

VBScript

' Obtains the object
Set AppVar = ODT.Classes.New("ClsOperation")
' Accesses the property
AppVar.MenuPath = "File|Open..."
' Calls the method
Call AppVar.Execute

DelphiScript

// Obtains the object
AppVar := ODT.Classes.New('ClsOperation');
// Accesses the property
AppVar.MenuPath := 'File|Open...';
// Calls the method
AppVar.Execute();

C++Script, C#Script

// Obtains the object
AppVar = ODT["Classes"]["New"]("ClsOperation");
// Accesses the property
AppVar.MenuPath = "File|Open...";
// Calls the method
AppVar["Execute"]();
How to Access Objects in Methods (This and Self Keywords)

To access an object instance within a method that belongs to this instance, use the keyword This (VBScript, JScript, C++Script or C#Script) or Self (DelphiScript). You can access an object's properties and methods using these keywords. Below is the complete code for the ClsNotepadTest.Execute method that illustrates this (in our example, the MainUnit.ClsOperation_Execute script routine plays the role of the Execute method).

JavaScript, JScript

function ClsOperation_Execute()
{
  Log.Message("ClsOperation_Execute");
  MainW.Activate();
  MainW.MainMenu.Click(This.MenuPath);
}

VBScript

Sub ClsOperation_Execute
  Log.Message "ClsOperation_Execute"
  Call MainW.Activate()
  Call MainW.MainMenu.Click(This.MenuPath)
End Sub

DelphiScript

procedure ClsOperation_Execute;
begin
  Log.Message('ClsOperation_Execute');
  MainW.Activate;
  MainW.MainMenu.Click(Self.MenuPath);
end;

C++Script, C#Script

function ClsOperation_Execute()
{
  Log["Message"]("ClsOperation_Execute");
  MainW["Activate"]();
  MainW["MainMenu"]["Click"](This["MenuPath"]);
}
Note: This and Self keywords refer to the object instance, not to the object class. To obtain the class of the object instance that the given method belongs to, use the This.ClassRef or Self.ClassRef notation:

JavaScript, JScript

function Cls_Method()
{
  ...
  // Obtain the object class
  var cls = This.ClassRef;
  ...
}

VBScript

Sub Cls_Method
  ...
  ' Obtain the object class
  Set cls = This.ClassRef
  ...
End Sub

DelphiScript

procedure Cls_Method;
var cls : OleVariant;
begin
  ...
  // Obtain the object class
  cls := Self.ClassRef;
  ...
end;

C++Script, C#Script

function Cls_Method()
{
  ...
  // Obtain the object class
  var cls = This["ClassRef"];
  ...
}

Suppose that within an object’s method you need to pass the current object instance as a parameter of another method. For this purpose, you can also use the This and Self keywords:

JavaScript, JScript

function Cls_Method1()
{
  ...
  This.Method2 (This);
  ...
}

VBScript

Sub Cls_Method1
  ...
  This.Method2 (This)
  ...
End Sub

DelphiScript

procedure Cls_Method1;
begin
  ...
  Self.Method2 (Self);
  ...
end;

C++Script, C#Script

function Cls_Method1()
{
  ...
  This["Method2"](This);
  ...
}

However, if you need to pass the current object instance as a parameter of another object’s method, you should use the This.GetObject and Self.GetObject notation. Since This and Self are wrappers that refer to the current object instance, if passed to the method of another object they will refer to that object. In this case, using the GetObject method allows you to obtain the current object instance itself:

JavaScript, JScript

// This routine is used as the Class1.Method method
function Cls1_Method ()
{
  var obj2 = ODT.Classes.New("Class2");
  obj2.Method (This.GetObject);    // Posts "Class1" to the log
}

// This routine is used as the Class2.Method method
function Cls2_Method (Obj)
{
  // Log the object's class name
  Log.Message (Obj.ClassRef.Name);
}

VBScript

' This routine is used as the Class1.Method method
Sub Cls1_Method
  Set obj2 = ODT.Classes.New("Class2")
  obj2.Method (This.GetObject)    ' Posts "Class1" to the log
End Sub

' This routine is used as the Class2.Method method
Sub Cls2_Method (Obj)
  ' Log the object's class name
  Log.Message (Obj.ClassRef.Name)
End Sub

DelphiScript

// This routine is used as the Class2.Method method
procedure Cls2_Method (Obj);
begin
  // Log the object's class name
  Log.Message (Obj.ClassRef.Name);
end;

// This routine is used as the Class1.Method method
procedure Cls1_Method;
var obj2: OleVariant;
begin
  obj2 := ODT.Classes.New('Class2');
  obj2.Method (Self.GetObject);    // Posts "Class1" to the log
end;

C++Script, C#Script

// This routine is used as the Class1.Method method
function Cls1_Method ()
{
  var obj2 = ODT["Classes"]["New"]("Class2");
  obj2["Method"](This["GetObject"]);    // Posts "Class1" to the log
}

// This routine is used as the Class2.Method method
function Cls2_Method (Obj)
{
  // Log the object's class name
  Log["Message"](Obj["ClassRef"]["Name"]);
}

Note: This and Self keywords can only be used within script routines that represent object methods. Outside these routines, 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 do this, use the Owner property. Below is an 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 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"]();
  ...
}

We would like to note that the objects you create via the ODT project item can be referred to only by one property, array item or data-group variable at a time. That is, an object cannot be referred to by several properties, array items or variables in data groups. For instance, if you need to exchange values of two properties, you will have to assign an empty value to one of these properties. The following sample code illustrates how you can do this.

JavaScript, JScript

// Stores objects to properties
obj.FirstObject = Obj1;
obj.SecondObject = Obj2;

// Exchange property values
// Saves objects to local variables
v1 = obj.FirstObject;
v2 = obj.SecondObject;

// Assigns Nothing to one of the properties,
// since an object cannot be referred to by several properties
obj.FirstObject = null;

// Assigns properties
obj.SecondObject = v1;
obj.FirstObject = v2;

VBScript

' Store objects to properties
Set obj.FirstObject = Obj1
Set obj.SecondObject = Obj2

' Exchange property values
' Saves objects to local variables
Set v1 = obj.FirstObject
Set v2 = obj.SecondObject

' Assigns Nothing to one of the properties,
' since an object cannot be referred to by several properties
Set obj.FirstObject = Nothing

' Assigns properties
Set obj.SecondObject = v1
Set obj.FirstObject = v2

DelphiScript

// Stores objects to properties
obj.FirstObject := Obj1;
obj.SecondObject := Obj2;

// Exchange property values
// Saves objects to local variables
v1 := obj.FirstObject;
v2 := obj.SecondObject;

// Assigns Nothing to one of the properties,
// since an object cannot be referred to by several properties
obj.FirstObject := nil;

// Assigns properties
obj.SecondObject := v1;
obj.FirstObject := v2;

C++Script, C#Script

// Stores objects to properties
obj["FirstObject"] = Obj1;
obj["SecondObject"] = Obj2;

// Exchange property values
// Saves objects to local variables
v1 = obj["FirstObject"];
v2 = obj["SecondObject"];

// Assigns Nothing to one of the properties,
// since an object cannot be referred to by several properties
obj["FirstObject"] = null;

// Assigns properties
obj["SecondObject"] = v1;
obj["FirstObject"] = v2;

Note that the ODT project item includes specific means that help you walk down the hierarchy of custom objects, call object methods at each level, specify what methods to call and filter data that participates in testing. We will describe how to do this in the next step. For more information, see Controlling Object-Driven Tests.

Note that you can execute the script code described above in your keyword tests using the Run Script Routine operation.

Prev     Next

See Also

Visual Creation of Custom Objects

Highlight search results