What's the difference between `visible?` and `present?`?

前端 未结 1 1330
广开言路
广开言路 2021-01-21 02:30

From the Watir API, I\'ve derived two assertions (which of course might not be correct):

  • I can use exists? if I simply want to verify that the element
相关标签:
1条回答
  • 2021-01-21 02:53

    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.

    0 讨论(0)
提交回复
热议问题