From the Watir API, I\'ve derived two assertions (which of course might not be correct):
exists?
if I simply want to verify that the element
The difference between visible?
and present?
is when the element does not exist in the HTML.
When the element is not in the HTML (ie exists?
is false):
visible?
will throw an exception.present?
will return false.I tend to stick with present?
since I only care if a user can see the element. I do not care if it cannot be seen due to it being hidden via style, eg display:none;
, or not being in the DOM, eg it got deleted. I would only use visible?
if the application actually treats the element not being in the DOM as a different meaning than being in the DOM but not visible.
For example, given the page:
<html>
<body>
<div id="1" style="display:block;">This text is displayed</div>
<div id="2" style="display:none;">This text is not displayed</div>
</body>
</html>
You can see the difference when looking for a div that is not on the page:
browser.div(:id => '3').visible?
#=> Watir::Exception::UnknownObjectException
browser.div(:id => '3').present?
#=> false
For elements on the page, the two methods will be the same:
browser.div(:id => '1').visible?
#=> true
browser.div(:id => '1').present?
#=> true
browser.div(:id => '2').visible?
#=> false
browser.div(:id => '2').present?
#=> false
A comparison of these two methods along with exists?
can be found in the Watirways book.