在编写集成测试时无法使用 Capybara 和 Rspec 找到 link
Cannot find link using Capybara and Rspec while writing an integration test
我正在尝试通过 CSS 类 和 ID 查找 link,但总是出现错误:Capybara::ElementNotFound: Unable to find css ...
实际的代码是:
find('#bucket_resources_containers > #user_base_widget.widget >
div.widget_header > div.right.may-
edit.control.button.add.icon.add_options > a.tasksy.options').click
页面来源为:enter image description here
您在评论中给了我们答案,该元素不可见。
简答:find_link(selector, visible: :all).click
如水豚在 documentation 中所示:
By default Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements.
只定位可见元素是水豚的一个巧妙设计,它避免了认为用户能够找到该元素。
find_link
method documentation 在查找隐藏链接时没有多大帮助,因为它只显示这些选项:wait, href, id, title, alt, class
:
#find_link([locator], options = {}) ⇒ Capybara::Node::Element
但是你看到 finding documentation 上有一个 visible
选项:
find_link('Hello', :visible => :all).visible?
find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible?
此选项 visible
来自 #all
方法,where you can see here。它可以有这些值:
true - only finds visible elements.
false - finds invisible and visible elements.
:all - same as false; finds visible and invisible elements.
:hidden - only finds invisible elements.
:visible - same as true; only finds visible elements.
因此,在您的情况下,如果您确实想要隐藏它,则可以使用 visible: false
,或者如果您不关心可见性,则可以使用 visible: :all
。
我正在尝试通过 CSS 类 和 ID 查找 link,但总是出现错误:Capybara::ElementNotFound: Unable to find css ...
实际的代码是:
find('#bucket_resources_containers > #user_base_widget.widget >
div.widget_header > div.right.may-
edit.control.button.add.icon.add_options > a.tasksy.options').click
页面来源为:enter image description here
您在评论中给了我们答案,该元素不可见。
简答:find_link(selector, visible: :all).click
如水豚在 documentation 中所示:
By default Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements.
只定位可见元素是水豚的一个巧妙设计,它避免了认为用户能够找到该元素。
find_link
method documentation 在查找隐藏链接时没有多大帮助,因为它只显示这些选项:wait, href, id, title, alt, class
:
#find_link([locator], options = {}) ⇒ Capybara::Node::Element
但是你看到 finding documentation 上有一个 visible
选项:
find_link('Hello', :visible => :all).visible?
find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible?
此选项 visible
来自 #all
方法,where you can see here。它可以有这些值:
true - only finds visible elements.
false - finds invisible and visible elements.
:all - same as false; finds visible and invisible elements.
:hidden - only finds invisible elements.
:visible - same as true; only finds visible elements.
因此,在您的情况下,如果您确实想要隐藏它,则可以使用 visible: false
,或者如果您不关心可见性,则可以使用 visible: :all
。