我有以下表格
<%= 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'route_origin_input',其中:“Kimmage,都柏林,爱尔兰”Capybara :: ElementNotFound:无法找到字段“route_origin_input”#/ User / jeff / .rvm / gems / ruby-2.0.0-p594 / gems / capybara-2.4.4 / lib / capybara / node / finders.rb:41:在找到'#/ Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4的块中 . 4 / lib / capybara / node / base.rb:84:同步'#/ Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/node/ finders.rb:30:在找到'#/ 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 <class:Session>中的块(2级) '#/Users/jeff/.rvm/gems/ruby-2.0.0-p594/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in块中的(2级)'# . / specpec /requests/route_pages_spec.rb:30:在'块(4级)中'
我在其他测试中使用了fill_in,唯一的区别是我在表单中使用了标准的html代码 . 这些字段用于处理用户输入的信息,然后将结果添加到作为模型一部分的隐藏字段中,基本上从Google maps api获取结果 .
EDIT: 这是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 回答
HTML和规范看起来不错 . 这是我要尝试的一些事情:
你确定它在
route_origin_input
上失败而不是另一个字段吗?如果在没有
:js => true
的情况下运行该怎么办?也许有一些Javascript阻止表单呈现或重定向 .使用save_and_open_page和/或获取屏幕截图以查看发生了什么 .
您使用的是哪个驱动程序?不同的驱动程序表现不同 . 尝试不同的驱动程序 .
试试
selenium-webdriver
. 它运行浏览器,你可以看到发生了什么 . 在fill_in
之前使用sleep 10
. 规范将暂停,您可以检查表单或手动填写 .通常我使用
within '.form' do
. 这可能会有所不同 . 有关如何使用within
,请参见Capybara docs .