Unable to find field "Name" (Capybara::ElementNotFound) - ruby-on-rails-3

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

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'

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.

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

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

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.

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.

Related

Problems Using Thinking Sphinx with Rails 3.2.1

I am attempting to implement Thinking Sphinx 2.0.10 which from my understanding is compatible with Rails 3. I am very new to programming using Ruby on Rails. I have gone through quite a few articles on StackOverflow but could not find a solution to my problem.
I installed the gem without the :require parameter using in my Gemfile.
gem 'thinking-sphinx', '2.0.10'
I have a working Rails application that displays a list that I would like to add a search to.
Here is my code for defining the indexes in my model file.
define_index do
indexes :email, :sortable => true
indexes :name, :sortable => true
indexes microposts.content, :as => :micropost_content
end
Here is the Rails code in my controller file. The commented out line is the original code that is working. I have a default for will_paginate I think in the application controller for 15 records per page.
def index
#users = User.search params[:search], :per_page => 15
# #users = User.paginate(page: params[:page])
end
Here is the search box that I added to my index page.
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
Here is my rSpec code. This is the original code that I was using before I attempted to implement Thinking Sphinx.
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_selector('title', text: 'All users') }
describe "pagination" do
before(:all) { 15.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
let(:first_page) { User.paginate(page: 1) }
let(:second_page) { User.paginate(page: 2) }
it { should have_link('Next') }
it { should have_link('2') }
it { should_not have_link('delete') }
it "should list each user" do
User.all[0..2].each do |user|
page.should have_selector('li', text: user.name)
end
end
it "should list the first page of users" do
first_page.each do |user|
page.should have_selector('li', text: user.name)
end
end
it "should not list the second page of users" do
second_page.each do |user|
page.should_not have_selector('li', text: user.name)
end
end
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end
end
When I run my rSpec test I get the following error:
Failure/Error: visit users_path
ActionView::Template::Error:
getaddrinfo: nodename nor servname provided, or not known
The page displays just fine. When I enter text in the search box and click the button nothing happens which is no surprise to me.
I am able to run Sphinx on the terminal. The searches work fine. I just do not know enough to debug this problem with Thinking Sphinx. I have searched many pages on this website and many others the past few days but none of them are dealing with this issue. Any help would be appreciated.
Did you initialize the sphinx database?
Usually you just need to do something like:
rake ts:rebuild
This should automatically run rake ts:conf for you and rebuild your indexes.
You could also use rake ts:in to update the indexes.
A simple way to test if this is working is to run the rails console (rails c) and try manually searching your users (User.search).
If you get any results, the indexes are available and you can troubleshoot your views/controllers next ;)

Rails simple_forms collections radio button broken down

I am attempting to make a voting system for some video. On the vote page I want a radio button for each finalist and next to it an image pulled form youtube the file name and the finalist name. I have a domain for submission and user and made a domain for the voting that is associate to both with the fields submission_id and user_id with submission has_many votes and users has_one vote. Using simple form I create the following:
= simple_form_for #vote do |f|
= f.input :submission_id, :as => :radio, :collection => #finalists, :label_method => :file_name, :value_method => :id, :label => false
= f.submit "Submit Vote 2"
This does work and it puts up a radio button for each and allows the user to select on of the submissions to vote for. However, wanted to put next to the radio button a thumbnail and some text to make it clear what they are voting for. I tried just making a field of HTMl text that of the all the information I wanted:
<img src="youtube/thumbnail" /> <div> Text for label</div>
However, it just prints out the HTML mark up. tried raw(:file_name) but that errored out with bad syntax.
At this point I though I might just need to break down the colleciton and make the loop my self. I tryed the following:
= simple_form_for #vote do |f|
- #finalists.each do |finalist|
= f.input :submission_id, :as => :radio, :value => finalist.id, :input_html => {:name => 'vote[submisison_id]', :id => "vote_submission_id_#{finalist.id}", :value => finalist.id}
= f.submit "Submit Vote"
This results in 5 different radio button collections with their values being yes/no. I of course want one set of radio buttons where one of the submissions gets selected.
I have looked up a few different page on radio_button_tag and collections and I am having a lot of troubles trying to figure out how to tell rails that I want all the radio button to be part of the same set.
Any help would be much appreciated. Thank you.

Can't find field when using symbol reference

