Monday, April 10, 2006

Logical Names

When QTP learns objects from your AUT and stores them into the object repository, it assigns a logical name to each of these objects. These logical names are used in the VBScript code that QTP generates whenever it records your actions. You may see code in your test pane (Expert View) that looks like:

VbWindow("frmMain").VbButton("btnSave").Click

In this case, two different objects are referenced, frmMain which is a container and btnSave which is an object within that container. frmMain and btnSave are both logical names that are stored within our object repository. They however didn't start out with names like these. You will learn by experience that QTP tries to guess unique names for each of the object. Uniqueness is important because 2 objects in the same scope (i.e. within the same container) cannot have the same name. QTP uses internal logic to decide on what the logical name for an object would be and if the name already exists, appends a numerical index to the name. The following names are examples of this:

VbEdit, VbEdit_1, Size:, Direction

You will notice that the format of these logical names and the specific objects they represent within the AUT are a little difficult to reconcile. These are just some of the problems the QTP naming scheme creates, though I should note that none of these names are difficult for QTP to use in a test. The challenges posed by this naming pattern includes:
* The logical name does not specify the type of object that exists
* The logical name does not help to identify the object in your AUT
* The logical name is difficult to reproduce because it contains symbols, spaces and special characters

Since our goal is to simplify the process of testing, it is therefore very advisable after adding objects into your object repository to rename the objects so that you solve all the problems described above. They are many useful naming conventions that you can use here, and it is often best to use the one you are most familiar with, or if you are on a team project to use the one described in the test plan. Remember that the goal of naming conventions is to promote universality among resources shared by many people.

I use a convention known as the objDescriptiveName naming convention. You may be familiar with this naming convention under a different name, the VB Naming Convention.

The convention has two parts

obj: A three letter prefix that specifies the generic type of the object
DescriptiveName: A descriptive name that makes it easy to identify the object on the screen

You will notice that using this convention, you will not have to use any special characters or symbols thus making it additionally easy to reference your object names in your test script. Special characters are notoriously difficult to use in names and many naming conventions do not accept them at all, once we start working on VBScript syntax, you will see that VBScript prevents the use of symbols and special characters as part of names for constants and variables.

QTP however allows us to use them for logical names. The problem with these though is that it is difficult to correctly recreate the following logical name.

"<- Today's Date:" For some, it might be difficult to see that there are 2 spaces between Today's and Date, but QTP sees this. You may also miss the fact that there is a colon after Date. QTP however will not miss this fact and anything you type that does not match exactly with what you have above will be considered incorrect. You will also notice that the first letter of every word in the objDescriptiveName convention starts with an upper case character. This is another benefit of the convention and it is done in order to make it easy for you to read the name of the object. As an example, will it be easier for you to read the following: btntodaysdate or btnTodaysDate While the first form is not impossible to read, you have to spend a little more time determining where word boundaries end and new words begin. The second form however is much easier to read, and it achieves this by marking the first character of each word in upper case. You will find below a list of prefixes that I will also use to specify the type of each of the objects. I have broken these into groups:

WINDOWS TEST OBJECTS

Prefix

Object

btn

cal

cmb

chk

dlg

drv

dtp

fra

frm

lbl

lst

lvw

mnu

nud

obj

opt

pic

prg

tkb

txt

Button

Calendar

ComboBox

CheckBox

Dialog

Drive Selector

Date Picker

Frame

Form

Label

ListBox

ListView

Menu Item

Numeric UpDown (Spinner)

Object

Radio (Option) Button

Picture Box

Progress Bar

Track Bar

TextBox


DATA TABLE OBJECTS

Prefix

Object

cpt

out

dat

Parametrized Checkpoint value

Output Value

DataTable parameter