本文介绍了为什么我的Capybara / Poltergeist测试无法从jQuery自动完成字段中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:经过大量艰苦的工作,我已经解决了此问题。我很高兴成为任何需要帮助的人的资源。这是我的

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驱动程序通常很慢​​,速度很慢,没有一个驱动程序能够覆盖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(缩进的下拉列表)上也有类似的代码,但是由于某种原因它会定期中断(有人可能会说它有一个poltergeist吗?到目前为止,我已经在硒和poltergeist之间进行了几次测试切换),不确定为什么autocompleter对您不起作用,但感觉像是个错误,

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,

将问题提交给(您已经有?),尝试改变为se lenium或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自动完成字段中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 15:35