Pages

Sunday, March 27

Descriptive Programming in QTP - Part 3

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...!!

20 comments:

  1. Jain Sahib, i really don't have any idea about QTP but there is a small observation:
    in the very first example, you used oDesc in the second line but you didn't assign any property/value to this object. So, how it will work?

    ReplyDelete
  2. @Ankur

    see these lines carefully..

    Set oDesc=Description.Create()
    Set oAll=Window("text:=Flight Reservation").ChildObjects(oDesc)

    oDesc is just a blank object, which is passed to oAll. oAll will have all the objects.

    ReplyDelete
  3. hi ankur how to click one button among two or more buttons having same name

    ReplyDelete
  4. Hi Abhikansh first i would like to thank u for ur posts, its very help full and clear, could u explain me the following statement how it works, GetROproperty is used to get the property of the object but why we are using it for the second time.

    Print "Class: "&oAll(i).GetROproperty("micclass")&", Text: "&oAll(i).GetROproperty("text")

    and why we are giving "k=i+2" in the first example in the for statement.

    ReplyDelete
  5. @kamalesh

    thanks for your feedback.

    GetROproperty is used two times for two different properties..

    GetROproperty("micclass")
    GetROproperty("text")

    ReplyDelete
  6. great article...recently joined a QTP project and this article is proving very instructive. Thanks a lot.

    ReplyDelete
  7. Hi Abhishek,
    Thanks for posting this good article.My question is,suppose we have 6 Editbox with the same property on a page. I want to enter the data into the 4th box.I have some idea that index will be used in that case but how to write the code with that index. Could you please explain?

    ReplyDelete
    Replies
    1. Shekhar,
      below code may help you

      Set oDesc=Description.Create()
      oDesc("micclass").value="WebEdit"

      Set oAll=Browser("").Page("").Frame("").ChildObjects(oDesc)
      cnt=oAll.count
      For i=0 to cnt-1
      If oAll(i).Getroproperty("enabled") AND i =3 Then
      oAll(i).Set "your Data"
      End If
      Next

      Delete
  8. Akhilesh..may I know why what is purpose of using
    GetROproperty("micclass")

    ReplyDelete
  9. Great post... Clears my doubts about child object method :) Thank u.

    ReplyDelete
  10. Hi Abhi is it mandatory know the scripting for learning QTP. Because past 2+ years i am working as manual tester,so i am new to the tool. If VBS is mandatory which are the main key areas i have to concentrate. Please list out the topics.

    @Bangalore

    ReplyDelete
  11. Can you Please share a complete example illustrating how to automate a page,,say irctc just show how to login ,search a train and click on book.Please show how to use wait statement also in this in this example
    Also could you please explain what are the associated library functions in QTP and how to use it.

    Thanks in advance !

    ReplyDelete
  12. This section is little complex having problem to understand. You have done it with Dynamic approach.Can we do it using DP. Please do reply..

    Thanks

    ReplyDelete
  13. @Deepak - yes we can. Please refer part-1 and part-2 of this post. Let me know if you still have any issue.

    ReplyDelete
  14. @ sharfu - It will not be possible to post whole code here. I suggest you to start and let me know the problems you face. I tried my best to explain how to start.

    ReplyDelete
    Replies
    1. How to create function for entering username and password....
      and function for Submit button..

      Delete
    2. You should use data table or excel to take values of username and password. for submit button you can write it as separate or you can combine both in log in function.

      Delete