1. Creating Classes Programmatically

Applies to TestComplete 15.42, last modified on September 08, 2022

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.

We begin creating the hierarchy of custom objects by defining classes. In our example (see Creating Custom Objects Programmatically), we are going to create three classes: ClsNotepadTest, ClsFile, ClsOperation, ClsFindData and ClsReplaceData classes. We will start with the class of the highest level, ClsNotepadTest, and then continue with the others.

To create a new class, use the Classes.Declare method:

JavaScript, JScript

ODT.Classes.Declare( class_name );

VBScript

ODT.Classes.Declare( class_name )

DelphiScript

ODT.Classes.Declare( class_name );

C++Script, C#Script

ODT["Classes"]["Declare"]( class_name );

This method returns a Class object that provides access to the class in scripts and keyword tests. Use the methods and properties of this program object to add methods and properties to the newly created class. You can also create a new class when adding a property to any existing class via the AddPropOfClassType method.

The following code creates the ClsNotepadTest class:

JavaScript, JScript

TestClassObj = ODT.Classes.Declare("ClsNotepadTest");

VBScript

Set TestClassObj = ODT.Classes.Declare("ClsNotepadTest")

DelphiScript

TestClassObj := ODT.Classes.Declare('ClsNotepadTest');

C++Script, C#Script

TestClassObj = ODT["Classes"]["Declare"]("ClsNotepadTest");

Once we have created a class, we can add methods and properties to it. The ClsNotepadTest class will contain two properties and one method: The first property, ID, will hold the identifier of the current test. The second property, Files, will hold an array of program objects that will be used to test the text files. The Init method will perform initialization of the test.

All classes you create in tests are added to the ODT and Classes editors upon running the tests (unless you delete the classes via the script or keyword test). This is possible due to the fact that the classes are saved to the current TestComplete project.

To add methods and properties to a class, we can use the AddMethod, AddProperty, AddPropOfArrayType and AddPropOfClassType methods. The code below adds methods and properties to the ClsNotepadTest class. The code is followed by a description:

JavaScript, JScript

TestClassObj = ODT.Classes.Declare("ClsNotepadTest");
// Adds the Init method to the class
TestClassObj.AddMethod("Init", "MainUnit.ClsNotepadTest_Init");
// Adds the ID property
TestClassObj.AddProperty("ID", "");
// Adds the Files property
TestClassObj.AddPropOfArrayType("Files");

VBScript

Set TestClassObj = ODT.Classes.Declare("ClsNotepadTest")
' Adds the Init method to the class
TestClassObj.AddMethod "Init", "MainUnit.ClsNotepadTest_Init"
' Adds the ID property
TestClassObj.AddProperty "ID", ""
' Adds the Files property
TestClassObj.AddPropOfArrayType "Files"

DelphiScript

TestClassObj := ODT.Classes.Declare('ClsNotepadTest');
// Adds the Init method to the class
TestClassObj.AddMethod('Init', 'MainUnit.ClsNotepadTest_Init');
// Adds the ID property
TestClassObj.AddProperty('ID', '');
// Adds the Files property
TestClassObj.AddPropOfArrayType('Files');

C++Script, C#Script

TestClassObj = ODT["Classes"]["Declare"]("ClsNotepadTest");
// Adds the Init method to the class
TestClassObj["AddMethod"]("Init", "MainUnit.ClsNotepadTest_Init");
// Adds the ID property
TestClassObj["AddProperty"]("ID", "");
// Adds the Files property
TestClassObj["AddPropOfArrayType"]("Files");

To add the Init method to the ClsNotepadTest class, we called the AddMethod method of the Class object. This method uses two parameters: the first parameter specifies the name of the new method, the second - the script routine that will be used as a method. You can omit the second parameter. In this case, you will have to specify a script routine for the method in every object (class instance) that is based on the class. You can specify different script routines for methods in different class instances. This allows you to create objects containing methods that have the same name, but perform different operations. The script routine must be specified using the following syntax: unit_name.routine_name (both unit_name and routine_name are required).

To add the ID property, we used the AddProperty method. The first parameter of the AddProperty method specifies the name of the added property. The second parameter specifies the default property value. That is, in all objects that are based on the class, the property will store this value until you change it in scripts or keyword tests. In our example, we used an empty string to specify the property values. However, properties can store any Variant-compatible value: number, string, date, array, and so on. The only restriction is that the default property value cannot be an object reference (see the method description for details).

The AddProperty method returns the PropertyDeclaration object that provides a scripting interface to the property. For instance, you can use properties of this object to modify the name or value of the added property. Note that when you are modifying a property name in a class, the property name is modified in all existing objects that are based on this class. A similar principle is used for the property value: when you are changing a value of the property in a class, the value of object properties is also changed on condition that you have not modified this value since the objects creation.

To create the Files property, we used the AddPropOfArrayType method. Unlike AddProperty, it returns an array. AddPropOfArrayType returns the ArrayType object which you can use to add, modify and delete array items. The array in a class serves as a base for arrays in objects. For example, if you add ten elements to an array in a class, then each object based on this class will contain an array property with ten items in this array. Of course, later you can add and remove items from arrays in objects and classes. For more information on this, see the description of methods and properties of the ArrayType and ArrayObject objects. We will not add items to the array.

One important note: the method or property name is used to address this method (property) in scripts and keyword tests. That is why the method or property name must match the naming rules of the project’s scripting language (it must be a valid identifier).

We have finished creating the ClsNotepadTest class. Now we can create four other classes, ClsFile, ClsOperation, ClsFindData and ClsReplaceData:

  • ClsFile will contain two properties, Path and Operations. Path will specify the full path to the given file. Operations is an array property. It will hold references to objects that represent operations with the file in Notepad. In addition, ClsFile will contain two methods. The first, Load (mapped to MainUnit.ClsFile_Load), will load the file to Notepad. The second, Check (mapped to MainUnit.ClsFile_Check), will check to see if the file was loaded successfully.
  • ClsOperation will contain the following properties and methods:
    • ID - Property. Identifier of the operation.
    • MenuPath - Property. Path to the operation in Notepad’s menu. In the path, submenus are separated with | (without spaces).
    • Values - Array property. It will hold the values to be input for the given operation.
    • Execute - Method. Performs the operation with the specified data. We will map this method to the script routine MainUnit.ClsOperation_Execute.
  • ClsFindData will hold one property, FindText, which will specify the text we will search for using the Edit | Find... operation in the current file loaded in Notepad. In addition, ClsFindData will contain two methods. The first, Find (mapped to MainUnit.ClsFindData_Find), will perform the search-for-text operation. The second, Check (mapped to MainUnit.ClsFindData_Check), will check to see if the search-for-text operation was performed successfully.
  • ClsReplaceData will hold two properties: FindText, which will specify the text we will search for using the Edit | Replace... operation in the current file loaded in Notepad, and ReplaceWithText, which will specify the text that will replace the text that is found. In addition, ClsReplaceData will contain two methods. The first, Replace (mapped to MainUnit.ClsReplaceData_FindReplace), will perform the search-and-replace-text operation. The second, Check (mapped to MainUnit.ClsReplaceData_Check), will see if the search-and-replace-text operation was performed successfully.

We will not be covering the creation of these classes in detail, since the steps are similar to those we have just described for the ClsNotepadTest class.

Prev     Next

See Also

Creating Custom Objects Programmatically

Highlight search results