Custom Search

How & Where to Use Descriptive Programming in HP QTP 11.0

How & Where to Use Descriptive Programming in HP QTP 11.0

Descriptive programming has become a choice by most QTP automation testers. In this article, I will not talk much about the advantages and disadvantages of descriptive programming, rather I will focus much on how and where to use the technique of descriptive programming.

Descriptive programming in QTP is a way to interact with the objects that are not there in the Quicktest Professional Object repository.

Before discussing Descriptive programming in details, I will firstly discuss how QTP identifies objects present in the Object Repository.

The best example would be to record on the Google's textbox which is shown as below:



As seen from the above screenshot, Google search textbox has been contained inside the Object repository with the name "q" which is under a two level hierarchy. The textbox is contained inside the Page object which is again contained inside the Browser object. "q" is the logical name of object and no two objects can have the same logical name under the same hierarchy.

Inside the Object Properties, we can see that class for this particular bject is displayed as "WebEdit". So, we can conclude that web application textboxes maps with the QTP's WebEdit class.

Now to recognize google textbox, QTP uses three mandatory properties "type", "name" and "html tag".

Now, the question arises: where these three properties came from? The answer is, QTP takes these properties from Object Identification screen which can be opened from Tools-->Object Identification.

Now from the above screenshot, it is clear we have defined three mandatory properties for the textbox object. So on recording on any textbox, QTP will take these three properties only. However, we can change these properties if required and from now onwards QTP will take these new properties upon recording. However, existing objects will remain untouced which means they will retain their old properties.

Coming back to the first Object Repository screenshot, we see that QTP has used 3 properties to identify the objects uniquely but in actual, we can remove "type" and "html tag" property and only keep the "name" property. This is because on Google homepage, there is no other textbox with the name as "q" so for this particular object we can keep only 1 mandatory property.

Lastly, the logical name "q" has been taken from the "name" property only.

Sounds Interesting? Alright, keep reading!!

What is Descriptive Programming in QTP?

The main idea behind the descriptive programming is to provide more flexibility to the automation developers which is too choose the properties they would like to use in order to identify the object rather than relying on QTP to choose properties arbitrarily. Choosing appropriate properties for an object comes with experience and if done correctly creates robust scripts.

In simple terms, it is more flexible but it does require manual selection of the object properties so creating a script for lot many objects can be very much time consuming.

When and where to use the Descriptive Programming?

I will list few examples where descriptive programming is more helpful than using QTP's object repository.

1) The objects in our application under test are dynamic. For example, if you want to click on a link that says "Welcome Mr Jones. Today is 10th August, 2012". Now in this link the name "Mr Jones" and date "10th August, 2012" is dynamic one as it will keep on changing for different users and same goes for date as well as if run tomorrow will generate a new date.

2)In case we wnt to start with the automation before the application is available to use. However, this is not a very common approach. 

3)To prevent QTP object repository from getting huge as the performance of script degrades if the Object repository size increases substantially.

Now I will list down few scenarios where descriptive programming is the best method available to tackle the issues:

1)We wish to find out the number of links contained in a webpage which keeps on changing every time the page is loaded. This can only be done through Descriptive programming.

2)We want to check all the checkboxes inside a window which are changing dynamically. In this case, descriptive programming is the only way to approach this issue.

3)In a webpage there are 10 textboxes and we want to put data in each textbox. Also, the number of textboxes is changing dynamically. Again we can use descriptive programming in this case.

4)We have 4 different Internet Explorer windows and we want to find the count of these windows and close these windows.

How to use Descriptive Programming in QTP?

We have two ways of using Descriptive programming:

1) Providing the set of different properties & the values directly in the QTP statement.

We can provide the appropriate properties directly inside the statement in property:= value format

For example, in order to set something in Google search textbox, we can use a recorded statement like 

Now the above statement can be written in Descriptive programming style for WebEdit object as below:



As I said earlier, we can choose which properties we would like to use to identify the textbox object. So we can modify the above statement as:



As shown above, we can add and remove properties as required.

Instead of using the hardcoded logical name inside the statement we can store the name inside a variable and use the variable as:



If you are using QTP 9.5 and above, you can skip the browser object and use the following statement as well:



You can also skip the page object and use the following statement:




2) Use the Description Object

In this approach, we create a Description object and then define properties and values for object identification. I will try to use the same example of setting the text in Google search box but this time using Description Object.



In the above code, objBtn is an object and we have added 3 properties to our collection. By default, all the property values are considered as Regular expressions in QTP. So, to make sure the property is not a regular expression, set as False.

Now we can use the above property collection object as shown below:



Hierarchy of test object description

Specifying the properties and values directly inside the QTP script for identifying the object without using Object repository is known as programmatic description.

