Descriptive Programming Simplified
This document demonstrates the usage of Descriptive programming in QTP 9.1. It also discusses situations where Descriptive programming can be used. Using Descriptive Programming automation scripts can be created even if the application has not been developed.
1.1 Introduction:
One of the most profound techniques necessary for advanced QTP usage is "Descriptive Programming" (DP).
Descriptive Programming is a technique used to handle automation test even though object repository is not available. Whenever QTP records any action on any object of an application, it adds some description on how to recognize that object to a repository of objects called object repository.
When using DP, we’re bypassing the native object repository (OR) mechanism, which may have many advantages (easy to generate and read code, good data organization, etc.), but is extremely not flexible. We’ll examine situations in which the OR’s advantages are outweighed by the DP’s flexibility.
In order to fully grasp how DP actually works, it’s best to first understand how QTP’s native OR works. The native OR is not some complex and deep mechanism, but rather a simple way of keeping groups of properties and values. To clarify: Whenever we add a certain window to the OR, QTP gathers the window’s/web properties (e.g. – Height="400", Title="New Entity", Name="NewEntityID", html tag="test"), and stores them as a group of property-value pairs. When QTP runs the script, it compares these properties and values to the "physical" objects, and finds (or fails to find) the object which they describe. Nothing mystical about it.
Below screen shots shows an example of Object repository:
QTP uses its object repository to refer to objects present in your test and which have been recorded, if you wish to use objects that were not recorded and are not present in your object repository then we use descriptive programming, where QTP does not refer to object repository but the property name and value are mentioned in the code itself for QTP to use it.
QTP cannot take action on an object until unless its object description is in the Object Repository. But descriptive programming provides a way to perform action on objects which are not in Object repository.
e.g.
This is not Descriptive programming:
Browser(“New Page”).Page(”New Page”).WebButton(“Search”).Click
This is Descriptive programming:
Browser(“name:= New Page”).Page(”title:= New Page”).WebButton(“name:=Search”, “html tag:= BUTTON”).Click
1.2 When and Why to use Descriptive programming?
Below are some of the situations when Descriptive Programming can be considered useful:
1. Functions & Recovery scenarios work with different actions.
Means: Different actions = Different ORs.
2. Can’t record certain objects:
Auto-hide panels
Objects with changing hierarchies
Nested inner-objects, Sub menus
3. The objects in the application 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, Ex. “Logout <
4. When object repository is getting huge due to the no. of objects being added. If the size of Object repository increases too much then it decreases the performance of QTP while recognizing a object.
5. When you don’t want to use object repository at all. Well the first question would be why not Object repository? Consider the following scenario which would help understand why not Object repository
Scenario 1: Suppose we have a web application that has not been developed yet. Now QTP for recording the script and adding the objects to repository needs the application to be up, that would mean waiting for the application to be deployed before we can start of with making QTP scripts. But if we know the descriptions of the objects that will be created then we can still start off with the script writing for testing.
Scenario 2: Suppose an application has 3 navigation buttons on each and every 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 this 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 just write 3 descriptions for the object and use it on any page. Although there is no limit in QTP9.1 regarding size, but I have found that the moment the size exceeds 80MB, probability of it being crashed increases...
6. Modification to a test case is needed but the Object repository for the same is Read only or in shared mode i.e. changes may affect other scripts as well.
7. When you want to take action on similar type of object i.e. suppose we have 20 textboxes on the page and there names are in the form txt_1, txt_2, txt_3 and so on. Now adding all 20 the Object repository would not be a good programming approach.
1.3 How to use Descriptive Programming?
In DP, we’re "manually" specifying the properties and values by which the relevant object will be identified. This way QTP won’t search for the properties data in the OR, but will take it from the DP statement.
There are two ways in which Descriptive Programming can be used
1. By creating properties collection object for the description.
2. Throw the properties and values straight into a command.
1. By creating properties collection object for the description.
Throw the properties & values into a description object, and throw it into the command. To use this method you need first to create an empty description. DP allows us to define a static descriptive object, and set its properties once. This way, the script simply refers to the descriptive object:
'—-Create Object—-
Dim obj_Desc
Set obj_Desc = Description.Create
'—-Set ID properties & values—
obj_Desc("property1").Value = "value1"
obj_Desc("property2").Value = "value2"
‘—-Use and reuse the description object
Winedit(obj_Desc).Type "Something"
Dialog("Microsoft Internet Explorer").WinButton("obj_Desc").Click
‘—-Release description object—’
Set obj_Desc = Nothing
Each description has 3 properties “Name”, “Value” and “Regular Expression”.
obj_Desc(“html tag”).value= “INPUT”
When you use a property name for the first time the property is added to the collection and when you use it again the property is modified. By default each property that is defined is a regular expression. Suppose if we have the following description:
obj_Desc(“html tag”).value= “INPUT”
obj_Desc(“name”).value= “txt.*”
This would mean an object with html tag as INPUT and name starting with txt. Now actually that “.*” was considered as regular expression.
This is how we create a description. Now below is the way we can use it
Browser(“Browser”).Page(“Page”).WebEdit(obj_Desc).set “Test”
When we say .WebEdit(obj_Desc) we define one more property for our description that was not earlier defined that is it’s a text box (because QTPs WebEdit boxes map to text boxes in a web page).
2. Throw the properties and values straight into a command
Throw the properties and values straight into a command by specifying property:=value pairs describing the object instead of specifying an object’s name. The general syntax is:
TestObject("PropertyName1:=PropertyValue1", "..." ,
e.g:
Browser(“name:= New Page”).Page(”title:= New Page”).WebButton(“name:=Search”, “html tag:= BUTTON”).Click
TestObject—the test object class could be WebEdit, WebRadioGroup etc….
PropertyName:=PropertyValue—the test object property and its value. Each property:=value pair should be separated by commas and quotation marks. E.g.:
WebButton("name:=Search", "html tag:= BUTTON").Click
It’s the good old syntax we know, except the string between the () is not the object repository name. It’s the property:=value identification string.
Comparison of both methods:
Method 2 is faster, best used for one or two commands. When you want to execute multiple commands on an object, method 1 is a better choice by far (allows one-time definitions, multiple uses).
We can even combine the two methods:
Browser(obj_browser).Page(obj_page).WebButton(“name:=Search”, “html tag:= BUTTON”).Click
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
No comments:
Post a Comment