Capybara无法找到一块土地

时间:2014-11-20 22:50:56

标签: ruby-on-rails ruby-on-rails-4 capybara

我有以下表格

<%= 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 %>

以及以下测试

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

出于某种原因,capybara无法找到我用html添加的两个文本字段

我收到的错误是

  

失败/错误:fill_in&#39; route_origin_input&#39;,with:&#34; Kimmage,Dublin,   爱尔兰&#34;        水豚:: ElementNotFound:          无法找到字段&#34; route_origin_input&#34;        #/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 同步&#39;        #/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&#39;        #/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 阻止(2级)&#39;        #./spec/requests/route_pages_spec.rb:30:在&#39;

中的块(4级)

我在其他测试中使用过fill_in,唯一的区别是我在表单中使用了标准的html代码。这些字段用于处理用户输入的信息,然后将结果添加到作为模型一部分的隐藏字段中,基本上从Google maps api获取结果。

修改 这是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 个答案:

答案 0 :(得分:1)

HTML和规范看起来不错。这是我要尝试的一些事情:

  1. 你确定它在route_origin_input而不是另一个领域失败吗?
  2. 如果在没有:js => true的情况下运行该怎么办?也许有一些Javascript阻止表单呈现或重定向。
  3. 使用save_and_open_page和/或获取屏幕截图,了解正在发生的事情。
  4. 您使用的是哪个驱动程序?不同的驱动程序表现不同。尝试不同的驱动程序。
  5. 试试selenium-webdriver。它运行浏览器,你可以看到发生了什么。在sleep 10之前使用fill_in。规范将暂停,您可以检查表单或手动填写表格。
  6. 通常我使用within '.form' do。这可能会有所不同。有关如何使用within
  7. ,请参阅Capybara docs