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

Sunday 8 January 2012

Object Repository in QTP

Wooah!!! Let get into deep understanding of Object Repository (OR in Short)

As the name suggest "A place where Object is stored", but don't get confused. Object in QTP world is basically "PropertyName-PropertyValue" pair. QTP Object Repository is a Graphical representation Various Test Objects and their properties along with hierarchical relationship with other objects as well.

Their are some more functionality available in Object Repository window like . . .
 Add Objects to Repository
 Object Spy
 Associate Other Repository from file
 Highlight Selected Test Object in Application

These are very few enlisted above with Icon Image.

Now lets see how does an Object Repository GUI window looks like.

Left Pane shows the total Test Objects what is going be used by QTP during Test Run. Now these object can be added manually or by QTP itself while recording a script rather I should say a workflow.
Two thing keep in mind to get familiarize with OR.
1) Notice the hierarchical relationships among objects. People who can understand HTML DOM (Document Object Model) can easily figure out why these objects are placed in such order.
2) Notice the Icon and Name of each Object.

Right Pane shows an Object properties and its values.

Lets take an example of our Test Object having Name "Google Search"
Notice the Icon, its a WebButton and the name "Google Search" 
Look at the Right Pane now 3 Description properties are displayed like type, name and html tag.
QTP have itself added these 3 properties to uniquely identify a particular object in application.

Now you will ask me what do you mean "QTP has itself added these properties"? (Note that I am talking about object properties and not Object because I have manually added all those objects in OR, but QTP have itself added all the required properties of each object.)

When I click on Icon         QTP vanishes and Object Spy Hand  appears and placed on the object (Google Search Button) then QTP fetch all the available object properties from the application and using Object Identification Mechanism it filters out that it require above 3 properties to uniquely identify Google Search Button in Google landing page. 

QTP self assign a name ("Google Search") to Google Search button object so that automation tester can use this name as reference in his code.
Now let's around with this object, 
1. Click on highlight button  and then Google Search Button gets highlight
2. Delete  some property and see that whether you can identify (or highlight) object, I deleted property "html tag" and guess what I was able to highlight Google Search Button. And I again deleted property "type" and I was able to highlight Google Search Button. And I again deleted property "name" and guess what QTP showed me 

What did I concluded here.
QTP sometimes take extra precaution to uniquely identify object hence add some more properties without which it can still identify object uniquely.
I have learned that the more number of properties you add, QTP will take more time to analyze object. Hence I started deleting properties.

Okay pals try yourself . . .
Happy Learning . . . :)

-Pankaj Dhapola