Sunteți pe pagina 1din 8

Chapter 5

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/>

<input name="txt4" type="text" value="txt4" /><br/>


<input name="txt5" type="text" value="txt5" /><br/>
</body>
</html>

Figure 5.1: Demo web page


The following code can be used to create the collection:

Dim desc, parent, colEdits


Set desc = Description.Create
desc("micclass").Value = "WebEdit"
Set parent = Browser("name:=TOProperties.*").Page("micclass:=Page")
Set colEdits = parent.ChildObjects(desc)
Visually speaking, we know that the colEdits collection must contain five objects of type (micclass)
WebEdit. A loop can be used to retrieve properties of each object using GetROProperty. However, what
will the code return if GetTOProperty were to be used instead? Below is a demonstration of values
retrieved using GetROProperty vs GetTOProperty:

For ix = 0 To colEdits.Count - 1
Print "Object_Index " & ix & "->" & colEdits(ix).GetROProperty("name")
Next

Figure 5.2: Print results


Considering the output above, a user might expect the same values when using GetTOProperty.
However, this is not the case. Consider the same loop using GetTOProperty:

For ix = 0 To colEdits.Count - 1
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("name")
Next

Figure 5.3: Print results


Notice that the value of each property returned by the Print statement is Null.
The reason behind this behavior is quite simple. The property we have tried to retrieve is a property of a
runtime object, not a design-time object. An object returned using the ChildObjects method
(DispTOCollection) is always a runtime collection, thus any object present in the collection will only have
runtime attributes. This holds true for all properties except micclass. The code below demonstrates that
no other property for the target object will be retrieved except for the object's micclass:

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")

Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("name")


Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("readonly")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("type")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("x")
Print "Object_Index " & ix & "->" & colEdits(ix).GetTOProperty("y")
Print vbNewLine
Next
Below is the output from the Print statement for the first object in the collection:

Figure 5.4: Print results Tele


In summary, for any runtime ChildObjects collection, GetTOProperty will only return the associated
value for the micclass property. For all other properties, GetROProperty must be used.
If we needed to check for all links on a webpage, the code we would use would be as follows:

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.

Figure 5.5: General run error for ChildObjects loop


This happens because when we click the link and return Telecommuni to the previous page, the existing
collection is no longer valid. Using an invalid runtime object reference causes the general run error. A
workaround to the problem is to regenerate the collection every time inside the loop:

'Find all the childObjects


Set allLinks = Browser("B").Page("P").ChildObjects(oDescLink)
Dim i
For i = 0 to allLinks.Count - 1
Set allLinks = Browser("B").Page("P").ChildObjects(oDescLink)
'Clear the textbox
allLinks(i).Click
Browser("B").Back
Browser("B").sync
Next
This approach will work but has a few constraints. When we click on a link and return to the previous
page, the page remains the same in terms of the number of links and their position, but this approach
may not always work. If we have a link on the main page which changes the language of the application
by setting a cookie, clicking the link and using the back button will cause the main page to have changed
completely. Now regenerating the collection inside the loop can create unexpected results.
Objects derived from the ChildObjects method differ from the usual ChildObjects in many ways. We will
discuss the difference one by one

Using Init or RefreshObject on Derived Objects


When we use Init or RefreshObject on an object derived from ChildObjects, it basically destroys the
object. As we mentioned earlier, objects derived from the Press ChildObjects method have a directmapping to the runtime object. When we use RefreshObject on such an object, that mapping is

destroyed. Since the derived objects don't have any description of their own, they cannot function.
Consider the following example: tions

Set oDescSearch = Description.Create


oDescSearch("micclass").value = "WebEdit"
oDescSearch("name").value = "q"
Set oSearchText = Browser("B").Page("P").ChildObjects(oDescSearch).item(0)
oSearchText.RefreshObject
oSearchText.Set UCase(Text)
Running the above code raises the following Post error:

Figure 5.6: Object Not found error for referenced object


For this reason, we should make sure that we never call RefreshObject on an object that is derived from
ChildObjects.

Derived Objects don't Support RegisterUserFunc


Until QTP 11 there was a limitation that impeded using registered methods on derived objects. Consider
the following code sample:

Function NewSetMethod(obj, Text)


obj.Set UCase(Text)
End Function
RegisterUserFunc "WebEdit", "NewSet", "NewSetMethod"
Set oDescSearch = Description.Create
oDescSearch("micclass").value = "WebEdit"
oDescSearch("name").value = "q"
Set oSearchEdit = Browser("B").Page("P").ChildObjects(oDescSearch).item(0)
oSearchEdit.NewSet "tarun lalwani"
The above code will throw an error message saying the object doesn't support the property or method

Derived Objects don't Support Objects in Further Hierarchy


Until QTP 11 there was a limitation C that impeded using further object hierarchy on derived objects.
Consider the following inese code sample:

Set oDescSearch = Description.Create


oDescSearch("micclass").value = "WebTable"
Set oSearchTable = Browser("B").Page("P").ChildObjects(oDescSearch).item(0)
oSearchTable.WebEdit("name:=q").Set "tarun lalwani"
The above code will similarly throw an error message saying that the object doesn't support the
property or method.
Note
The workaround to fix this issue is to create a new description and use ChildObjects again
on the derived objects.

ChildObject Returns Zero Objects for WebElements


Consider the HTML source below:

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

Detecting If an Object has been Derived Mmunicatio using ChildObjects


There is no direct method available for one to detect Teleco if an object has been derived from using
ChildObjects, but we can use our understanding of how the derived objects differ from normal objects.
The method, however, is not and foolproof.

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

S-ar putea să vă placă și