Exist Returns True Even When The Object Does Not Exists
Exist Returns True Even When The Object
Does Not Exists
December 21, 2008 7:27 AM
Checking Exist property for a Test object
might return True even when the object is not present on the screen. Though
this is not an issue with QTP but there could be 2 possible reasons behind
this. This article walks through both the reasons in details
Root Cause #1 – Smart identification
being used to identify the object
My article “Is
Smart Identification really smart?” explains this issue in great
details.
Root Cause #2 – Element is hidden using
Cascading style. sheets (CSS)
This is what we will look into in detail.
There are 3 different ways of hiding a element on a web page
- Method 1 – Element hidden using display
In this the element is hidden using the CSS
display property. Below HTML code and preview show a demo of the same
<label
id="hiddenMessageDisplay" style="color:blue">This
message is hidden using CSS display property</label> <input
nclick="if(this.value=='Show message'){this.value='Hide
message';document.getElementById('hiddenMessageDisplay').style.display=''}else{this.value='Show
message';document.getElementById('hiddenMessageDisplay').style.display='none'}"
name="btnhiddenMessageDisplay" type="button"
value="Hide message" /> |
HTML preview
This message is hidden using CSS display
property
Since the object exist in the HTML source
code QTP Exist method will return true even if the element is not being
displayed. To check the visibility of the element we can use code 3 different
approaches
Set Elm =
Browser("B").Page("P").WebElement("This message is
hidden using CSS display property") If
oElm.object.currentStyle.display="none" Then
Msgbox "Object is hidden" Else
Msgbox "Object is visible" End if |
- The above code only compatible with Internet explorer as QTP
supports the object property only for IE. Though there are 2 workarounds
to test the same in Firefox.
- 2nd Approach
This approach uses the fact that x,y, width
and height of a element hidden using display:none is zero.
Set Elm =
Browser("B").Page("P").WebElement("This message is
hidden using CSS display property") Dim x,y, width, height x = oElm.GetROProperty("x") y = oElm.GetROProperty("y") width =
oElm.GetROProperty("width") height =
oElm.GetROProperty("height") If x=0 And y=0 And width=0 And height=0
Then
Msgbox "Object is hidden" Else
Msgbox "Object is visible" End if |
This approach gets the style. property of
element and looks for display:none in the style.
Set Elm =
Browser("B").Page("P").WebElement("This message is
hidden using CSS display property") If
InStr(oElm.GetROProperty("attribute/style"),"display:none")
Then
Msgbox "Object is hidden" Else
Msgbox "Object is visible" End if |
The best way to check for CSS related is
property is to use IE browser and use the .object property to access
currentStyle. object.
- Method 2 – Element hidden using visibility
In this the element is hidden using the
visibility attribute of CSS. The below HTML code and preview shows the same
<label
id="hiddenMessageVisibility" style="color:red;">This
message is hidden using CSS visibility property</label> <input
nclick="if(this.value=='Show message'){this.value='Hide
message';document.getElementById('hiddenMessageVisibility').style.visibility=''}else{this.value='Show
message';document.getElementById('hiddenMessageVisibility').style.visibility='hidden'}"
name="btnhiddenMessageVisibility" type="button"
value="Hide message" /> |
HTML preview
This message is hidden using CSS visibility
property
The difference in hiding a object using
display and visibility is that when a object is hidden using visibility the
object will have the actual position and occupy space on the page. While
display:none will totally hide the element. Playing with the 2 HTML previews
will make thing clear. This can be tested using 1st and 3rdapproach
discussed in Method
1
- Method 3 – Element hidden using class
In this method two different stylesheet
class are created, one for hidden and one for visible elements. When the
visibility needs to be changed for a element the class is changed. Element are
hidden using Method1 or Method2 only in CSS class.
<label id="messageClass"
class="messageVisibile">This message is hidden using class
property</label> <input
nclick="if(this.value=='Show message'){this.value='Hide
message';document.getElementById('messageClass').className='messageVisible'}else{this.value='Show
message';document.getElementById('messageClass').className='messageHidden'}"
name="btnmessageClass" type="button" value="Hide
message" /> |
HTML preview
This message is hidden using class property
This can be tested using Approach
1 discussed in Method
1. We can also check for the class value and see it is the one of the
hidden one’s or not.
Set Elm =
Browser("B").Page("P").WebElement("This message is
hidden using CSS display property") If oElm.GetROProperty("class")
= "messageHidden" Then
Msgbox "Object is hidden" Else
Msgbox "Object is visible" End if |
This approach is very much implementation
specific and should be avoided if possible.
TAG: