使用Capybara,我需要断言表单元素不存在,例如,"那么我不应该看到"Username"文本字段".如果找不到元素,find会抛出一个异常,这是我想到的最好的方法.有更好的办法吗?

Then /^I should not see the "([^\"]+)" ([^\s]+) field$/ do |name, type|
  begin
    # Capybara throws an exception if the element is not found
    find(:xpath, "//input[@type='#{type}' and @name='#{name}']")
    # We get here if we find it, so we want this step to fail
    false
  rescue Capybara::ElementNotFound
    # Return true if there was an element not found exception
    true
  end 
end

我是水豚的新手,所以我可能错过了一些明显的东西.

推荐答案

你可以利用水豚来做到这一点吗?方法与rspecs魔法匹配器相结合.然后你可以这样使用它:

 page.should have_no_selector(:xpath, "//input[@type='#{type}' and @name='#{name}']")

您可以在"capybara文档"第here页的"查询"一节中看到可以执行的断言的更多细节

Ruby相关问答推荐

R10K模块在try 安装gem r10k时获得错误的Ruby版本

Ruby错误-应为数组或字符串,已获取哈希

在 Ruby 中,如何编写一个可以与 &:symbol 一起使用的简单方法?

Ruby |= 赋值运算符

用于 ruby​​ gems 的新 10.9 OSX 的命令行工具?

在 gem 中放置/访问配置文件的位置?

RSpec 是否有可能期望在两个表中发生变化?

rbenv 没有显示可用的 ruby​​ 版本

模块中的实例变量?

jekyll 调试或打印所有变量

如何在 linux (ubuntu) 上更新 ruby​​?

如何在 Ruby 中使用 (n) 诅咒?

格式化 Ruby 的漂亮打印

如何将值保存到 YAML 文件中?

在Ruby中实现平等的正确方法是什么

我应该如何在哈希上使用 each_with_object?

rbenv、rvm 和 chruby 有什么区别?

判断是否在 SASS 中定义了变量

如何重命名 gemset?

Ruby:从 Ruby 中的变量创建哈希键和值