Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

Search Web Object – Simplified

by Anshoo Arora on August 8, 2009

It can be quite confusing at times when an object exists on a page, but it is hidden or multiple instances of an object exists with some instances hidden. The IsObjectFound function implements the search function for the specified web object and returns ‘True’ if a single instance of the object is found.

'Arg:
'    TestObject
'
'Returns:
'    -1  Object not found
'    0  Multiple instances found
'    1  Single unique instance found

Public Function IsObjectFound(ByVal TestObject)
     'If .Exist returns True, single instance found
    If TestObject.Exist(0) Then
        IsObjectFound = 1
        Exit Function
    End If
 
    Dim TOProperties, i, oDesc, Parent
 
    Set TOProperties = TestObject.GetTOProperties
    Set oDesc = Description.Create
 
    'Enumerate supplied description
    'Add properties to the Description object
    For i = 0 To TOProperties.Count - 1
        oDesc.Add TOProperties(i).Name, TOProperties(i).Value
    Next
 
    Set Parent = TestObject.GetTOProperty("parent")
 
    If Parent.ChildObjects(oDesc).Count > 0 Then
        IsObjectFound = 0
    Else
        IsObjectFound = -1
    End If
End Function

IsObjectFound can return 3 possible values, depending upon the object’s existence:

-1 = Object not found
0 = Multiple instances found
1 = Single unique instance found

The above function can be tested on www.Google.com:

MsgBox IsObjectFound(Browser("title:=Google").Page("micclass:=Page").WebEdit("name:=q"))
 
'If used with RegisterUserFunc
RegisterUserFunc "WebEdit", "IsObjectFound", "IsObjectFound"
MsgBox Browser("title:=Google").Page("micclass:=Page").WebEdit("name:=q").IsObjectFound()

There are instances in Web apps when .Exist returns true but the object is not visible to the human eye. To find out if that particular object is visible, use the following code statement to check whether “x” (or “y”) is greater than zero:

If Object.GetROProperty("x") > 0 Then
    'Object is visible
End If

Hope you guys find this article useful :)

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

{ 8 comments… read them below or add one }

1 sunitha September 16, 2009 at 5:45 am

Hi Anshoo,

Good observation

Reply

2 Anshoo Arora September 16, 2009 at 5:46 pm

Thanks! :)

Reply

3 raja February 23, 2010 at 3:53 am

Hi Anshoo,

When i have executed this statement Msgbox IsObjectFound( “WebEdit”, “name:=q” ), runtime error displayed as Type mismatch ‘IsObjectFound’.
Please clarify this.

4 deep June 15, 2010 at 3:28 am

the link “Download IsObjectFound Class.” is refering to http://relevantcodes.com/search-web-object-simplified/#.
I hope the class file is missing.

Reply

5 Anshoo Arora June 22, 2010 at 10:20 am

Hi Deep,

Please use this link: http://relevantcodes.com/Articles/IsObjectFoundClass/IsObjectFoundClass.txt

Thanks for bringing this to my attention. I will update this article tonight.

6 Oxana August 16, 2010 at 12:56 pm

Hi Anshoo

I have a question for you. I am currently scripting website. I have an issue with number of webedits on the page. QTP shows that there is 15 web edit fields. I could see only 7. I have already talked to the developer who created this page and he confirmed that there was no hidden edit fields. Did you ever experience such problem? If “yes”, please let me know why there is a huge difference between the results.

thanks in advance.

Reply

7 Anshoo Arora August 22, 2010 at 4:40 pm

There may be hidden WebEdits in which case QTP will give the correct count but a user won’t see any of the hidden objects – they will only see the ones made visible by the developer. This can help you retrieve the number of hidden objects:

Set oDesc = Description.Create
oDesc("micclass").Value = "WebEdit"
oDesc("x").Value = 0

Set oBase = Browser("").Page("").ChildObjects(oDesc)

MsgBox "Number of hidden objects: " + oBase.Count

8 Anshoo Arora February 24, 2010 at 8:40 am

Hi Raja,

I think this would generally appear if the library has not been associated with the test..

Reply

Leave a Comment

Next post: