This article covers the basics of Descriptive Programming in QTP with the main aim to help you get familiarized with its basic syntax. The article uses an illustrative approach to help you understand the difference between Object Repository and Descriptive Programming code. And also to guide you through the process of creating descriptive programming code.
Simple enough? Well, after reading the above definition of descriptive programming, there might be many queries that may come to your mind -
>> Why do we eliminate object repository while scripting?
>> How to skip Object Repository and write scripts using Descriptive Programming?
>> What are the advantages/disadvantages of Descriptive Programming over Object Repository?
>> Are there any situations where using Descriptive Programming would be advantageous over Object Repository?
Before we begin answering the above queries, let us first see the structure of a line of code in QTP and understand how Object Repository fits in there. Once you are clear with this concept, understanding and using Descriptive Programming in your script becomes very very easy.
Structure of a QTP Statement and Object Repository
Let’s take an example to understand the below concept. Consider that you have to write the code which enters some text in Google Search. The code that you would write with the use of object repository would look something like this -
Browser("brGoogle").Page("pgGoogle").WebEdit("q").Set "google"
You can divide the above line of code into 2 parts
a) The object hierarchy part (or simply the object part) denoted by Browser(“brGoogle”) .Page(“pgGoogle”) .WebEdit(“q”)
b) and the action part specified by Set “google”
So we can safely say that a QTP statement usually consists of 2 things – the object and the action.
Also, the object is something which is stored in the Object Repository. So when you run the above code, QTP will try to find out the object properties from Object Repository and then perform the action on that object
So when you say that you want to write your script a such a manner that it doesn’t use object repository, it actually means that you have to write the object part of your statement in a different way such that it doesn’t go to Object Repository to find the object properties. Check the below diagram that illustrates the this concept.
Now you know that in descriptive programming, you only need to modify the Object part of your code. But how do you do that? Well, the answer to this query also lies within the Object Repository.
Why does QTP refer to Object Repository while running a statement? QTP does it so that it can find the properties associated with the object from the Object Repository. So what if you directly provide the properties of the object in the code itself? If you do this, QTP finds the object properties in the code itself and therefore, there is no need for it to go and refer the Object Repository. And this is the way, you write your Descriptive Programming Code.. :–)
Writing Descriptive Programming Code
The below code snippet shows the syntax and example of Descriptive Programming approach.
1 'Descriptive Programming Syntax
2 Control("PropName1:=PropValue1","PropName2:=PropValue2", and so on..)
3 'Let's assume we have a Search button with properties as: name=Search & type=Submit.
4 'This can be written as follows using descriptive programming approach
5 WebButton("name:=Search","type:=Submit")
The below image depicts this concept more clearly.
Using this concept, you can now re-write each of the objects as follows -
Browser(“brGoogle”) becomes Browser(“title:=Google”)
Page(“pgGoogle”) becomes Page(“title:=Google”)
WebEdit(“q”) becomes WebEdit(“name:=q”,”type:=text”)
Combining the above 3 objects, the complete statement can be written as -
Browser("title:=Google").Page("title:=Google").WebEdit("name:=q","type:=text").Set "google"
Descriptive Programming Example: As an example, let us see the descriptive programming code that searches for a string in google.com and then displays the total number of results found in a message box.
'Open Google.com
SystemUtil.Run "iexplore.exe", "http://www.google.com"
'Set value in Google Search Box
Browser("title:=Google").Page("title:=Google").WebEdit("name:=q","type:=text").Set "quicktestprotech"
'Click on Search button
Browser("title:=Google").Page("title:=Google").WebButton("name:=Google Search","type:=submit").Click
'Find out the number of search results displayed
sResults = Browser("title:=.*Google Search").Page("title:=.*Google Search").WebElement("html id:=resultStats").GetROProperty("innertext")
'Display the Result in a message box
Msgbox sResults
Ended to you
This was all about the very basics of Descriptive Programming and its Syntax in QTP. In the next article, we will see some more important concepts that you can use with descriptive programming. Did you find this article useful? Let us know your thoughts about this using the comments section.
0 comments:
Post a Comment