Skip to content

Instantly share code, notes, and snippets.

@NaN1488
Forked from erkattak/jquery_chosen_helper.rb
Last active December 15, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaN1488/5240475 to your computer and use it in GitHub Desktop.
Save NaN1488/5240475 to your computer and use it in GitHub Desktop.
Update to work with chosen jquery plugin. If don't know the id of the select and find option by text rather than id. Whitout using execute_script
# Example:
# chosen_select 'select[name="projects"]', "ProjectA"
#
def chosen_select (css_select, option_text)
element_id = page.find(css_select)['id']
page.driver.browser.mouse.down find("##{element_id}_chzn").native
within("##{element_id}_chzn") do
find(:xpath,"//li[text()=\"#{option_text}\"]").click
end
end
@sjtipton
Copy link

Does this happen to work using Capybara 2.0?

@NaN1488
Copy link
Author

NaN1488 commented Apr 2, 2013

Hi @sjtipton
It was tested in Capybara 1.1.1.
If you test it in Capybara 2.0, please let me know if it works.
Thanks!

@Paxa
Copy link

Paxa commented Apr 22, 2015

For current version of capybara and choosen this working:

  def chosen_select(css_selector, value)
    page.evaluate_script(%{$('#{css_selector}').val('#{value}').trigger("chosen:updated");})
  end

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