本文介绍了“可见?"和“存在?"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

从 Watir API 中,我得出了两个断言(当然可能不正确):

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

  • 如果我只想验证元素是否在 HTML 中,我可以使用 exists?.我不在乎它是否可见.
  • 如果我希望能够在页面上看到它,我可以使用 visible?.
  • I can use exists? if I simply want to verify that the element is in the HTML. I don't care if it's visible or not.
  • I can use visible?if I want to be able to see it on the page.

那么,我什么时候使用present??

So, when do I use present??

在我看来,我可以这样回答我自己的问题:

It seems to me that I could answer my own question by saying:

  • 如果我想在页面上看到它,我可以使用 present?,但我不希望它出现在 HTML 中.
  • I can use present? if I want to be able to see it on the page, but I don't want it to be in the HTML.

所以,如果我用记号笔在屏幕上写一些东西,它会出现吗??

So, if I write something on the screen using a marker pen, would that be present??

(对不起,如果我看起来不敬.)

(Sorry if I seem to be irreverent.)

那么 - 标题中提出问题的另一种方式 - 我什么时候应该使用 visible? 什么时候应该使用 present??

So - another way to ask the question in the title - when should I use visible? and when should I use present??

推荐答案

visible?present? 的区别在于该元素不存在于 HTML 中.

The difference between visible? and present? is when the element does not exist in the HTML.

当元素不在 HTML 中时(即 exists? 为 false):

When the element is not in the HTML (ie exists? is false):

  • visible? 会抛出异常.
  • present? 将返回 false.
  • visible? will throw an exception.
  • present? will return false.

我倾向于使用 present? 因为我只关心用户是否可以看到该元素.我不在乎它是否因为通过样式隐藏而无法看到,例如 display:none;,或者不在 DOM 中,例如它被删除了.如果应用程序实际上将不在 DOM 中的元素视为不同于在 DOM 中但不可见的含义,我只会使用 visible?.

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.

例如,给定页面:

<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>

查找不在页面上的 div 时可以看出区别:

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

这两种方法的比较以及 exists? 可以在 Watirways 书.

A comparison of these two methods along with exists? can be found in the Watirways book.

这篇关于“可见?"和“存在?"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:54