问题描述
更新:经过大量艰苦的工作,我已经解决了这个问题.我很高兴成为任何需要帮助的人的资源.这是我的工作设置的要点.
UPDATE: I have fixed this problem after lots of painstaking work on my own. I am happy to be a resource to anybody needing a hand with this. Here is a gist of my working setup.
我已经尝试了所有我能找到的 Google 和 SO 解决方案.以下是我尝试过的一些不同的事情:
I have tried every solution I could find Google and SO. Here are some different things I have tried:
page.execute_script %Q{$('#{selector}').val('#{value}').trigger('keydown')}
和
fill_in field, with: options[:with]
page.execute_script %Q{ $('##{field}').trigger('focus') }
page.execute_script %Q{ $('##{field}').trigger('keydown') }
这就是失败的原因:
page.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
但是当我在 Firebug 中查看它并在浏览器中测试它时,它肯定存在.
But it's definitely there when I look at it in Firebug and test it in the browser.
以下是所有详细信息,包括对上述内容的重述.请记住,自动完成字段在浏览器中运行良好.
Here are all of the details, including a restatement of those above. Remember, the autocomplete field works fine in the browser.
listing_integration_spec.rb
require "spec_helper"
describe "Listing Integration" do
let!(:user) { login_user }
it "lets a user add information listing", js: true do
listing = create(:listing, user: user)
click_link('Additional Information')
click_link('Create')
fill_autocomplete('listings_search', with: listing.item_id)
end
end
spec/support/feature_helper.rb
def fill_autocomplete(field, options = {})
fill_in field, with: options[:with]
page.execute_script %Q{ $('##{field}').trigger('focus') }
page.execute_script %Q{ $('##{field}').trigger('keydown') }
selector = %Q{ul.ui-autocomplete li.ui-menu-item a:contains('#{options[:with]}')}
page.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
page.execute_script %Q{ $("##{selector}").trigger('mouseenter').click() }
end
来自视图模板的ERB
<%= simple_fields_for :listings do |f| %>
<%= f.input :search, label: "Search by Listing", required: true %>
<% end %>
和 Coffeescript:
and the Coffeescript:
$("#listings_search").autocomplete
source: (request, response) ->
options =
term: request.term
$.get "/search_listings", options, (data) ->
if data.length == 0
alert "No listings found."
response data
minLength: 2
select: (event, ui) ->
add_listing_hash =
type: "GET"
url: "/add_listing"
data: { id: ui.item.id }
success: () ->
$.ajax(add_listing_hash)
推荐答案
JS 驱动程序通常是 meh,它们很慢,而且它们中的任何一个都不能覆盖 100% 的功能,而且它们通常很古怪且难以调试,但我相信你现在已经明白了.
JS drivers are generally meh, they're slow and not single one of them covers 100% of function, and they're often quirky and hard to debug, but I'm sure you've got that figured out by now.
我有一段类似的代码在 rails 3.2、minitest 和 poltergeist 1.3.0(一个 ajaxed 下拉列表)上工作,但它会无缘无故地定期中断(有人可能会说它有一个 poltergeist?我已经求助了到目前为止,在 selenium 和 poltergeist 之间切换了几次测试),不确定为什么自动完成程序对您不起作用,但感觉像是一个错误,
I've got similar piece of code working on rails 3.2, minitest and poltergeist 1.3.0 (an ajaxed dropdown) but it kind of breaks periodically for no good reason (one might say it has a poltergeist? I have already resorted switching that test between selenium and poltergeist a couple times so far), not sure why autocompleter wouldn't work for you but it feels like a bug,
将问题提交给https://github.com/jonleighton/poltergeist(您已经有了?https://github.com/jonleighton/poltergeist/issues/439),尝试更改为selenium 或 webkit,看看它是否有效,如果它能让你摆脱困境,你可以在这个测试中使用不同的驱动程序(它比一个有效的小部件浪费了几天的工作时间).
submit issue to https://github.com/jonleighton/poltergeist (you already have? https://github.com/jonleighton/poltergeist/issues/439), try changing to selenium or webkit, see if it works, you can use a different driver in this one test if it gets you out of the woods (it beats losing days of work over a widget that works).
这篇关于为什么我的 Capybara/Poltergeist 测试不能从 jQuery 自动完成字段中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!