I'm playing around with RoR for the first time and hit a weird error. I have the following test code for my navigation links:
describe "when logged in" do
before(:each) do
#user = Factory(:user)
visit login_path
#the line below is the weird one
fill_in "session[user_name]", :with => #user.user_name
fill_in :password, :with => #user.password
click_button
end
it "should have navigation links" do
response.should have_selector("nav")
end
it "should have link to log out" do
response.should have_selector("a", :href => logout_path, :content => "Log out")
end
end
the code above works just fine, but if I would change the line
fill_in "session[user_name]", :with => #user.user_name
to
fill_in :user_name, :with => #user.user_name
it won't work and I can't figure out why. The error I get is
Failure/Error: fill_in :user_name, :with => #user.user_name
Webrat::NotFoundError:
Could not find field: :user_name
The relevant generated html is:
<label for="session_user_name">User name</label><br/>
<input id="session_user_name" name="session[user_name]" size="30" type="text" />
<label for="session_password">Password</label><br/>
<input id="session_password" name="session[password]" size="30" type="password" />
If you look at the code you see I do exactly that for the password, and that works just fine. I would like to use the syntax which is causing the error so am I doing something wrong?
Try this:
fill_in "user name", :with => #user.user_name
I think webrat is being nice, and its able to find your label 'Password' because it is case insensitive and its turning :password into 'Password'.
You could also use the HTML id attribute, i.e.
fill_in :session_user_name, :with => #user.user_name
One gotcha here is if you are thinking of switching to use Capybara rather than Webrat: Capybara is case-sensitive, so it would fail on fill_in "user name".
I'm not sure what the best practice is here, but I have switched to using the HTML id and name attributes rather than the label text in my own code. The reason for this is that it is less brittle because people change the customer-facing text on the site more than they change the semantic elements and names.

Rails Session Incorrect?

I am using capybara for an integration test and it looks like something is wrong with the session that gets passed to my controller for my "sign in" part of a test. The sign in works fine when going through a browser, but is failing in capybara.
def integration_sign_in(user)
visit signin_path
puts "Pre email: " + user.email
puts "Pre password: " + user.password
# When I use these, everything works correctly
fill_in "session_email", :with => user.email
fill_in "session_password", :with => user.password
# When I use these, the session is wrong
# The params[:session][:email] on the server has the password field
# The params[:session][:password] on the server is nil
#fill_in :email, :with => user.email
#fill_in :password, :with => user.password
click_button "Sign in"
end
Can I not use symbols for the capybara tests? My guess is that the first field (email) is being filled in for both fields in the failing case, which is why the session only has a value for email and that value is password.
It looks like the latest version of capybara doesn't accept symbols correctly. It only works when the full string of the field is entered.

Rails 3: f.select - options_for_select

I have a form on my Ruby on Rails3 Application with a drop menu, this is my current code for the select option:
<%= f.select :phone_type, options_for_select(["Select One", "Cell", "Work", "Office", "Home", "Other"],:disabled => ["Select One"]), :class => 'genForm_dropBox' %>
From my understanding this should have "Select One" as the default option when someone opens the page, but if they don't select one of the other options an error displays when they hit submit.
This is true in Browsers like Safari and Chrome and IE7, but in Firefox and IE8 it shows "Cell" as the first option as Select One is disabled.
I'd like it to display "Select One" by default, but have it as an unusable option when they submit the form. Do I need to script this into the controller, or model? or do I have this coded in the form wrong?
for those looking to incorporate this feature, I've taken a new approach from the model end of things. Being that all fields are required to be filled out in order for the user to submit and not receive an error alert, I gave the "Submit One" option a default value of nothing. You can take a look at the following code to see how I did that.
<%= f.select :phone_type, options_for_select([["Select One", ""], "Cell", "Work", "Office", "Home", "Other"]), :class => 'genForm_dropBox' %>
This is a little cleaner:
<%= f.select :phone_type, [ 'Cell', 'Work', 'Office', 'Home', 'Other' ], :prompt => 'Select One' %>
The :prompt argument generates an option with an empty value.
In Rails 4, this approach works well for me.
<%= f.select :status, options_for_status, prompt: 'Select One' %>
Meanwhile I have defined the options in a helper to keep the clutter out of my view.
def options_for_status
[
['First Option','first_option'],
['Second Option','second_option']
]
end
Thanks to everyone who contributed an answer.
I needed similar code for a project I'm working on and I really liked the approach Ryan Burnette took.
This is what worked for me using Rails 4.1.0.
<%= f.select :season, options_for_seasons, :prompt => 'Select One' %>
Then I defined the options in my helper.
def options_for_seasons
['Spring', 'Summer', 'Autumn', 'Winter']
end
I went with:prompt => 'Select One'because I only wanted the "Select One" option to be listed in the edit form if a season had not been previously selected.
Adding the ["Select One", ""] causes the edit screen to alway display "Select One" rather than the stored value.
Rails 3.1 (2012 Aug 17)
could be <%= f.select :phone_type, options_for_select(["Cell", "Work", "Office", "Home", "Other"]), :prompt => "Select One", :class => 'genForm_dropBox' %>