When using Descriptive programming syntax in a statement from a particular point, we need to use the programmatic descriptions beyond that particular point onwards within the statement. However If you happen to specify a test object by the name of its object repository after the parent objects in this similar hierarchy where it is being described with the use of programmatic descriptions, QTP can't identify this object and will throw an error.

For example, following statement can be adopted as it uses the programmatic description throughout the hierarchy:



The following statement can also be used as it uses the programmatic description from a specific point onwards(starting from Page object)

In the above code, "Browser" is the logical name used from Object repository while the rest of the code uses programmatic description. You can also try deleting the Page object from the Object repository and the above code will still work as the Browser object is still present.

However, the following code will not run if you attempt to adopt the Object Repository name for the textbox if our parent objects have been specified by programmatic descriptions.



The reason for the failure is, in order to get an object identified successfully, its parent object has to be present in the statement which is not there in the above case.

Regular expressions in QTP

Consider the following code:



Now, if there is an object inside the application which has the name : This$isdummy$name, it will not work because the name contains regular expression characters and by default regular expression is enabled. So in order to fix this issue, we have two workarounds:

1) Use a backslash character to instruct Quicktest to treat the special character as literal characters

For example, the following code will work



What we have done is added a backslash just to the left of the regular expression characters.

2) Use the description object

Now, its upto you which approach you desire to use. Providing the properties as string arguments takes up less memory while using Description object takes up more memory as an object is created inside the memory.

MicClass in QTP

micclass represents the type of QTP test object. It is the default property of any QTP test object. For example, Browser("CreationTime:=0") is equivalent to



If there is only a single textbox on a webpage, then instead of providing any of the default mandatory properties, we can even use micclass

For example:

As seen in the above QTP statement, the micclass for any test object would be the object itself. For example the micclass of Browser object would be the browser and for webedit, it would be webedit. The point is that we have to specify atleast one property as string arguments in the case of programmatic description. In case we dont have any property, we can specify micclass as the default property.

For example :



Whether we can use Descriptive Programming with Smart Identification?

Smart Identification successfully functions with Object Repository. However the Object Repository is bypassed in case of Descriptive Programming. Hence the mechanism of Smart Identification is not usable with Programmatic Description or Descriptive Programming.

Descriptive Programming Object Repository

Rather than storing Object properties inside the QTP object repository, we can store the object properties inside the variables or constants. For example,



Descriptive Programming Examples

1) Display all links present inside Google Homepage



Explanation:
 ChildObjects is a built-in method in QTP which is used to find all the objects contained inside an object. From the above code, we are looking to find all the objects inside "Google" page which have the "html tag" as "A" which means a link.

ChildObjects basically returns an array of objects. In the above example objLinks(0) would refer to 



Issues with Regular Expressions in Descriptive Programming

Some properties can't become regular expression. For example, you can not have the micclass property as a regular expression in QTP.

For example, consider the following code:



If we use the above collection object with ChildObjects, it returns a count of zero which looks weird. As a smart workaround, we can use individual objects "WebEdit", "WebCheckBox" , "WebRadioGroup" as the value of the micclass.

There is another workaround which is not so common. We can use insert function on the collection returned by the ChildObjects method. 



This would give count of all textboxes, checkboxes and radiobuttons inside a webpage.

QTP Closes all browsers using Descriptive Programming

The simplest code to close all browsers using QTP is:



In order to close a particular browser using its Window handle (hwnd), we can use the following code:



There is another way to close a particular browser using its Window title as shown below



However, we can also use Childobjects to close all the open browsers.



We can simply modify the above code slightly as described below:



There is another way for closing all the browsers which is shown below:

CreationTime is an ordinal identifier property which is used for Browser object only. It specifies the order in which the browsers are opened. The very first opened Browser has the CreationTime as 0 and so on.

The above code will close the Browser which has the CreationTime as 0. Then the browser which has the CreationTime as 1 will become 0 and so on.

In case we are running our tests from Quality Center and we want the above code to close all browsers except Quality Center, we can modify the above code as shown below:

There is another way to close all browsers using Windows Management Instrumentation(WMI) as shown below:

Using WMI technique, we can close any particular process from the Task Manager

Browser not really required

In web applications, we use the code something like the following:

Browser("micclass:=Browser").Page("micclass:="Page").WebEdit("name:=q").Set "Quicktest"

We can modify the above code as:

In the above code, we have omitted the Page object and the QTP code would still work fine.

In case you are using QTP 9.5 or higher version, we can even omit Browser object so that the code would look like:

This code would work perfectly fine in QTP 9.5 or higher.

Using SetToProperty with Descriptive Programming

Consider the following code:



Now if we open Google browser and run the following code,



The above code would still return true as SetTOProperty works only with object repository and not with Descriptive programming.

No comments: