I got this cucumber sceanario:
When I fill in "End User" with "john"
Then wait
Then wait
When I click "John Doe"
Then show me the page
Step definitions:
Then /^wait$/ do
sleep 2
end
When /^(?:|I )click "([^"]*)"$/ do |selector|
find(":contains('#{selector}')").click
end
It passes but it doesn't select a user."End User" equals "john" in 'show me the page'.
I even can't get it to work in a javascript console. The following code does not select anything.
$(":contains('John Doe')").last().trigger('click')
# => [<a class="ui-corner-all" tabindex="-1"...
How can I script a autocomplete select? Be it in pure javascript or in cucumber.
While this is not a solution, this could lead you down the path of the solution:
the click event is bound to the UL, no the a or li:
$('ul.ui-autocomplete').click();
However, this didn't work for me. I imagine the click event relies on some sort of state with the (a)s and the (li)s. It adds a few classes and an ID to the currently hovered item which I simulated...
$('a.ui-corner-all').attr('id','ui-active-menuitem') $('a.ui-corner-all').addClass('ui-active-menuitem')
Still no dice. No errors, but no action either.
This should lead to the correct path...I just wish I could have figured it out!
Give this a go
Use like so
I myself bumped into the same pain spot too. After spending few hours on this, I have one good helper that works both with selenium and polstergeist plus no usage of
sleep()
. The following code has been tested with Capybara 2.1.0:Basically, I tell Capybara to fill in the input field then use JS to trigger the
keydown
event to activate autocomplete. However instead ofsleep()
, I take advantage ofpage.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
that wait till the dropdown list appeared. Then I use JS to trigger themouseenter
event then click. I wish that there are better way than doing things with JS eval, but this is the most reliable solution that I could come up.You need to first trigger a mouseover, then a click.