Descriptive Programming is one of the most useful, simple yet often confused concepts of QTP. This article will serve as a complete guide on Descriptive Programming.
Descriptive Programming (also known as Programmatic Description) provides a way to perform operations on objects that are not present in object repository.
When and where is it used? How is it written? We will see complete details below. Please note that the words Descriptive Programming and Programmatic Description can be used interchangeably. We will be using Descriptive Programming throughout this guide.
Listed below are some of the situations where Descriptive Programming can be considered useful:
Handling Dynamic Object Property
One of the very useful places where you can use Descriptive Programming is when the object properties in the Application Under Test (AUT) are dynamic in nature and need special handling to identify the object. The best example would be of clicking a link which changes according to the user of the application.
As can be seen in the example above, the text property of Link object shown above changes according to the username. It is easier to handle such properties with descriptive programming.
Using External Function Library
Another place where DP can be of significant importance is when you are creating functions in an external file. You can use these function in various actions directly , eliminating the need of adding object(s) in object repository for each action. [If you use local object repository]. This forms the basis of keyword driven framework approach.
Huge Object Repository
When object repository is getting huge due to the number of objects being added. Bulky object repository may decrease the performance of QTP while recognizing an object. [For QTP 8.2 and below, Mercury – which was subsequently acquired by HP – used to recommend object repository size less than 1.5 MB]
When Application is Not Ready Yet
Suppose we have a web application that has not been developed yet. Now for QTP to record the script and add the objects to repository, needs the application to be up, that would mean waiting for the application to be deployed before we can start making QTP scripts. But if we know the descriptions of the objects that will be created, we can start-off with the scripts using Descriptive Programming.
Object Repository in read only or shared mode
Let’s say you wish to modify a QTP script but the Object repository for the same is read-only or in shared mode i.e. changes may affect other scripts as well. In such a case, you may use Descriptive Programming approach.
Several Identical Objects needing Same Operations
On the Same Page: Suppose we have 15 textboxes on a web page and there names are in the form txt_1, txt_2, txt_3 and so on. Now adding all 15 objects in the Object repository would not be a good programming approach (since the object description would be the same except the index ordinal identifier.) We can simple go for Descriptive Programming.
On Different Pages: Suppose a web application with several pages has 3 navigation buttons on each page. Let the buttons be “Cancel”, “Back” and “Next”. Now recording action on these buttons would add 3 objects per page in the repository. For a 10 page flow, it would mean 30 objects which could have been represented just by using 3 objects. So instead of adding these 30 objects to the repository, we can write 3 descriptions for the object and use those descriptions on any page.
It should be noted that Descriptive Programming can be used across technologies and add-ins in QTP. It is not limited to a particular technology. In the examples that follow, we will use web based objects.
There are two ways to create Descriptive Programming statements:
1. By giving description in form of the string arguments. (also known as inline Descriptive Programming or static Descriptive Programming)
This is a commonly used method for writing Descriptive Programming statements.
In this method, property:=value pairs describe the object. The general syntax for inline descriptive programming is:
TestObject("PropertyName1:=PropertyValue1", "PropertyName2:=PropertyValue2",……, "PropertyNameN:=PropertyValueN")
- TestObject — is the test object class. This could be Browser, WebEdit, WebRadioGroup etc
- PropertyName:=PropertyValue — is the test object property-value pair. Each property:=value pair should be enclosed in quotes and separated with other pairs with a comma. Property and its value is separated with a colon(:) and equals to (=) sign. There should be no space between the := sign and property-value pair hence while TestObject(“PropertyName1:=PropertyValue1”) is acceptable, but this isn’t TestObject(“PropertyName1 := PropertyValue1”)
Let us write an inline descriptive programming statement for username box of demo flight reservation application.
A normal recorded statement for username would like
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set "mercury"
We will create a DP statement in place of the recorded statement above. Here is the generic process that one should follow.
- Record the object for which you wish to write a DP statement.
- Open the repository corresponding to the object(s).
- Note the property-value pairs used by QTP to identify the object.
- Write them using the syntax discussed above.
QTP does not record any property for a browser or a page object.
For a browser object, there is no other property required in case you are dealing with a single browser.
Browser object shown above can be written as Browser(“micclass:=Browser”)
Page object shown above can be written as Page(“micclass:=Page”)
WebEdit object shown above can be written as
If you notice we have not written, miclass property-value pair for this object type. Actually micclass is an implicit property which is taken automatically since QTP assumes that based on the test object type being referenced. In case of Browser or page object, you can provide any one such implicit property.
micclass, html tag are some examples of implicit properties.
Putting it all together, the statement above would look like this when written in Descriptive Programming
Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebEdit(“type:=text”,”name:=userName”,”html tag:=INPUT”).Set "mercury"
Copy Identification Properties to Clipboard
In UFT 11.5 and above, you can also use the Copy Identification Properties to Clipboard option in the Object Spy dialog box to copy all identification property-value pairs for a selected object to the clipboard.
The copied values are formatted in standard DP syntax with line breaks between each property-value pair.
You can paste the copied data to any document and then copy selected lines – after removing the line breaks- into a DP statement. Please note that you won’t be using ALL the properties to identify the object. As a rule of thumb, you should use a minimum set of properties to identify an object.
2. By creating properties collection object for the description. (also known as dynamic Descriptive Programming)
Properties collection does the same thing as string arguments. The only difference is that it “collects” all the properties of a particular object in an instance of that object. Now that object can be referenced easily by using the instance, instead of writing “string arguments” again and again. It is my observation that people find 1st method easier to work with.
The process of writing DP statements remains almost the same as inline DP. However, in this case the property value pairs are written as part of a description object.
For this example, we will create a description object for WebEdit.
Dim oDesc ‘Declare an object variable Set oDesc = Description.Create ‘Create an empty description
Now we have a blank description in “oDesc”.
oDesc(“type”).value= “text” oDesc(“name”).value= “userName” oDesc(“html tag”).value= “INPUT”
The statement above would look like this when written using this approach.
Note for advanced users: Each description object’s property supports a value as well as a RegularExpression assignment. If the name value above was username786 and you wanted the last three digits as a regular expression, you could write the same statement as
If you wish to turn off regular expression property for a particular property you can use
Both the forms described above support usage of Regular Expressions by default. Check our regular expressions guide if you wish to learn about them in detail.
The first image shown at the top has a dynamic username. If you wish to identify that as a regular expression using inline descriptive programming, it can be written as
Browser(“QTP Training”).Page(“QTP Training”).Link(“text:=Go To Next Page user\d\d\d”, “html tag:=A”).Click
Using dynamic DP, it would be written as
Dim oDesc ‘Declare an object variable Set oDesc = Description.Create ‘Create an empty description oDesc(“text”).value= “Go To Next Page user\d\d\d” oDesc(“html tag”).value= “A” Browser("QTP Training").Page("QTP Training").Link(oDesc).Click
In the example mentioned above, if you wish to substitute a variable instead of using a regular expression, it can be written as
Dim uName uName = user786 Browser("QTP Training").Page("QTP Training").Link(“text:=Go To Next Page "&uName, “html tag:=A”).Click
You can also use any of the three ordinal identifiers (index, location, in descriptive programming. If you have 2 username boxes one below the other in mercury demo app, they can be written as –
Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebEdit(“type:=text”,”name:=userName”,”html tag:=INPUT”,”index:=0”).Set "mercury" Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebEdit(“type:=text”,”name:=userName”,”html tag:=INPUT”,”index:=1”).Set "mercury"
- When using Descriptive Programming from a specific point within a test object hierarchy, you must continue to use Descriptive Programming from that point onward within the same statement. If you specify a test object by its object repository name after other objects in the hierarchy have been described using Descriptive Programming, QTP will not be able to identify the object.
For example, you can use Browser(Desc1).Page(Desc1).Link(desc3), since it uses Descriptive Programming throughout the entire test object hierarchy.
You can also use Browser(“Index”).Page(Desc1).Link(desc3), since it uses Descriptive Programming from a certain point in the description (starting
from the Page object description).
However, you cannot use Browser(Desc1).Page(Desc1).Link(“Example1”), since it uses Descriptive Programming for the Browser and Page objects but
then attempts to use an object repository name for the Link test object (QTP tries to locate the Link object based on its name, but cannot
locate it in the repository because the parent objects were specified using Descriptive Programming).
- QTP evaluates all property values in Descriptive Programming as regular expressions. Therefore, if you want to enter a value that contains a special regular expression character (such as *, ?, or +), use the \ (backslash) character to instruct QTP to treat the special characters as literal characters.
In the above example, if we had parenthesis around username, it would have been written as
Browser(“micclass:=Browser”).Page(“micclass:=Page”).WebEdit(“type:=text”,”name:=\(userName\)”,”html tag:=INPUT”).Set "mercury"
since parenthesis is a special regular expression character and in this case you would want to treat it as a literal.
- micclass is shown as Class Name in object spy. If you use Class Name in DP, QTP will throw an error. Make sure to use micclass. Check Class Name vs Class vs micclass in QTP.
- Somehow there is a myth in QTP community that DP can be used when QTP is unable to identify an object using normal means. Descriptive Programming provides a way to bypass object repository and gives a bit of flexibility to identify the object. However, if even after adding appropriate add-ins you are unable to identify an object using normal means, don’t assume that DP can come to your rescue.
If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.