Capybara无法找到场地

时间:2017-03-16 21:19:50

标签: ruby-on-rails rspec capybara ruby-on-rails-5

您好我正在进行第一次测试,我需要您的帮助。我正在使用Rails5。

问题是因为我相信嵌套的字段......(使用Cocoon gem)

Capybara返回此错误:

Capybara::ElementNotFound: Unable to find field "event_participants_attributes_1489697584487_first_name"

我的HTML看起来像这样:

<div class="nested-fields">
  <div class="form-group string optional event_participants_first_name"><label class="control-label string optional" for="event_participants_attributes_1489697584487_first_name">Participant's first name</label><input class="form-control string optional" type="text" name="event[participants_attributes][1489697584487][first_name]" id="event_participants_attributes_1489697584487_first_name" data-com.agilebits.onepassword.user-edited="yes"></div>
  <div class="form-group integer optional event_participants_salary"><label class="control-label integer optional" for="event_participants_attributes_1489697584487_salary">Participant's monthly pay</label><input class="form-control numeric integer optional" type="number" step="1" name="event[participants_attributes][1489697584487][salary]" id="event_participants_attributes_1489697584487_salary"></div>
  <div class="links">
    <input type="hidden" name="event[participants_attributes][1489697584487][_destroy]" id="event_participants_attributes_1489697584487__destroy" value="false"><a class="btn btn-danger btn-xs btn-remove-friend remove_fields dynamic" href="#">Remove this friend</a>
  </div>
</div>

这是我的功能/ events / create_event.spec.rb

require 'spec_helper'
require 'rails_helper'

describe "Creating an event" do
  it "redirects on result page on success" do
    visit "/"
    click_link "Create a new event"
    expect(page).to have_content('Wanna share fair?')

    fill_in :name, with: 'Rent a plane'
    fill_in "What is the total price", with: 200
    click_link "Add a participant"


    fill_in "event_participants_attributes_1489697584487_first_name", with: "John"
enter code here
    fill_in ":event_participants_attributes_1489697584487_salary", with: 2300

    click_button "See result"


    expect(page).to have_content('Your salary together:')
 end
end

这是我的simple_forms:

_form.html.erb

<%= simple_form_for @event do |f| %>
      <div class="col-xs-12 col-md-10 col-md-offset-1">
        <h1>Wanna share fair?</h1>
        <p>Create an event, enter the bill to share.</p>

        <%= f.input :name, label: "Event's name" %>

        <%= f.input :total_price, label: "What is the total price" %>

        <p>Add the participants.</p>
        <div id="participants">
          <%= f.simple_fields_for :participants do |participant| %>
          <%= render "participants_fields", f: participant %>
          <% end %>
          <div class="links text-center">
            <%= link_to_add_association "Add a participant", f, :participants, partial: "participants_fields", class:"btn btn-primary btn-sm btn-add-friend" %>
          </div>
        </div>
      </div>
    <div class="col-xs-12  col-md-10 col-md-offset-1 text-center">
      <%= f.submit "See result" , class:"btn btn-success btn-lg btn-event"  %>
    </div>
    <% end %>

_participants_fields.html.erb

<div class="nested-fields">
  <%= f.input :first_name, label: "Participant's first name" %>
  <%= f.input :salary, label: "Participant's monthly pay" %>
  <div class="links">
    <%= link_to_remove_association "Remove this friend", f , class: "btn btn-danger btn-xs btn-remove-friend" %>
  </div>
</div>

编辑更多html

<form novalidate="novalidate" class="simple_form new_event" id="new_event" action="/events" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="O5XM0JMiVBbpPNaKF1apw7rdtC/XEBhasZQoK6POycZQ2Zp14Td1ljoJyIUKTwtl91LlBHeDkKFtcWRnNu+iEQ==">
      <div class="col-xs-12 col-md-10 col-md-offset-1">
        <h1>Wanna share fair?</h1>
        <p>Create an event, enter the bill to share.</p>

        <div class="form-group string optional event_name"><label class="control-label string optional" for="event_name">Event's name</label><input class="form-control string optional" type="text" name="event[name]" id="event_name" data-com.agilebits.onepassword.user-edited="yes"></div>

        <div class="form-group integer optional event_total_price"><label class="control-label integer optional" for="event_total_price">What is the total price</label><input class="form-control numeric integer optional" type="number" step="1" name="event[total_price]" id="event_total_price" data-com.agilebits.onepassword.user-edited="yes"></div>

        <p>Add the participants.</p>
        <div id="participants">
                    <div class="nested-fields">
  <div class="form-group string optional event_participants_first_name"><label class="control-label string optional" for="event_participants_attributes_1489705438668_first_name">Participant's first name</label><input class="form-control string optional" type="text" name="event[participants_attributes][1489705438668][first_name]" id="event_participants_attributes_1489705438668_first_name" data-com.agilebits.onepassword.user-edited="yes"></div>
  <div class="form-group integer optional event_participants_salary"><label class="control-label integer optional" for="event_participants_attributes_1489705438668_salary">Participant's monthly pay</label><input class="form-control numeric integer optional" type="number" step="1" name="event[participants_attributes][1489705438668][salary]" id="event_participants_attributes_1489705438668_salary" data-com.agilebits.onepassword.user-edited="yes"></div>
  <div class="links">
    <input type="hidden" name="event[participants_attributes][1489705438668][_destroy]" id="event_participants_attributes_1489705438668__destroy" value="false"><a class="btn btn-danger btn-xs btn-remove-friend remove_fields dynamic" href="#">Remove this friend</a>
  </div>
