In this tutorial you will learnwhat descriptive programming in QTP is, and Database connection in QTP i.e. how to connect to external data sources like database and MS Excel sheets using QTP.
Descriptive programming is a mechanism for creating tests where you use “Programmatic description” of objects instead of recording them.
Using this technique QTP can be made to identify objects that are not in the repository.
There are 2 variations of descriptive programming:
1) Static Descriptive programming
2) Dynamic Descriptive programming
Static Descriptive programming
Static method is when you try to access an object by using a set of properties and values directly in a VB statement.
Syntax: TestObject(“Property name1:=property value”,”property name 2:=property value”,….n)
This is how you use it:
1. Browser(“creationtime:=0”).Page(“title:=Google”).WebButton(“name:=Google Search”)
Dynamic Descriptive programming
This works by creating a description object. Look at the following example to create a webButton object.
1. Set Testdesc=description.create
2. Testdesc(“micClass”).value= “webButton”
micClass refers to the predefined classes in QTP. The values you can assign can be webbutton, weblist etc.
In QTP 10 micClass values are case sensitive but in QTP 11 onwards they are not. If you write webbutton in QTP 10 it will fail. You will have to write webButton. But the same webbutton will pass in QTP 11.
You can extract all the objects of a certain class in a page by using the following statement:
1. Set ObjectList=Browser(“creationtime:=0”).Page(“title:=*”).ChildObjects(Testdesc)
2. Msgbox ObjectList.count
The above set of statements will extract all the buttons on a page and store them in the ObjectList object.
The versatility of using descriptive programming is that these lines of code will work on any open page. You can open google.com in your browser and it will count how many buttons are on that page. It will work exactly the same way if you had amazon.com or any other site open.
This is because we have the name of the title of the page set to * which is a regular expression.
So you can see, how we can write code that can be used in more than one occasions by not hard coding the property values and by creating the objects at run time.
Let us take our example a little further. Say I am trying to print the names of all the webbuttons on the page one after the other.
If there are 4 buttons in a page, you can access each one of them the following way:
Msgbox ObjectList (0).GetRoProperty(“name”) –This will print the name of the first button.
Msgbox ObjectList (1).GetRoProperty(“name”)
Msgbox ObjectList (2).GetRoProperty(“name”)
Msgbox ObjectList (3).GetRoProperty(“name”)
The index of the child objects starts from 0
Since the object achieves its properties at runtime, we use the GetRoProperty method to retrieve the same.
We can change the above code to work for any number of Buttons in the page by using a ‘For loop’ and repeating the statements within the ‘For block’ until it reaches the end of the object count.
1. For i=0 to ObjectList.count -1 to Step 1
2. Msgbox ObjectList (i).GetRoProperty(“name”)
Using a ‘For loop’ is better because in this case you don’t need to know how many objects are in your description object.
Couple of points to note:
1. You will need practice to master descriptive programming. No matter how many examples you look at and understand, you will need hands on experience to really work with it.
2. As a tester you are not expected to know how the objects are coded into your AUT and what values they are set to. So use ObjectSpy from time to time to choose the right properties to view the properties.
3. The test results will indicate that the test object was created dynamically during the run session using a programming description or the ChildObject methods.
Connecting to some commonly used external data sources from QTP:
There will be many instances while you are preparing your tests that you will have to connect to an external DB or some other data sources. Once connected, you also will have to move data to and from these apps to QTP and vice versa. Though it is beyond the scope of these articles to provide a complete guide for working with external interfaces, we will look into a few that are most commonly used.
Database Connection in QTP:
To connect to a database we typically use an ADO connection object. ADO is Microsoft’s ActiveX Data Objects.