Hello friends!
In Part-1 and Part-2 of this post, we learned type of DP and how to use DP. In this part, we'll cover -
- Using childobjects
- Using GetROProperty
Childobjects and Getroproperty are two most commonly used and i think most useful methods for DP.
Life is easy while working on a stable application where objects are not changing dynamically. But imagine the situation where there are only one or two properties found by QTP for a particular object, and these are also not constant!!
Childobjects may save your day!! Yess!!
You can do a lot using Childobjects and Getroproperty. Lets start with simple things in this post.
Childobjects:
Childobjects the collection of child objects contained within particular object.
Means???
Okey. Consider the below screen -
This is a Login dialog, contain few other object i.e. Agent name, password, buttons.
In this case Login Dialog is parent and Agent name, password, buttons are child objects.
How to use Childobjects method:
Suppose you have a page which gives you few options, i.e. Individual and Corporate. If you select 1st, on next page, you will see few fields like name, passport no, age, sex etc. But if you choose 2nd, you will see name, registration no, type of business etc.
Means page will be change on the basis of selection. Suppose you have 50 options, so you'll be having 50 different pages, all with different number/type of objects. You don't know how many objects will be there on any page.
Now you need to count the number of buttons on a particular screen, or, let's say number of links on a web page. How to do that?
And the answer is.. using childobjects method!!
Open the above window - Start>>Program>>QuickTest Professional\Sample Applications\flight.exe>>Login into application, and run the following code in QTP..
Set oDesc=Description.Create()
Set oAll=Window("text:=Flight Reservation").ChildObjects(oDesc)
cnt=oAll.count
Print "Total number of objects: "&cnt
For i=0 to cnt-1
Print "Class: "&oAll(i).GetROproperty("micclass")&", Text: "&oAll(i).GetROproperty("text")
Next
Above code will give you the number of objects on window. You can provide more specific description. Suppose you need to enter some value in all the textboxes on the page. You don't know how many textboxes are there on page. Run following code..
Set oDesc=Description.Create()
oDesc("micclass").value="WinEdit"
Set oAll=Window("text:=Flight Reservation").ChildObjects(oDesc)
cnt=oAll.count
Print "Total number of EditBox: "&cnt
For i=0 to cnt-1
If oAll(i).Getroproperty("enabled") Then
oAll(i).Set "Editbox:"&i
End If
Next
Here oAll is an array which contain all the textboxes and it's index starts from zero. So if there are total 9 textboxes on window, array index will be oAll(0) to oAll(8).
Following is one more example. You have to click on particular button (if it displayed on the page). t might come or not..
Set oDesc=Description.Create()
oDesc("micclass").value="WinButton"
Set oAll=Window("text:=Flight Reservation").ChildObjects(oDesc)
cnt=oAll.count
Print "Total number of Buttons: "&cnt
For i=0 to cnt-1
If oAll(i).Getroproperty("text")="FLIGHT" Then
oAll(i).Click
End If
Next
I was working on a application, where you need to work on two instances of the application at the same time.(1st as customer and 2nd as trader). All properties are same because application is exactly same. In that type of situations, childobjects is the way you can survive upon!!
So it was starting learning advanced DP uses. Explore above codes and try to get good understanding of childobjects and getroproperty methods.
In next part of this post we'll cover more advanced stuff of DP.
In case of any queries, please post your comments. Happy QTPing...!!