Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find field "Name" (Capybara::ElementNotFound)

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????

like image 349
Andre Tachian Avatar asked Dec 13 '12 18:12

Andre Tachian


2 Answers

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'
like image 124
danlee Avatar answered Sep 22 '22 20:09

danlee


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.

like image 36
WattsInABox Avatar answered Sep 20 '22 20:09

WattsInABox