Skip to content

Instantly share code, notes, and snippets.

@twe4ked
Created May 8, 2012 00:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twe4ked/2631690 to your computer and use it in GitHub Desktop.
Save twe4ked/2631690 to your computer and use it in GitHub Desktop.
Testing chosen with RSpec.
def search_chosen(selector, search)
page.execute_script "jQuery('#{selector}').click();"
find('div.chzn-search input').set search
end
def select_from_chosen(selector, name)
page.execute_script "jQuery('#{selector}').click();"
page.execute_script "jQuery(\".chzn-results .active-result:contains('#{name}')\").click();"
wait_for_ajax
end
def visible_in_chosen(*args)
within '.chzn-drop' do
args.each do |text|
page.should have_css('li', :text => text)
end
end
end
def not_visible_in_chosen(*args)
within '.chzn-drop' do
args.each do |text|
page.should have_no_css('li', :text => text)
end
end
end
def wait_for_ajax
if Capybara.current_driver == :selenium
delay = Capybara.default_wait_time/100.0
100.times do
break if evaluate_script('jQuery.active').to_i == 0
sleep delay
end
unless evaluate_script('jQuery.active').to_i == 0
raise "Giving up waiting for AJAX to complete after #{Capybara.default_wait_time} seconds"
end
end
end
@dbarrionuevo
Copy link

This code was very helpful, but I needed to change the click event, for a mouseup event, something like:

page.execute_script "jQuery(".chzn-results .active-result:contains('#{name}')").trigger('mouseup');"

@andreorvalho
Copy link

how can I be sure that the result got chosen when I do select_from_chosen ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment