Description Programming - Enumerating ChildObjects.
In this tutorial, We will learn the basics of ChildObjects i.e. to count the number of objects available in a webpage.We will do following exercises:
- 1. Count the number of text boxes available in the webpage
- 2. Enter data in all the textboxes in a webpage
- 3. Count the number of check boxes available in the webpage
- 4. Check All Checkboxes in the webpage
- 5. Count the number of buttons available in the webpage
- 6. Count the number of Links available in the webpage
ChildObjects method is used to retrieve all objects located inside a specified parent object. In our current example, parent object is a webpage. To reterive the childobject, we must create a description object.Check the below example Count the number of text boxes available in the webpage by QTP:
[Note – For practice, you can use webpage - http://www.onestopsoftwaretesting.com/qtptest.html. You can copy and pate the below code in QTP, It will work flawlessly]
SystemUtil.Run "iexplore.exe", “http://www.onestopsoftwaretesting.com/qtptest.html”
' - - - - - - - - - - Count the number of text boxes available in the webpage - - - - - - - - - -
'Create a Description object
Set dpAllTxt = Description.Create
'Define the class of the above created Description object. 'In this example, we are counting the number of text boxes, so we are defining "WebEdit"
dpAllTxt("micclass").value = "Webedit"
'Getting all the textboxes
Set Alltxt = Browser("Browser").Page("Page").childobjects(dpAllTxt)
'count all the textboxes
txtcount = Alltxt.Count
msgbox txtcount & " text boxes are available in the webpage"
'Enter data in all textboxes in the webpage
icounter = Alltxt.Count -1
For i = 0 to icounter
Set otext = Alltxt.item(i) ' item property - Exposes a specified item from a collection.
otext.set "This is text box #" & (i+1)
Next
' - - - - - - - - - - - - - - - - End - - - - - - - - - - - - - - - -
More examples in upcoming posts..
More Examples -
Count the number of check boxes available in the webpage by QTP – Childobjects method
References -
- QTP Help
- QTP Unplugged book – by Tarun Lalwani