14
votes

I'm trying to use capybara+rspec and get this error: Unable to find field "Name" (Capybara::ElementNotFound)

Here is my form:

%h2 Sign up
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f|
  = f.error_notification
  = display_base_errors resource
  = f.input :name, :autofocus => true
  = f.button :submit, 'Sign up', :class => 'btn-primary'
= render "devise/shared/links"

Here is my user_steps.rb

When /^I sign up with valid user data$/ do
  create_visitor
  sign_up
end

def create_visitor
  @visitor ||= { :name => "Test visitor"}
end

def sign_up
  visit '/users/sign_up'
  fill_in "Name", :with => @visitor[:name]
  click_button "Sign up"
end

What's wrong????

6
When an element is not found it could denote that either you didn't write the element or your test is looking at the wrong page. I would first check the page source of /users/sign_up to see if you matched the appropriate name or id to your test. If that's right, then I would check to see if there are any obstructions the test might have when reaching /users/sign_up.thank_you
Drop in save_and_open_page just after your fill_in and you will be able to see what Capybara actually thinks is on the page.nmott
good suggestion about save_and_open_page @nmott but should point out that you need the launchy gem in your Gemfile.SteveTurczyn
@jason328 thx mate was exactly my problem!BKSpurgeon

6 Answers

5
votes

I encountered this issue and realized that Capybara filtered out hidden fields--my element belonged to a non-active tab (hidden) in a multi-tab page. I just passed the :visible arg and set it to false, and voila! the element was found.

fill_in "content", with: 'foo bar', visible: false

or

find('#item_content', visible: false).set 'foo bar'
5
votes

It looks like to me that you are looking for a field label Name, but your name field does not have a label, so you probably need to use the ID of the field or the name which is probably:

"#{resource_name}[name]"

Also, as @nmott said in his comment, you should try using save_and_open_page so that you can actually look at the page, However, be aware you need the launchy gem to use this method.

Furthermore, what you might discover is you're not even on the right page. My usual problem when testing pages OTHER than the sign up page is that I've been redirected and didn't know it. So after using visit, you should also use assert_template to make sure you're on the right page.

4
votes

I have the same problem myself and I had to stop using fill_in all together. What I did was replacing all occurrences of fill_in with the following code :

    element = page.find("Name")
    element.set(@visitor[:name])

I guess you can encapsulate this into a method in order to make your tests more smooth

2
votes

Try to narrow down your scope from within the form as such

within("form#whatever_form_id") do
  fill_in "Name", :with => @visitor[:name]
  click_button "Sign up"
end
0
votes

Ok guys I found it out having the same issue , its very simple : In capybara or rspec they need you to but "Name" and in your form or label field you need to write "name" in small....there you go works for me.

0
votes

For me it was the next line of the spec that was causing the problem not the fill_in "name" line. It didn't matter whether or not name was "Name" or "name". The next click_button line for me had click_button "Wrong Name" which was the wrong name for the button, and this did not give the expected error of "can't click on button "Wrong Name" but instead gave can't find field "name". A bit verbose for my first even post on stack overflow. Bottom line. Consider the line below the line given in the capybara error message.