1

我第一次尝试 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 find form elements(以及其他)之后,我无法弄清楚为什么它不起作用。

4

1 回答 1

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" %>

并且那部分测试通过了!我重复了我的每个表单元素,并且一切都按预期执行。

希望这对其他人有用。

于 2012-10-31T19:14:14.150 回答