问题描述
我在 Rails 项目(Rails 5.2.0) 中使用 Capybara(capybara 3.1.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 首次发布时,visible 的值(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
需要变成
This means your find
would need to become
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 的 Capybara:如何只找到不可见的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!