</div><div class="nested-fields">
  <div class="form-group string optional event_participants_first_name"><label class="control-label string optional" for="event_participants_attributes_1489705443842_first_name">Participant's first name</label><input class="form-control string optional" type="text" name="event[participants_attributes][1489705443842][first_name]" id="event_participants_attributes_1489705443842_first_name" data-com.agilebits.onepassword.user-edited="yes"></div>
  <div class="form-group integer optional event_participants_salary"><label class="control-label integer optional" for="event_participants_attributes_1489705443842_salary">Participant's monthly pay</label><input class="form-control numeric integer optional" type="number" step="1" name="event[participants_attributes][1489705443842][salary]" id="event_participants_attributes_1489705443842_salary" data-com.agilebits.onepassword.user-edited="yes"></div>
  <div class="links">
    <input type="hidden" name="event[participants_attributes][1489705443842][_destroy]" id="event_participants_attributes_1489705443842__destroy" value="false"><a class="btn btn-danger btn-xs btn-remove-friend remove_fields dynamic" href="#">Remove this friend</a>
  </div>
</div><div class="links text-center">
            <a class="btn btn-primary btn-sm btn-add-friend add_fields" data-association="participant" data-associations="participants" data-association-insertion-template="<div class=&quot;nested-fields&quot;>
  <div class=&quot;form-group string optional event_participants_first_name&quot;><label class=&quot;control-label string optional&quot; for=&quot;event_participants_attributes_new_participants_first_name&quot;>Participant&amp;#39;s first name</label><input class=&quot;form-control string optional&quot; type=&quot;text&quot; name=&quot;event[participants_attributes][new_participants][first_name]&quot; id=&quot;event_participants_attributes_new_participants_first_name&quot; /></div>
  <div class=&quot;form-group integer optional event_participants_salary&quot;><label class=&quot;control-label integer optional&quot; for=&quot;event_participants_attributes_new_participants_salary&quot;>Participant&amp;#39;s monthly pay</label><input class=&quot;form-control numeric integer optional&quot; type=&quot;number&quot; step=&quot;1&quot; name=&quot;event[participants_attributes][new_participants][salary]&quot; id=&quot;event_participants_attributes_new_participants_salary&quot; /></div>
  <div class=&quot;links&quot;>
    <input type=&quot;hidden&quot; name=&quot;event[participants_attributes][new_participants][_destroy]&quot; id=&quot;event_participants_attributes_new_participants__destroy&quot; value=&quot;false&quot; /><a class=&quot;btn btn-danger btn-xs btn-remove-friend remove_fields dynamic&quot; href=&quot;#&quot;>Remove this friend</a>
  </div>
</div>

" href="#">Add a participant</a>
          </div>
        </div>
      </div>
    <div class="col-xs-12  col-md-10 col-md-offset-1 text-center">
      <input type="submit" name="commit" value="See result" class="btn btn-success btn-lg btn-event" data-disable-with="See result">
    </div>
</form>

1 个答案:

答案 0 :(得分:1)

IIRC&#34; 1489697584487&#34;您显示的部分ID是由Cocoon根据当前时间戳创建的。这意味着每次运行测试时,数字都会有所不同,因此您无法按ID选择这些元素。

相反,如果您只添加一个参与者,则应该可以执行类似

的操作
fill_in "Participant's first name", with: "John"
fill_in "Participant's monthly pay", with: "2300"

如果您要添加多个参与者,那么根据确切的布局,您可以使用nth-child / nth-of-type CSS选择器来填充fill_in或使用all并选择您想要的那个,等等。 / p>

find('.nested-fields:nth-child(2)').fill_in ...
all(:field, "Participant's first name", minimum: 2)[1].set("John") # 0 based index so minimum should be 1 more than the index you want

此外 - 对于任何此类工作,您需要使用支持JS的驱动程序(Cocoon需要JS)。您尚未使用js: true元数据标记您的规范,因此要么覆盖默认驱动程序,要么您当前没有使用支持JS的驱动程序 - 请参阅https://github.com/teamcapybara/capybara#selecting-the-driver。如果您尚未配置database_cleaner以用于支持JS的驱动程序,请参阅https://github.com/teamcapybara/capybara#transactions-and-database-setuphttps://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example