在Rails 3中用Cucumber/Capybara测试jQuery标记化自动完成



我需要在应用程序中测试自动完成,但我遇到了一些问题。我正在使用以下脚本:http://loopj.com/jquery-tokeninput/并遵循了本教程:http://railscasts.com/episodes/258-token-fields

我尝试了一些类似的解决方案,但没有成功:http://blog.aentos.com/post/1114672143/testing-ajax-autocomplete-fields-with-cucumber-and

黄瓜(场景):

When I fill in "token-input-article_tag_tokens" with "R"
Then I should see the following autocomplete options:
| Ruby |
And I click on the "Ruby" autocomplete option

水豚(步骤):

Then /^I should see the following autocomplete options:$/ do |table|
  table.raw.each do |row|
    page.should have_css('div.token-input-dropdown-facebook ul li', :text => "#{row[0]}")
  end
end
When /^I click on the "([^"]*)" autocomplete option$/ do |link_text|
  page.execute_script %Q{ $('.token-input-dropdown-facebook ul li:contains("#{link_text}")').trigger("mouseenter").click(); }
end

HTML(表单):

<div class="controls">
  <ul class="token-input-list-facebook">
    <li class="token-input-input-token-facebook">
      <input type="text" autocomplete="off" style="outline: none; width: 30px; " id="token-input-article_tag_tokens">
      <tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: normal; letter-spacing: normal; white-space: nowrap; "></tester>
    </li>
  </ul><input class="text_field" data-pre="[]" id="article_tag_tokens" name="article[tag_tokens]" size="30" type="text" style="display: none; ">
</div>

HTML(JQuery生成的自动完成列表)

<div class="token-input-dropdown-facebook" style="display: block; position: absolute; top: 931px; left: 192px; ">
  <ul style="display: block; ">
    <li class="token-input-dropdown-item2-facebook token-input-selected-dropdown-item-facebook">
      <b>R</b>uby
    </li>
  </ul>
</div>

但是,当我运行黄瓜时,我会出现以下错误:

And I fill in "token-input-article_tag_tokens" with "R" 
  cannot fill in, no text field, text area or password field with id, name, or label 'token-input-article_tag_tokens' found (Capybara::ElementNotFound)

我为您的问题找到了解决方案。也许它可以帮助

page.execute_script %Q{ $('li.token-input-dropdown-item2-facebook:first').trigger('mousedown') }

最新更新