In some cases, a TO may appear repeatedly on a window or page. A common example is images that are used as buttons in web applications. If a function is called to perform an action on such a TO, we may need to ensure that the correct object was passed. A powerful technique to achieve this is already outlined in the Identifying elements based on anchors recipe in Chapter 2, Testing Web Pages. In this recipe, we will see how to do it according to the object's parent (such as a Frame
or WebTable
, but can also be any WebElement
), which is the container that holds our target TO.
Suppose that we have a reference to an OR TO and need to perform an operation or checkpoint using it and that we know the description of its container object. We can then write the following function that will indicate whether the target TO is the one we were looking for, based on its parent:
Function IsObjectByParent(ByVal obj, ByVal oDicProps) Dim bParent, oParent, prop Set oParent = obj.GetTOProperty("parent") bParent = true For each prop in oDicProps.keys If oDicProps(prop) <> oParent.GetTOProperty(prop) Then bParent = false Exit for End If Next IsObjectByParent=bParent End Function
Our function accepts two arguments, the target TO and a Dictionary
object, with key-value pairs representing the properties and their respective expected values for the parent object. Using the GetTOProperty
method, we dynamically retrieve the actual parent object of our referenced TO. Finally, we loop through the Dictionary
keys and compare the expected value associated with each key to the corresponding TO property. We set the bParent
flag variable as true
(optimistic initialization), and in case any of its properties do not match the expected set, then it will fail and exit the For
loop. The function returns true
only if all parent properties match the expected set.