我第一次尝试Capybara并且遇到麻烦找到我的表格。我正在使用Rspec,Capybara,Rails 3.2.8和Twitter Bootstrap。
在我的测试中,我有:
before do
choose "Residential"
choose "Storage"
fill_in "Customer Location", with: 30082
fill_in "datepicker", with: 2012-12-02
fill_in "Email", with: "jesse@test.com"
fill_in "Phone", with: "555-555-5555"
end
得到了错误:
Failure/Error: choose "Residential"
Capybara::ElementNotFound:
cannot choose field, no radio button with id, name, or label 'Residential' found
在我的表格中,我有(摘录):
<%= quote.radio_button :customer_type, "Residential"%>
<%= quote.label "Residential", class:"radio inline" %>
当我启动服务器时,我可以看到表单,Firebug显示我有一个名为“Residential”的标签与表单元素相关联。
在搜索问题并找到这篇文章之后:Capybara not finding form elements(等等),我无法弄清楚它为什么不起作用。
答案 0 :(得分:1)
我最终回到了Capybara的Rubydocs(http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions)并找到了“选择”方法的部分。
我没有深入研究文档,但我看到它正在寻找“radio_button”中的“定位器”,因此尝试直接关联它。
我因此将表单更改为(注意我在表单元素中添加了“id”):
<%= quote.radio_button :customer_type, "Residential", id:"Residential"%>
<%= quote.label "Residential", class:"radio inline" %>
并且测试的那部分通过了!我重复了每个表单元素,所有内容都按预期执行。
希望这对其他人有用。