QA automation with QTP/UFT – function for counting the number of objects within an object

Hi. The function I am about to show you, counts the number of objects within an object. I'll show you what I mean...

Example:

Access to anyone on our email list
Test1

Code:


 Public Function fun_countObjectsWithinObjext(pstrObjectName, pstrObjInnertext, pintIndex)
    Set objectDescription = Description.Create
    objectDescription("micclass").value = pstrObjectName
    Dim counter
    Set counter = Browser("index:=0").Page("title:=.*").WebElement("innertext:="& pstrObjInnertext, "Index:="& pintIndex).ChildObjects(objectDescription)
    MsgBox counter.count    
End Function


1. Let's see how many web elements are within the yellow box web element above. Click on the object spy and highlight the whole yellow box.

2. Copy the innertext of this web element which should be "Access to anyone on our email listTest1"

3. Now call the function countObjectsWithinObjext. Since we are trying to find how many web elements are within the yellow web element box, your first parameter should be "WebElement"

fun_countObjectsWithinObjext "WebElement"

4. Enter the inntertext from step two as our second parameter

fun_countObjectsWithinObjext "WebElement", "Access to anyone on our email listTest1"

5. Enter an index of 0 as the third parameter:

fun_countObjectsWithinObjext "WebElement", "Access to anyone on our email listTest1", 0

Now run the code and you should get a message with the value 2. There are two web elements withing the yellow web element box.

1. Access to anyone on our email list(WebElement)

2. Test1(WebElement)

Cool right? Post a comment below and let me know if this was helpful!

Pin It on Pinterest

Clef two-factor authentication