下拉菜单在应该被选中的时候没有被选中……为什么?

Dropdown not getting selected when it should be ... why?

我正在尝试解决我们测试中的一个错误,我认为它应该有效。我很确定这是 selectize 或 capybara 中的错误,但我不明白为什么。

我已经查看了水豚的源代码,一切似乎都在正常工作。我不太确定如何前进。

为了测试这个错误,我已经尽可能地把这个错误剥离成一个小 test application。请参阅下面的设置

bugs/show.html.erb

  <select id="select-repo" class="repositories selectized" placeholder="Pick a repository...">
  </select>

  <select id="dropdown1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
  </select>

  <select id="dropdown2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
  </select>

bug_spec.rb

feature 'bug' do
  it "spec setup", js: true do
    visit bug_path

    find('div.selectize-input input', match: :first).set('exercism.io')
    select 'Four', from: 'dropdown1' # this is not getting selected
    select 'Four', from: 'dropdown2'

    sleep 2

    expect(page).to have_select('dropdown1', selected: 'Four') # testing that dropdown1 is being selected
  end
end

# note that the javascript to initialize the selectize drop down is in application.js if you want to look at it go to the github application.

上面的测试访问的页面有一个 ajax selectize 下拉列表和两个正常的 select 元素。它试图将文本 - 'exercism.io' - 放在 selectize 下拉列表中(通常我有另一行来实际模拟按下 enter 键,但是没有那一行就会发生错误)然后它继续设置 dropdown1 和 dropdown2 的值。我已经进行了测试 js: truesleep 2 以使 ajax 正常工作,因此您可以看到测试 运行 时实际发生了什么。

问题是它无法设置 dropdown1 的值。当您 运行 测试并查看发生了什么时,您可以看到它找到了要设置的值,但实际上并没有设置它。它只是移动到下一个 select.

另一件奇怪的事情是,如果我按如下方式更改测试,测试就会通过。所以我很确定它与 selectize 下拉列表的设置有关。

bug_spec.rb

feature 'bug' do
  it "spec setup", js: true do
    visit bug_path

    select 'Four', from: 'dropdown1' # this is not getting selected
    select 'Four', from: 'dropdown2'
    find('div.selectize-input input', match: :first).set('exercism.io')

    sleep 2

    expect(page).to have_select('dropdown1', selected: 'Four') # testing that dropdown1 is being selected
  end
end

我已经在 demo application 中复制了这个错误,可以在 github 上找到。

抱歉,如果这个问题很长,我真的不确定这个问题还能怎么表达。

请注意,此示例已精简。在我的实际代码中,我使用了 provided 的代码来一起使用水豚和 selectize。

我得到了关于水豚的答案forums

看起来这是@tgf 提到的浏览器焦点问题(在 link 中)

谢谢。