Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing JQuery autocomplete ui with cucumber

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.

like image 843
Tom Maeckelberghe Avatar asked Dec 13 '22 12:12

Tom Maeckelberghe


1 Answers

Give this a go

When /^I type in "([^\"]*)" into autocomplete list "([^\"]*)" and I choose "([^\"]*)"$/ do |typed, input_name,should_select|
   page.driver.browser.execute_script %Q{ $('input[data-autocomplete]').trigger("focus") }
   fill_in("#{input_name}",:with => typed)
   page.driver.browser.execute_script %Q{ $('input[data-autocomplete]').trigger("keydown") }
   sleep 1
   page.driver.browser.execute_script %Q{ $('.ui-menu-item a:contains("#{should_select}")').trigger("mouseenter").trigger("click"); }
end

Use like so

And I type in "Foo" into autocomplete list "input_id" and I choose "Foobar"
like image 176
Pallinder Avatar answered Jan 31 '23 02:01

Pallinder