capybara没有传递选择参数

时间:2016-07-25 23:03:42

标签: ruby-on-rails capybara

我正在使用Capybara在Rails应用程序中测试表单。

表单有2个选择框,每个框都有默认选项。在开发和生产中提交表单时,这些选项作为参数传递,但由于某些原因,Capybara未在测试中提交它们。 Capybara正在查找选择框和选项OK,因为如果我输入不存在的选项会抛出错误。但是Capybara在提交表单时不会将默认选项或选定选项作为参数传递。

表单摘要如下:

<%= form_for(@reservation, :url => account_reservations_path(account.id), remote: false, :html=>{:id=>'dates_form'}) do |f| %>
       <tr>
    <td style:"text-align:center" colspan="2"><%= f.submit 'Submit Dates, Source of Booking & Room Preference', class: "btn btn-primary btn-sm" %></td>
    </tr>
      <td>
        <%= f.text_field :check_in_date, id: check_in_date_id,  placeholder: "Check In Date" %></td>
      <td><%= f.text_field :check_out_date, id: check_out_date_id, placeholder: "Check Out Date" %></td>
    </tr>
    <tr>
    <td colspan="1"><%= f.select(:source_of_booking, Reservation::SOURCE_OF_BOOKING, {}, {:style => "width:150px;", default: Reservation::SOURCE_OF_BOOKING[0]})  %></td>
      <td>
        <%= f.select(:bed_preference, @bed_options, {}, {:style => "width:100px;"}) %>
      </td>
    </tr>
   <% end %>

rails服务器提交表单的处理如下:

Processing by ReservationsController#create as HTML

参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“7Q3jakPp91bleWe1qZQFGRvYTXIxj9AdIYbeVtrF3bg =”,“commit”=&gt;“提交日期,预订来源和房间偏好”,“预订”= &gt; {“check_in_date”=&gt;“2016年7月29日星期五”,“check_out_date”=&gt;“2016年8月1日星期一”,“source_of_booking”=&gt;“直接发送电子邮件”,“bed_preference”=&gt; “女王”},“account_id”=&gt;“5015”}

但是在Capybara中,选项选项中的参数缺失,即使它们具有默认值

Started POST "/accounts/625262370/reservations" for 127.0.0.1 at 2016-07-25 18:23:59 -0500

ReservationsController处理#create as HTML   参数:{“utf8”=&gt;“✓”,“预订”=&gt; {“check_in_date”=&gt;“2016-07-25”,“check_out_date”=&gt;“2016-07-28”},“提交“=&gt;”提交日期,预订来源和房间偏好“,”account_id“=&gt;”625262370“}

测试代码抛出没有错误,直到最终期望页面有文本确认,因此Capybara找到了选择框但只是没有处理它。

it "should add a new reservation for room category", :focus => true do
  fill_in('reservation[check_in_date]', :with => Date.today.to_s)
  fill_in('reservation[check_out_date]', :with => (Date.today + 3).to_s)
  select 'Twin', :from => 'reservation[bed_preference]'
  click_button("Submit Dates, Source of Booking & Room Preference")
  expect(page).to have_text("CONFIRMED")
end

1 个答案:

答案 0 :(得分:0)

根据您在测试期间使用的驱动程序和/或浏览器,无效的html可以/将以不同的方式进行解释。您拥有的表单代码段将生成一个&lt; form&gt;元素与&lt; tr&gt;作为子元素的元素(您似乎也缺少围绕日期元素的起始tr元素)。 &LT;形式&GT;元素不是&lt; tr&gt;的有效子元素。元素和&lt; tr&gt;元素仅作为&lt; table&gt;,&lt; thead&gt;,&lt; tbody&gt;和&lt; tfoot&gt;的子元素有效。元素,所以要有效你的代码片段必须是

<%= form_for(@reservation, :url => account_reservations_path(account.id), remote: false, :html=>{:id=>'dates_form'}) do |f| %>
  <table>       
    <tr>
      <td style:"text-align:center" colspan="2"><%= f.submit 'Submit Dates, Source of Booking & Room Preference', class: "btn btn-primary btn-sm" %></td>
    </tr>
    <tr>
      <td><%= f.text_field :check_in_date, id: check_in_date_id,  placeholder: "Check In Date" %></td>
      <td><%= f.text_field :check_out_date, id: check_out_date_id, placeholder: "Check Out Date" %></td>
    </tr>
    <tr>
      <td colspan="1"><%= f.select(:source_of_booking, Reservation::SOURCE_OF_BOOKING, {}, {:style => "width:150px;", default: Reservation::SOURCE_OF_BOOKING[0]})  %></td>
      <td>
        <%= f.select(:bed_preference, @bed_options, {}, {:style => "width:100px;"}) %></td>
    </tr>
   </table>
 <% end %>

如果没有有效的HTML,某些字段可能实际上被认为是在表单之外,因此不会随之提交。我猜你在开发和生产系统中使用Chrome,因为它对无效表的解释往往更宽松,如果你在开发和生产系统上尝试使用firefox,你会发现它不起作用对于无效表,它往往不那么宽松。