Thursday, June 3, 2010

QTP-ChildObjects-1

QTP, being based on Objects, usage of 'Child objects' is an effective feature in identifying a particular object type within a parent object.
Perform actions on the child objects without adding them to the Object Repository.

For instance, if the scenario is to click on all the links on a webpage, and if the link count is more than 100, it is not required to add all the link objects into the repository and click on them.
Instead 'ChildObject' can be used very effectively to retrieve the collection of all links within the page, iterate through the collection and click on each of them as required.

Noted advantages of Childobjects:
Retrieve any object collection
Work on multiple objects without adding them to repository
Suited mainly for UI checks
Suited in cases where the application is dynamic and object properties changes frequently
Minimize the dependency on QTP's object recognition
Lesser maintenance due to elimination of object repository


**************************************************************************

Below is a sample illustration of using childobjects for working with checkboxes in a web application.

**************************************************************************
'Code to count the number of check boxes on a web page

Function getChboxCnt()
'Create current page object
Set iObj = Browser(...).Page(...)
'Create Checkbox description object
Set oDesc = Description.Create()
oDesc("micclass").value = "WebCheckBox"
oDesc("checked").value = 1
Set CC = iObj.ChildObjects(oDesc)
ccc = CC.Count
End Function

******************************************************************

'Code to count the number of check boxes on a web page, within a webtable

Sub chbox_count()
Dim obj_ChkDesc
'Create Checkbox description object
Set obj_ChkDesc=Description.Create()
obj_ChkDesc("html tag").value = "INPUT"
obj_ChkDesc("type").value = "checkbox"

'Create current page object
Set WebPage = Browser(...).Page(...)
Dim allCheckboxes, singleCheckBox
Set allCheckboxes = WebPage.WebTable("name:=allbox","html tag:=TABLE").ChildObjects(obj_ChkDesc)

If allCheckboxes.Count > 0 Then
print "No of check boxes are : "&allCheckboxes.Count
End If
End Sub

******************************************************************


Like the post, encourage you to add your comment
** Happy Automating **

No comments: