Sunday 22 January 2012

Descriptive Programming in QTP

What do you mean by Descriptive Programming (DP in Short)?

In QTP, An Automation script developer can write script codes using Object Repository or Descriptive Programming. Descriptive Programming is nothing but working with Object's property name and property value. As we all know QTP identify objects on the basis of object properties which is ultimately known as Descriptions.

Now in QTP, How do we write scripts in DP to perform an action like entering string in text field, clicking button etc. . .?

Ummm . . .

To Identify object uniquely we need to find descriptions or properties of an Object. How do we find properties? It's Object Spy. Descriptive code can be written in two ways

  • Mention your Descriptions using QTP inbuilt programmed Description Object. (Note here Description object is different than the GUI objects like WebButton, WinButton etc, Description Object is programming concept.)
  • Another way is to mention your description in string format.

Let's take an example . . .

I have adjacent html WebPage that I developed during my college days when I first learned HTML ;)

I have to write a script to enter firstname, lastname and then click on Submit button.

Now, to write script using Description Object you would require to create a description object and use its add method to add "propertyname -propertyvalue" pair, for that write below code

Set objDesc = Description.Create
objDesc.add "propertyname1", "propertyvalue1"
objDesc.add "propertyname2", "propertyvalue2"


Use this code to write script for object
QTPObject(objDesc )



Wait this is not the end, I want to uniquely identify the TextBox besides "First Name" Label. so for this I need the properties of the "First Name" TextBox and for that naturally I will use QTP object spy.

Let's see what Object spy has to say. . .
Look at the 3 property and value pair for First Name WebEdit (or Text Box)
class = fntxtclass
html tag = INPUT
name = fntxt 

According to hierarchy of "First Name TextBox" Object, with little knowledge of DOM I realized that to reach this textbox I need to identify Browser(MyFirsthtmlcode) first and then Page(MyFirsthtmlcode) and then WebEdit(fntxt)

okay let me go with First Name Textbox or WebEdit(fntxt)


Can you relate things in browser, HTML source page and object spy? Try to understand how are WebObjects being interpreted in QTP environment.

Now I decide to use these 3 properties for identifying the "First Name" TextBox (or WebEdit) in our description object.
Let's see how do i do that . . .

Set objFirstNameTextBoxDescription = Description.Create
objFirstNameTextBoxDescription.add "class","fntxtclass"
objFirstNameTextBoxDescription.add "html tag","INPUT"
objFirstNameTextBoxDescription.add "name","fntxt"

now this object is mentioned in WebEdit object as

WebEdit(objFirstNameTextBoxDescription)


Lets use the same process to identify Browser and page, I figured out below code for both Browser and Page Object


Set objMyBrowserDescription = Description.Create
objMyBrowserDescription.add "name","MyFirsthtmlcode"

As

Browser(objMyBrowserDescription


Set objMyPageDescription = Description.Create
objMyPageDescription .add "class","fntxtclass"

As 

Page(objMyPageDescription)



Now lets see whether we have uniquely identify or not, for that I used highlight method for each objects.
Notice hierarchy in the code as well.

Browser(objMyBrowserDescription).highlight
Browser(objMyBrowserDescription).Page(objMyPageDescription).highlight
Browser(objMyBrowserDescription).
Page(objMyPageDescription).
 WebEdit(objFirstNameTextBoxDescription).highlight

guess what!!! "First Name" TextBox was highlighted

And then I used the most common method of WebEdit object i.e. set() method to enter value in our text box.

Browser(objMyBrowserDescription).Page(objMyPageDescription).
 WebEdit(objFirstNameTextBoxDescription).Set "Pankaj Dhapola"

Now Another way without using Description Object is to enter Description properties in String format like this
QTPObject("PropertyName1:=PropertyValue1","PropertyName2:=PropertyValue2", . . . .)

Above equivalent code in String format is



Browser("name:=MyFirsthtmlcode").Page("name:=MyFirsthtmlcode").
WebEdit("class:=fntxtclass","html tag:=INPUT","name:=fntxt").Set "Pankaj Dhapola"



Try it yourself friends.
But notice the case sensitivity inside the String and Description object where you provide the Descriptions or "PropertyName:=Value" pair.
-Pankaj Dhapola

No comments: