Finally, autocomplete tests were obtained that work with at least the Selenium driver.
The solution is to focus on the field and fire a keydown event.
I confirmed this first with manual testing in the browser. If I use a mouse (not Ctrl-V) to insert a search query into the autocomplete field, nothing happens - not a single drop-down list is displayed. This seems to be equivalent to what happens when Capybara sends the fill_in command. However, after the term is in the field, while the focus is still in the field, if I touch any key, for example. Shift key appears selection list. Thus, it is obvious that the selection list appears only when a key is pressed.
Option 1
One solution is to extend the function from the original question as follows:
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 = "ul.ui-autocomplete a:contains('#{options[:select]}')" page.should have_selector selector page.execute_script "$(\"#{selector}\").mouseenter().click()" end
and then call it like this:
fill_autocomplete "to_contact_name", with: "Jone", select: "Bob Jones"
Option 2
A similar approach, adapted from the Steak test , basically the rails3-jquery-autocomplete gem, uses the standard fill_in followed by this select_autocomplete_result:
def choose_autocomplete_result(item_text, input_selector="input[data-autocomplete]") page.execute_script %Q{ $('#{input_selector}').trigger("focus") } page.execute_script %Q{ $('#{input_selector}').trigger("keydown") }
which is called like this:
fill_in "to_contact_name", :with => "Jone" choose_autocomplete_result "Bob Jones", "#to_contact_name"
I took the second approach for my tests. This seems pretty reliable with Selenium, but it doesnโt work with webkit, which is too bad since the Selenium tests are rather slow compared. A solution that works under webkit is welcome!