问题描述
我正在使用Capybara(capybara 3.1.0)在Rails项目(Rails 5.2.0)中进行系统测试.
I am using Capybara(capybara 3.1.0) for system tests in Rails project(Rails 5.2.0).
确保元素不可见的方法是什么.
What is the way to ensure element is not visible.
我一直使用visible: false
选项,直到我发现它也与可见元素匹配.
I was using the visible: false
option until I just found it matches visible elements, too.
例如,我使用:
find("h1", visible: false).text
也不例外,h1的文本会打印在控制台中,而h1绝对可见.
There is no exception, and the text of the h1 is printed in the console, while h1 is definitely visible.
这是预期的吗?这种行为背后的逻辑是什么?确保元素不可见的正确方法是什么?
Is this expected? What is the logic behind this behavior? And what is the correct way to ensure element is not visible?
推荐答案
首次发布Capybara时,可见的值(true或false)用于启用或禁用可见性过滤器,但由于遗留测试原因而一直存在.您还可以指定:visible,:hidden,:all的值( https://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Finders#find-instance_method ),其中:visible
的行为与true
相同,:all
的行为与false
相同,并且:hidden
将仅返回不可见的元素.
When Capybara was first released, the value of visible (true or false) meant to enable or disable the visibility filter, for legacy test reasons that has stayed. You can also specify values of :visible, :hidden, :all (https://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Finders#find-instance_method) where :visible
behaves the same as true
, :all
behaves the same as false
and :hidden
will return only non-visible elements.
这意味着您的find
需要成为
find("h1", visible: :hidden).text(:all)
如果需要文本,则需要:all
参数,因为text
默认为仅可见文本( https://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Element#text-instance_method )
With the :all
parameter needed if you want the text because text
defaults to only visible text (https://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Element#text-instance_method)
这篇关于水豚与Rails:如何查找唯一不可见的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!