Documente Academic
Documente Profesional
Documente Cultură
Chapter 5: ChildObjects
Overview
We looked at the ChildObjects method earlier in the Descriptive Programming chapter. This chapter
discusses ChildObjects in detail and highlights the key concepts that users should be aware of.
Let's look again at how we use ChildObjects.
Dim oDesc
Set oDesc = Description.Create
oDesc("micclass").value = "WebEdit"
oDesc("name").value = "txt_.*"
'Find all the childObjects
Set allTextBox = Browser("B").Page("P").ChildObjects(oDesc)
Dim i
For i = 0 to allTextBox.Count - 1
'Clear the textbox
allTextBox(i).Set ""
Next
One thing we need to understand is that allTextBox is a collection of objects which is mapped to the
runtime objects present at that moment in the application. If the state of the application changes, the
collection will become invalid. The objects derived from ChildObjects are directly mapped to its runtime
object and have no associated properties.
Consider an object collection of WebEdit objects that was created dynamically at runtime using the
following HTML:
<html>
<head>
<title>TOPROPERTIES OF A RUNTIME COLLECTION OBJECT</title></head>
<body>
<input name="txt1" type="text" value="txt1" /><br/>
<input name="txt2" type="text" value="txt2" /><br/>
<input name="txt3" type="text" value="txt3" /><br/>
For ix = 0 To colEdits.Count - 1
Print "Object_Index " & ix & "->" & colEdits(ix).GetROProperty("name")
Next
For ix = 0 To colEdits.Count - 1
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("name")
Next
For ix = 0 To colEdits.Count - 1
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("micclass")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("html
tag")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("value")
Print "Object_Index " & ix & "->" & colEdits(ix).
GetTOProperty("outerhtml")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("abs_x")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("abs_y")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("class")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("disabled")
Dim oDesc
Set oDescLink = Description.Create
oDescLink("micclass").value = "Link"
oDescLink("html tag").value = "A"
'Find all the childObjects
Set allLinks = Browser("B").Page("P").ChildObjects(oDescLink)
Dim i
For i = 0 to allLinks.Count - 1
'Clear the textbox
allLinks(i).Click
Browser("B").Back
Browser("B").sync
Next
When we run the above code, QTP will throw a general run error while it is still in the loop.
destroyed. Since the derived objects don't have any description of their own, they cannot function.
Consider the following example: tions
<div>
<div>
<span>test</span>
</div>
</div>
And the following ChildObjects code:
Dim oDesc
Set oDesc = Description.Create
oDesc("html tag").value = "DIV"
Set allObjects = Browser("Browser").Page("Page").ChildObjects(oDesc)
'Expected Count is 2 but we will get 0
MsgBox allObjects.Count
The above code will return the count as 0 because we have not specified the WebElement tag. This is
not For true for all tags as TD and TR tags will work without specifying micclass. To fix the issue, just
update the code as follow:
Dim oDesc
Set oDesc = description.Create
oDesc("html tag").value = "DIV"
oDesc("micclass").value = "WebElement"
Set allObjects = browser("Browser").Page("Page").ChildObjects(oDesc)
'Displays count 2 as expected
MsgBox allObjects.Count
Note
Using a wrong case for micclass and generic Web type (WebElement) will produce an
incorrect result. oDesc("micclass").value = "webElement" will also return 0. The right case is
"WebElement".
Function IsDerivedObject(Obj)
IsDerivedObject = False
If Obj.GetTOProperties().Count = 0 Then
'This is surely a derived object, otherwise we cannot have count as 0
IsDerivedObject = True
ElseIf Obj.GetTOProperties().Count = 1 Then
'Now if we only have a micclass property then let's assume
'that it is derived. Though that may not be true.
'Like in case of Browser("micclass:=Browser")
If Obj.GetTOProperties().Item(0).name = "micclass" then
IsDerivedObject = True
End if
End if
End Function