Pedro Souza
Pedro Souza

Reputation: 337

Testing jQuery Tokenizing Autocomplete with Cucumber/Capybara in Rails 3

I need to test autocomplete in my application, but im with some problems. I'm using this script: http://loopj.com/jquery-tokeninput/ and have followed this tutorial: http://railscasts.com/episodes/258-token-fields

I tried a few similar solutions, but without success: http://blog.aentos.com/post/1114672143/testing-ajax-autocomplete-fields-with-cucumber-and

Cucumber (Scenario):

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

Capybara (steps):

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 (form):

<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 (Generated autocomplete list by 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>

But, when i run cucumber i take this error:

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)

Upvotes: 0

Views: 1037

Answers (1)

skopu
skopu

Reputation: 96

I have a solution for your problem. Maybe it can help someone

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

Upvotes: 1

Related Questions