George Shaw
George Shaw

Reputation: 1781

Capybara won't fill in field with duplicate labels

I have a form page with a confirmation field for the email address and the password, both with the label "Confirmation". This fails when running with RSpec, as expected. If I make the labels unique, say by changing the email label to "Email Confirmation", then it will pass. I'd rather keep the labels the same. If I use the email ID generated by form_for, Capybara seems happy but the test fails. I've also tried the name with the same result.

RSpec/Capybara extract:

fill_in "company_contact_email_confirmation", with: "[email protected]"

form_for extract:

<div class="field">
  <%= f.label :contact_email_confirmation, "Confirmation" %><br />
  <%= f.email_field :contact_email_confirmation %>
</div>

Generated source:

<div class="field">
  <label for="company_contact_email_confirmation">Confirmation</label><br>
  <input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email">
</div>

Error message:

1) Company pages signup with valid information should create a company

 Failure/Error: expect { click_button submit }.to change(Company, :count).by (1)
   count should have been changed by 1, but was changed by 0

If I use Capybara within, as has been suggested elsewhere here, it still does not work:

RSpec/Capybara extract:

within ".email_stuff" do
  fill_in "Confirmation", with: "[email protected]"
end

form_for extract:

<div class="email_stuff">
  <div class="field">
    <%= f.label :contact_email_confirmation, "Confirmation" %><br />
    <%= f.email_field :contact_email_confirmation %>
  </div>
</div>

Generated source:

<div class="email_stuff">
  <div class="field">
    <label for="company_contact_email_confirmation">Confirmation</label><br />
    <input id="company_contact_email_confirmation" name="company[contact_email_confirmation]" size="30" type="email" />
  </div>
  </div>

Capybara does not seem to complain but the test fails with the same error. How do I get Capybara to allow me to use the same label twice on the same form?

Upvotes: 1

Views: 901

Answers (1)

George Shaw
George Shaw

Reputation: 1781

I added some debugging statements to the ActiveRecord after_validation callback to see the status of the virtual confirmation fields. As shown in my code above, I'd referenced the email confirmation field by name or ID, but not BOTH the password confirmation and email confirmation fields with IDs. Capybara was putting the password data in the wrong confirmation field since the email confirmation field was first on the form. If I'd put the IDs on the password field, it would have worked. IDs on both are safest.

Upvotes: 0

Related Questions