2. Visual Creation of Classes

Applies to TestComplete 15.47, last modified on January 20, 2023

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 start creating the hierarchy of custom objects by defining classes. In our example (see Visual Creation of Custom Objects), we are going to create five classes: ClsNotepadTest, ClsFile, ClsOperation, ClsFindData and ClsReplaceData. We will start with the class of the highest level, ClsNotepadTest, and then continue with the others.

To create a new class:

  1. Add the ODT project item to your test. For this purpose, use the Create Project Item dialog.
  2. Open the ODT or the Classes editor in the Workspace panel.
  3. In the tree view, right-click the Classes item of the ODT editor or the ODT | Classes item of the Classes editor and select New Item from the context menu. TestComplete will create a new class and call it NewClass.
  4. Change the class name to ClsNotepadTest. To do this, either right-click the class name in the tree view of the ODT or Classes editor and select Rename from the context menu, or select the class name and press F2. Then you will be able to type the new name.
    Note: The name OleObject is reserved. You cannot create classes with this name.

OK, we have created a new class. Now 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 the array of the program objects that will be used for testing the text files. The Init method will perform initialization of the test.

Note that method and property names are used to refer to these methods and properties in scripts and keyword tests. That is why they must match the naming rules of the project scripting language (they must be valid identifiers). The property and method names are case-sensitive in JScript, C++Script and C#Script scripts, and case-insensitive in VBScript and DelphiScript scripts.

Let's create the ID property:

  1. Right-click the ClsNotepadTest class in the tree view of the ODT or Classes editor and select New Item from the context menu. TestComplete will create a new property and name it NewProperty. The new property is represented as a child of the ClsNotepadTest class in the tree view.
  2. Change the property name to ID. To do this, either right-click the property name in the tree view and select Rename from the context menu, or select the property name and press F2. Then you will be able to type the new name.
  3. The Value column of the Properties page holds the default value of the property. This means that in all objects that are based on the class, the ID property will store the default value until you change the property value in the ODT or Data editor or from your scripts or keyword tests. We will leave the default value empty.

Now we can create a property that will hold the array of files:

  1. Create one more property for the ClsNotepadTest class. (Right-click the ClsNotepadTest class in the tree view and select New Item from the context menu.)
  2. Name the new property Files.
  3. From the drop-down list of the Value column, select (Array) for the property. We will not create array items now, we will do this later.
    Note: Properties can store an ordinary value (integer, string, date, etc.), an array or an object. To indicate that a property holds a reference to an object or an array, TestComplete displays the object's class name or the string Array in parenthesis, for example, (MyClass) or (Array). If you enclose this name in quotes, TestComplete will treat the property value as a string, for instance, "(MyClass)" or "(Array)".

    If a property stores an object that has not been created via the ODT or Data editor, for instance, one of TestComplete programming objects or any other OLE object (e.g. Word.Application), TestComplete displays (OleObject) as a property value. You can see this value in the editor during the test run.

    To store a boolean value, type true or false. If you want to specify a string value holding the true or false string, enclose that string in quotes (that is, "true" or "false").

Now let's add methods to the ClsNotepadTest class:

  1. Switch to the Methods tabbed page.
  2. Right-click somewhere on the page and select New Item from the context menu. TestComplete will create a new method and name it NewMethod.
  3. Change the method name to Init.
  4. The Script Procedure column of the Methods page specifies the script routine that will be used as the object's method by default. Click this column and specify the script routine using the following syntax: unit_name.routine_name (both unit_name and routine_name are required). Specify the name of the routine that should be used to initialize the ClsNotepadTest instances.
    Routines that are called by methods should be declared in advance, otherwise the routines cannot be found by ODT classes. That is why we have created the routines code at the previous step.
    In the MainUnit unit this routine is named ClsNotepadTest_Init. That is, you should specify MainUnit.ClsNotepadTest_Init in this column. Here and after, the names of class and object methods are given as they were declared in the provided code snippets. If you have another name for unit or routine, specify the actual names instead.
  5. Repeat steps 2 - 4 to create the Close method of the ClrNotepadeTest class. For this method, specify the MainUnit.ClsNotepadTest_Close script routine.

We have finished creating the ClsNotepadTest class. Now we can create the other four 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. (Please do not create any array elements. We will do this later). In addition, ClsFile will contain two methods. The first method, 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. (Please do not create any array elements. We will do this later).
    • 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, that will specify the text we will search using the Edit | Find... operation in the current file loaded to Notepad. In addition, ClsFindData will contain two methods. The first of them, 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 that will specify the text we will search using the Edit | Replace... operation in the current file loaded to Notepad, and ReplaceWithText that will specify the text that will replace the found text. In addition, ClsReplaceData will contain two methods. The first method, Replace (mapped to MainUnit.ClsReplaceData_FindReplace), will perform the search-and-replace-text operation. The second, Check (mapped to MainUnit.ClsReplaceData_Check), will check to see if the search-and-replace-text operation was performed successfully.

We will not describe 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

Visual Creation of Custom Objects

Highlight search results