简体   繁体   English

水豚找不到字段

[英]Capybara is unable to find a field

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 由于某种原因,水豚无法找到我用html添加的两个文本字段

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 ' 失败/错误:用“爱尔兰都柏林的Kimmage”填充“ route_origin_input” Capybara :: ElementNotFound:无法找到字段“ route_origin_input”#/Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems /capybara-2.4.4/lib/capybara/node/finders.rb:41: block in find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/base.rb:84:in同步'#/Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/finders .rb:30:在find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in jeff/ find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in gems/ find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in 2.0.0- find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in gems/ find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in 2.4.4/ find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in capybara/ find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in actions.rb: find' # /Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/actions.rb:56:in :in fill_in' #/Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/session.rb:676 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 jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:在'#./spec/ request / route_pages_spec.rb:30:in'中的块(4个级别)

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. 我在其他测试中使用了fill_in,唯一的区别是我在表单中使用了标准的html代码。 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. 这些字段用于处理用户输入的信息,然后将结果添加到我的模型的隐藏字段中,基本上从Google Maps api获取结果。

EDIT: Here is the HTML 编辑:这是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>

The HTML and spec look OK. HTML和规范看起来不错。 Here's some things I would try: 这是我会尝试的一些方法:

  1. Are you sure it is failing on route_origin_input and not another field? 您确定在route_origin_input而不是其他字段上失败吗?
  2. What if you run it without :js => true ? 如果不使用:js => true运行该怎么办? Perhaps there is some Javascript that is preventing the form from rendering or redirecting. 也许有一些Javascript阻止了表单的呈现或重定向。
  3. Use save_and_open_page and/or get a screenshot to see what is going on. 使用save_and_open_page和/或获取屏幕截图以查看发生了什么。
  4. Which driver are you using? 您正在使用哪个驱动程序? Different drivers behave differently. 不同的驱动程序表现不同。 Try a different driver. 尝试其他驱动程序。
  5. Try selenium-webdriver . 尝试selenium-webdriver It runs a browser and you can see what is going on. 它运行一个浏览器,您可以看到发生了什么。 Use sleep 10 before the fill_in . fill_in之前使用sleep 10 The spec will pause and you can inspect the form or manually fill it in. 规范将暂停,您可以检查表格或手动填写。
  6. Usually I use within '.form' do . 通常我within '.form' do That may make a difference. 那可能会有所作为。 See Capybara docs for how to use within . 水豚文档如何使用within

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM