Matthew
Matthew

Reputation: 21

Capybara fill_in only works with field id, why?

While runnng my rspec tests, I was getting an error Capybara::ElementNotFound: Unable to find field "First Name"

My rspec test is as follows

 describe "with valid information" do
  before do
    puts page.body
    fill_in "First Name",   with: "Matthew"
    fill_in "Email",        with: "[email protected]"
    fill_in "Password",     with: "foobar"
  end

  it "should create a user" do
    expect { click_button submit }.to change(User, :count).by(1)
  end
end

My form view is as so

<%= form_for(@user) do |f| %>
  <%= f.label :first_name %>
  <%= f.text_field :first_name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>

So after a couple hours of head bashing, I tried replacing

fill_in "First Name",   with: "Matthew"

with

fill_in "user_first_name",   with: "Matthew"

(i.e. using the field id and not the name)

My question is why does this work only with the field id ('user_first_name') and not the field name ('First Name')?

Upvotes: 2

Views: 499

Answers (1)

Louis Simoneau
Louis Simoneau

Reputation: 1791

Rails uses the human_attribute_name method to output the label text when you do f.label :first_name.

This means the text of the label is "First name", not "First Name", so that's what you'll need to provide to Capybara.

Upvotes: 1

Related Questions