2
votes

I have the following form

<%= form_for(@route, :html => {:class => "form-horizontal", :role => 'form'}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<div class="form-group">
    <input type="text" name="route_origin_input" id="route_origin_input" placeholder="From" onFocus="geolocate(); autocompleteLocation(this,route_origin)" class="form-control" />
    <%= f.hidden_field :origin, class: "form-control" %>
</div>

<div class="form-group">
    <input type="text" name="route_destination_input" id="route_destination_input" placeholder="Destination" onFocus="geolocate(); autocompleteLocation(this,route_destination)" class="form-control" />
    <%= f.hidden_field :destination, class: "form-control" %>
</div>

<div class="form-group">
   <% options = options_from_collection_for_select(@vehicleList, 'vehicle', 'vehicle') %>
   <%= f.select :vehicle,  options, class: "form-control" %>
</div>

<div class="form-group">
   <%= f.date_field :departure_date, class: "form-control" %>
</div>

<%= f.submit "Post", class: "btn btn-large btn-primary", id: "route_post" %>
<% end %>

and the following test

require 'spec_helper'

describe "Route pages" do

subject { page }

let(:user) { FactoryGirl.create(:user) }
before { sign_in user }

describe "route creation" do
    before { 
        visit terminal_path 
    }

    describe "with invalid information" do

        it "should not create a route" do
            expect { click_button "route_post" }.not_to change(Route, :count)
        end

        describe "error messages" do
            before { click_button "route_post" }
            it { should have_danger_message('Not Submitted') }
        end

    end

    describe "with valid information", :js => true do 

        before do
            fill_in 'route_origin_input', with: "Kimmage, Dublin, Ireland"

            fill_in 'route_destination_input', with: "Kimmage, Dublin, Ireland"

            fill_in 'route_departure_date', with: Time.now
            select 'Car', :from => "route_vehicle"
        end

        it "should create a route" do
            expect { click_button "route_post" }.to change(Route, :count).by(1)
        end

    end
end
end

For some reason the two text fields that I have added with html cannot be found by capybara

The error I receive is

Failure/Error: fill_in 'route_origin_input', with: "Kimmage, Dublin, Ireland" Capybara::ElementNotFound: Unable to find field "route_origin_input" # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/finders.rb:41:in block in find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/base.rb:84:in synchronize' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/finders.rb:30:in find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in fill_in' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/session.rb:676:in block (2 levels) in <class:Session>' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in block (2 levels) in ' # ./spec/requests/route_pages_spec.rb:30:in `block (4 levels) in '

I've used fill_in in other tests and the only difference I can see is that I have used standard html code in the form. These fields are used to process the info the user enters and then add the result to a hidden field that is part of my model, basically getting the result from Google maps api.

EDIT: Here is the HTML

<div class="form-group">
    <input type="text" name="route_origin_input" id="route_origin_input" placeholder="From" onFocus="geolocate(); autocompleteLocation(this,route_origin)" class="form-control" />
    <input class="form-control" id="route_origin" name="route[origin]" type="hidden" />
</div>
1
try page.find(:fillable_field, 'route_origin_date').set "Kimmage, Dublin, Ireland"Michael Cruz
...or you might not need to the 'page.' prefix on that line. I can't remember for certain.Michael Cruz
Can you show the rendered HTML?B Seven
@BSeven that is added nowJeff Finn

1 Answers

1
votes

The HTML and spec look OK. Here's some things I would try:

  1. Are you sure it is failing on route_origin_input and not another field?
  2. What if you run it without :js => true? Perhaps there is some Javascript that is preventing the form from rendering or redirecting.
  3. Use save_and_open_page and/or get a screenshot to see what is going on.
  4. Which driver are you using? Different drivers behave differently. Try a different driver.
  5. Try selenium-webdriver. It runs a browser and you can see what is going on. Use sleep 10 before the fill_in. The spec will pause and you can inspect the form or manually fill it in.
  6. Usually I use within '.form' do. That may make a difference. See Capybara docs for how to use within.