AJAX trigger autocomplete results with Cucumber/Capybara/Selenium

When I run my tests I can’t get the autocomplete dropdown menu to display unless I physically click on the search input field.

It is a Cucumber test using Selenium Webdriver and the results are coming from the Crafty Clicks Address Autocomplete API.

I have tried numerous versions of the same solution from several other posts/sources, all of which look something like this:

def fill_in_autocomplete(selector, value)
  page.execute_script("
    window.onload = function() {
      $('#{selector}').focus().val('#{value}').keydown();
    }
  ")
end

fill_in_autocomplete "#address_lookup", with: "EC2A 1AF"

I’ve also tried more specific keydown-based solutions like:

var e = jQuery.Event("keydown");
e.which = 50;
$("input").trigger(e);

I’ve also paused the test and tested each of the solutions manually in the console but again none work.

It’s worth noting that I’m not using jQuery UI so no .autocomplete() solutions are relevant.

I’ve also used sleep throughout the process to allow for any issues around timing.

I’m out of ideas!

Turns out this is an issue with some of the more recent versions of Firefox. At first I was advised to downgrade FF but instead I chose to switch to chromedriver instead (I used the chromedriver-helper gem) and everything worked perfectly.

Interesting side note, one of the reasons it took me so long to figure this out is because I was trying to trigger the focus of the input field in the Chrome console, as a way to figure out what code I needed to use in my tests. The problem with this is that apparently when you have the console open in Chrome you can’t take focus away from the console! So whatever method I tried to use to focus on the input field from the console was overridden by the fact that the focus was on the console itself. I got around it in the end by putting the focus code in a click event and attaching it to an element on the page. Then I closed the console, clicked on the element and focus shifted to the autocomplete input field.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.